@@ -3,6 +3,8 @@ package org.modelix.model.server
33import io.ktor.server.testing.ApplicationTestBuilder
44import io.ktor.server.testing.runTestApplication
55import io.ktor.test.dispatcher.runTestWithRealTime
6+ import kotlinx.coroutines.TimeoutCancellationException
7+ import kotlinx.coroutines.withTimeout
68import mu.KotlinLogging
79import org.modelix.datastructures.history.PaginationParameters
810import org.modelix.model.IVersion
@@ -17,6 +19,7 @@ import org.modelix.model.server.store.InMemoryStoreClient
1719import kotlin.random.Random
1820import kotlin.test.Test
1921import kotlin.test.assertEquals
22+ import kotlin.time.Duration
2023import kotlin.time.Duration.Companion.minutes
2124import kotlin.time.Duration.Companion.seconds
2225
@@ -26,19 +29,21 @@ class HistoryIndexPaginationTest {
2629
2730 private lateinit var statistics: StoreClientWithStatistics
2831 private fun runTest (block : suspend ApplicationTestBuilder .() -> Unit ) = runTestWithRealTime(timeout = 3 .minutes) {
29- runTestApplication {
30- application {
31- try {
32- installDefaultServerPlugins()
33- statistics = StoreClientWithStatistics (InMemoryStoreClient ())
34- val repoManager = RepositoriesManager (statistics)
35- ModelReplicationServer (repoManager).init (this )
36- IdsApiImpl (repoManager).init (this )
37- } catch (ex: Throwable ) {
38- LOG .error(" " , ex)
32+ retryOnTimeout(30 .seconds) {
33+ runTestApplication {
34+ application {
35+ try {
36+ installDefaultServerPlugins()
37+ statistics = StoreClientWithStatistics (InMemoryStoreClient ())
38+ val repoManager = RepositoriesManager (statistics)
39+ ModelReplicationServer (repoManager).init (this )
40+ IdsApiImpl (repoManager).init (this )
41+ } catch (ex: Throwable ) {
42+ LOG .error(" " , ex)
43+ }
3944 }
45+ block()
4046 }
41- block()
4247 }
4348 }
4449
@@ -123,3 +128,18 @@ class HistoryIndexPaginationTest {
123128 assertEquals(expectedOrder.drop(skip).take(limit), history.map { it.versionHash })
124129 }
125130}
131+
132+ suspend fun retryOnTimeout (timeout : Duration , body : suspend () -> Unit ) {
133+ var attempt = 0
134+ while (true ) {
135+ attempt++
136+ try {
137+ withTimeout(timeout) {
138+ body()
139+ }
140+ return
141+ } catch (ex: TimeoutCancellationException ) {
142+ if (attempt >= 3 ) throw ex
143+ }
144+ }
145+ }
0 commit comments