Skip to content

Commit bb3d05d

Browse files
committed
test(model-server): attempt to fix the flaky history index tests
1 parent 7d03416 commit bb3d05d

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

model-server/src/test/kotlin/org/modelix/model/server/HistoryIndexIntervalTest.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.modelix.model.server
22

33
import io.ktor.server.testing.ApplicationTestBuilder
44
import io.ktor.server.testing.testApplication
5+
import io.ktor.test.dispatcher.runTestWithRealTime
56
import mu.KotlinLogging
67
import org.modelix.datastructures.history.HistoryInterval
78
import org.modelix.model.IVersion
@@ -16,26 +17,31 @@ import org.modelix.model.server.store.InMemoryStoreClient
1617
import kotlin.random.Random
1718
import kotlin.test.Test
1819
import kotlin.test.assertEquals
20+
import kotlin.time.Duration.Companion.minutes
1921
import kotlin.time.Duration.Companion.seconds
2022

2123
private val LOG = KotlinLogging.logger { }
2224

2325
class HistoryIndexIntervalTest {
2426

2527
private lateinit var statistics: StoreClientWithStatistics
26-
private fun runTest(block: suspend ApplicationTestBuilder.() -> Unit) = testApplication {
27-
application {
28-
try {
29-
installDefaultServerPlugins()
30-
statistics = StoreClientWithStatistics(InMemoryStoreClient())
31-
val repoManager = RepositoriesManager(statistics)
32-
ModelReplicationServer(repoManager).init(this)
33-
IdsApiImpl(repoManager).init(this)
34-
} catch (ex: Throwable) {
35-
LOG.error("", ex)
28+
private fun runTest(block: suspend ApplicationTestBuilder.() -> Unit) = runTestWithRealTime(timeout = 3.minutes) {
29+
retryOnTimeout(30.seconds) {
30+
testApplication {
31+
application {
32+
try {
33+
installDefaultServerPlugins()
34+
statistics = StoreClientWithStatistics(InMemoryStoreClient())
35+
val repoManager = RepositoriesManager(statistics)
36+
ModelReplicationServer(repoManager).init(this)
37+
IdsApiImpl(repoManager).init(this)
38+
} catch (ex: Throwable) {
39+
LOG.error("", ex)
40+
}
41+
}
42+
block()
3643
}
3744
}
38-
block()
3945
}
4046

4147
@Test fun interval_0_201_1() = runIntervalTest(0, 201, 1)

model-server/src/test/kotlin/org/modelix/model/server/HistoryIndexPaginationTest.kt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package org.modelix.model.server
33
import io.ktor.server.testing.ApplicationTestBuilder
44
import io.ktor.server.testing.runTestApplication
55
import io.ktor.test.dispatcher.runTestWithRealTime
6+
import kotlinx.coroutines.TimeoutCancellationException
7+
import kotlinx.coroutines.withTimeout
68
import mu.KotlinLogging
79
import org.modelix.datastructures.history.PaginationParameters
810
import org.modelix.model.IVersion
@@ -17,6 +19,7 @@ import org.modelix.model.server.store.InMemoryStoreClient
1719
import kotlin.random.Random
1820
import kotlin.test.Test
1921
import kotlin.test.assertEquals
22+
import kotlin.time.Duration
2023
import kotlin.time.Duration.Companion.minutes
2124
import 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

Comments
 (0)