Skip to content

Commit

Permalink
Stabilize analytics tests for Pubstack (#1545)
Browse files Browse the repository at this point in the history
Stabilize analytics tests for Pubstack
  • Loading branch information
mtuchkova authored Oct 22, 2021
1 parent 4a57891 commit 9fe1d87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/test/groovy/org/prebid/server/functional/AnalyticsSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@ package org.prebid.server.functional

import org.prebid.server.functional.model.mock.services.pubstack.PubStackResponse
import org.prebid.server.functional.model.request.auction.BidRequest
import org.prebid.server.functional.service.PrebidServerService
import org.prebid.server.functional.testcontainers.Dependencies
import org.prebid.server.functional.testcontainers.PBSTest
import org.prebid.server.functional.testcontainers.scaffolding.PubStackAnalytics
import spock.lang.Retry
import spock.lang.Shared

@Retry
@PBSTest
class AnalyticsSpec extends BaseSpec {

private static final String scopeId = UUID.randomUUID()
private static final String SCOPE_ID = UUID.randomUUID()
private static final PrebidServerService pbsService = pbsServiceFactory.getService(pbsServiceFactory.pubstackAnalyticsConfig(SCOPE_ID))

@Shared
PubStackAnalytics analytics = new PubStackAnalytics(Dependencies.networkServiceContainer, mapper).tap {
it.setResponse(PubStackResponse.getDefaultPubStackResponse(scopeId, Dependencies.networkServiceContainer.rootUri))
it.setResponse(PubStackResponse.getDefaultPubStackResponse(SCOPE_ID, Dependencies.networkServiceContainer.rootUri))
}

def "PBS should send PubStack analytics when analytics.pubstack.enabled=true"() {
given: "Pbs config"
def pbsService = pbsServiceFactory.getService(pbsServiceFactory.pubstackAnalyticsConfig(scopeId))

and: "Basic bid request"
given: "Basic bid request"
def bidRequest = BidRequest.defaultBidRequest

and: "Initial request count"
Expand All @@ -32,7 +29,7 @@ class AnalyticsSpec extends BaseSpec {
when: "PBS processes auction request"
pbsService.sendAuctionRequest(bidRequest)

then: "Analytics request body should be not empty"
assert analytics.requestCount == analyticsRequestCount + 1
then: "PBS should call pubstack analytics"
assert analytics.checkRequestCount(analyticsRequestCount + 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ abstract class NetworkScaffolding {
.size()
}

boolean checkRequestCount(int expectedCount, int pollTime = 1000, int pollFrequency = 50) {
def expectedCountReached = false
def startTime = System.currentTimeMillis()
def elapsedTime = 0

while (elapsedTime < pollTime) {
def requestCount = getRequestCount()
if (requestCount == expectedCount) {
expectedCountReached = true
break
} else if (requestCount > expectedCount) {
throw new IllegalStateException("The number of recorded requests: $requestCount exceeds the expected number: $expectedCount")
} else {
elapsedTime += System.currentTimeMillis() - startTime
Thread.sleep(pollFrequency)
}
}

expectedCountReached
}

void setResponse(HttpRequest httpRequest, ResponseModel responseModel) {
def mockResponse = mapper.encode(responseModel)
mockServerClient.when(httpRequest, Times.exactly(1))
Expand Down

0 comments on commit 9fe1d87

Please sign in to comment.