Skip to content

Commit 797c207

Browse files
committed
DOPE-176: implement retry mechanism for index creation in CouchbaseDatabase tests
1 parent 0119ae3 commit 797c207

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

couchbase/src/integrationTest/kotlin/ch/ergon/dope/integrationTest/TestCouchbaseDatabase.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ object TestCouchbaseDatabase {
6666
}
6767

6868
private fun ensureAuditCollections() = runBlocking {
69+
cluster.waitUntilReady(MAX_TIMEOUT_IN_SECONDS.seconds)
6970
cluster.waitUntilReady(MAX_TIMEOUT_IN_SECONDS.seconds)
7071
cluster.query("CREATE SCOPE `$BUCKET`.`app` IF NOT EXISTS").execute()
7172
cluster.query("CREATE COLLECTION `$BUCKET`.`app`.`order_audit` IF NOT EXISTS").execute()
@@ -74,11 +75,22 @@ object TestCouchbaseDatabase {
7475
private fun ensureIndexes() = runBlocking {
7576
cluster.waitUntilReady(MAX_TIMEOUT_IN_SECONDS.seconds)
7677
cluster.query("CREATE PRIMARY INDEX IF NOT EXISTS ON `$BUCKET`").execute()
77-
cluster.query("CREATE PRIMARY INDEX IF NOT EXISTS ON `$BUCKET`.`app`.`order_audit`").execute()
78+
retryOnFailure { cluster.query("CREATE PRIMARY INDEX IF NOT EXISTS ON `$BUCKET`.`app`.`order_audit`").execute() }
7879
cluster.query("CREATE INDEX IF NOT EXISTS `ix_order_employee` ON `$BUCKET`(`employee`) WHERE `type` = \"order\"").execute()
7980
cluster.query("CREATE INDEX IF NOT EXISTS `ix_order_client` ON `$BUCKET`(`client`) WHERE `type` = \"order\"").execute()
8081
}
8182

83+
private suspend fun <T> retryOnFailure(maxRetries: Int = MAX_RETRIES, delayMs: Long = 2000, action: suspend () -> T): T {
84+
repeat(maxRetries - 1) {
85+
try {
86+
return action()
87+
} catch (_: Exception) {
88+
Thread.sleep(delayMs)
89+
}
90+
}
91+
return action()
92+
}
93+
8294
private fun initDatabase() {
8395
val bucket = cluster.bucket(BUCKET)
8496
val collection = bucket.defaultCollection()

0 commit comments

Comments
 (0)