Skip to content

Commit 64cc9a5

Browse files
committed
test with waitUntil
1 parent 3778af4 commit 64cc9a5

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/test/kotlin/org/prebid/cache/functional/SecondaryCacheSpec.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class SecondaryCacheSpec : ShouldSpec({
3434
specPrebidCacheConfig = BaseSpec.prebidCacheConfig.getBaseAerospikeConfig(true) +
3535
BaseSpec.prebidCacheConfig.getSecondaryCacheConfig(webCacheContainerUri)
3636
prebidCacheApi = BaseSpec.getPrebidCacheApi(specPrebidCacheConfig)
37-
val requestObject = RequestObject.getDefaultJsonRequestObject().apply { puts[0].key = getRandomUuid() }
38-
prebidCacheApi.postCache(requestObject, "no")
3937
}
4038

4139
afterSpec {
@@ -59,7 +57,7 @@ class SecondaryCacheSpec : ShouldSpec({
5957

6058
// and: Request to secondary cache was sent
6159
val secondaryCacheRecordedRequests =
62-
webCacheContainerClient.getSecondaryCacheRecordedRequests(requestObject.puts[0].key!!)
60+
webCacheContainerClient.checkRecordedRequests(requestObject.puts[0].key!!)
6361
secondaryCacheRecordedRequests?.size shouldBe 1
6462

6563
// and: Request contained secondaryCache=yes query parameter
@@ -89,7 +87,7 @@ class SecondaryCacheSpec : ShouldSpec({
8987

9088
// and: Request to secondary cache was sent
9189
val secondaryCacheRecordedRequests =
92-
webCacheContainerClient.getSecondaryCacheRecordedRequests(requestObject.puts[0].key!!)
90+
webCacheContainerClient.checkRecordedRequests(requestObject.puts[0].key!!)
9391
secondaryCacheRecordedRequests?.size shouldBe 1
9492

9593
// and: Secondary cache request 'expiry' parameter matches to the PBC request 'ttlseconds' parameter
@@ -116,7 +114,7 @@ class SecondaryCacheSpec : ShouldSpec({
116114

117115
// and: Request to secondary cache was sent
118116
val secondaryCacheRecordedRequests =
119-
webCacheContainerClient.getSecondaryCacheRecordedRequests(requestObject.puts[0].key!!)
117+
webCacheContainerClient.checkRecordedRequests(requestObject.puts[0].key!!)
120118
secondaryCacheRecordedRequests?.size shouldBe 1
121119

122120
// and: Secondary cache request 'expiry' parameter matches to the Prebid Cache 'cache.expiry.sec' config property
@@ -146,7 +144,7 @@ class SecondaryCacheSpec : ShouldSpec({
146144

147145
// and: Request to secondary cache was sent
148146
val secondaryCacheRecordedRequests =
149-
webCacheContainerClient.getSecondaryCacheRecordedRequests(requestObject.puts[0].key!!)
147+
webCacheContainerClient.checkRecordedRequests(requestObject.puts[0].key!!)
150148
secondaryCacheRecordedRequests?.size shouldBe 1
151149

152150
// and: Secondary cache request 'expiry' parameter matches to the Prebid Cache 'cache.max.expiry' config property
@@ -176,7 +174,7 @@ class SecondaryCacheSpec : ShouldSpec({
176174

177175
// and: Request to secondary cache was sent
178176
val secondaryCacheRecordedRequests =
179-
webCacheContainerClient.getSecondaryCacheRecordedRequests(requestObject.puts[0].key!!)
177+
webCacheContainerClient.checkRecordedRequests(requestObject.puts[0].key!!)
180178
secondaryCacheRecordedRequests?.size shouldBe 1
181179

182180
// and: Secondary cache request 'expiry' parameter matches to the Prebid Cache 'cache.min.expiry' config property

src/test/kotlin/org/prebid/cache/functional/testcontainers/client/WebCacheContainerClient.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,29 @@ class WebCacheContainerClient(mockServerHost: String, mockServerPort: Int) {
5959
request().withMethod(POST.name())
6060
.withPath("/$WEB_CACHE_PATH")
6161
.withBody(jsonPath("\$.puts[?(@.key == '$uuidKey')]"))
62+
63+
private fun waitUntil(closure: () -> Boolean, timeoutMs: Long = 5000, pollInterval: Long = 100) {
64+
var isConditionFulfilled = false
65+
val waiterStartTime = System.currentTimeMillis()
66+
var waiterElapsedTime = 0L
67+
68+
while (waiterElapsedTime <= timeoutMs) {
69+
if (closure()) {
70+
isConditionFulfilled = true
71+
break
72+
} else {
73+
waiterElapsedTime = System.currentTimeMillis() - waiterStartTime
74+
Thread.sleep(pollInterval)
75+
}
76+
}
77+
78+
if (!isConditionFulfilled) {
79+
throw IllegalStateException("Condition was not fulfilled within $timeoutMs ms.")
80+
}
81+
}
82+
83+
fun checkRecordedRequests(uuidKey: String): Array<out HttpRequest>? {
84+
waitUntil({ getSecondaryCacheRecordedRequests(uuidKey)?.isNotEmpty() == true }, timeoutMs = 5000)
85+
return getSecondaryCacheRecordedRequests(uuidKey)
86+
}
6287
}

0 commit comments

Comments
 (0)