Skip to content

Commit 7dce6e1

Browse files
authored
Add waiting for secondary cache call in functional tests (#163)
1 parent d25892e commit 7dce6e1

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ class WebCacheContainerClient(mockServerHost: String, mockServerPort: Int) {
4040
.withBody(body, mediaType)
4141
)
4242

43-
fun getSecondaryCacheRecordedRequests(uuidKey: String): Array<out HttpRequest>? =
44-
mockServerClient.retrieveRecordedRequests(getSecondaryCacheRequest(uuidKey))
43+
fun getSecondaryCacheRecordedRequests(uuidKey: String): Array<out HttpRequest>? {
44+
val secondaryCacheRequest = getSecondaryCacheRequest(uuidKey)
45+
waitUntil({ mockServerClient.retrieveRecordedRequests(secondaryCacheRequest)!!.isNotEmpty() })
46+
return mockServerClient.retrieveRecordedRequests(secondaryCacheRequest)
47+
}
4548

4649
fun initSecondaryCacheResponse(): Array<out Expectation>? =
4750
mockServerClient.`when`(getSecondaryCacheRequest())
@@ -59,4 +62,15 @@ class WebCacheContainerClient(mockServerHost: String, mockServerPort: Int) {
5962
request().withMethod(POST.name())
6063
.withPath("/$WEB_CACHE_PATH")
6164
.withBody(jsonPath("\$.puts[?(@.key == '$uuidKey')]"))
65+
66+
private fun waitUntil(closure: () -> Boolean, timeoutMs: Long = 5000, pollInterval: Long = 100) {
67+
val startTime = System.currentTimeMillis()
68+
while (System.currentTimeMillis() - startTime <= timeoutMs) {
69+
if (closure()) {
70+
return
71+
}
72+
Thread.sleep(pollInterval)
73+
}
74+
throw IllegalStateException("Condition was not fulfilled within $timeoutMs ms.")
75+
}
6276
}

0 commit comments

Comments
 (0)