Skip to content

Commit 25f33cf

Browse files
authored
Fix flaky tests (#481)
1 parent b4db18a commit 25f33cf

File tree

15 files changed

+77
-17
lines changed

15 files changed

+77
-17
lines changed

karma/chrome_bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ config.set({
2121
"client": {
2222
captureConsole: true,
2323
"mocha": {
24-
timeout: 10000
24+
timeout: 300000
2525
}
2626
}
2727
});

krpc/krpc-test/build.gradle.kts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,6 @@ tasks.named<Delete>("clean") {
100100
delete(resourcesPath.walk().filter { it.isFile && it.extension == tmpExt }.toList())
101101
}
102102

103-
tasks.withType<KotlinJsTest> {
104-
onlyIf {
105-
// for some reason browser tests don't wait for the test to complete and end immediately
106-
// KRPC-166
107-
!targetName.orEmpty().endsWith("browser")
108-
}
109-
}
110-
111103
tasks.register("moveToGold") {
112104
doLast {
113105
resourcesPath.walk().forEach {

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ interface KrpcTestService {
9494

9595
suspend fun nullableInt(v: Int?): Int?
9696
suspend fun nullableList(v: List<Int>?): List<Int>?
97+
suspend fun nullableEnum(enum: TestEnum?): TestEnum?
9798
fun delayForever(): Flow<Boolean>
9899

99100
suspend fun answerToAnything(arg: String): Int
100101

101102
suspend fun krpc173()
102103

103104
fun unitFlow(): Flow<Unit>
105+
106+
@Serializable
107+
enum class TestEnum {
108+
ENUM_VALUE_1, ENUM_VALUE_2
109+
}
104110
}

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class KrpcTestServiceBackend : KrpcTestService {
243243

244244
override suspend fun nullableInt(v: Int?): Int? = v
245245
override suspend fun nullableList(v: List<Int>?): List<Int>? = v
246+
override suspend fun nullableEnum(enum: KrpcTestService.TestEnum?): KrpcTestService.TestEnum? = enum
246247

247248
override fun delayForever(): Flow<Boolean> = flow {
248249
emit(true)

krpc/krpc-test/src/commonMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ abstract class KrpcTransportTestBase {
324324

325325
@Test
326326
fun RPC_should_be_able_to_receive_100_000_ints_in_reasonable_time() = runTest(timeout = EXTENDED_TIMEOUT) {
327-
val n = 100_000
327+
val n = iterations_100_000
328328
var counter = 0
329329
val last = client.getNInts(n).onEach {
330330
counter++
@@ -336,8 +336,8 @@ abstract class KrpcTransportTestBase {
336336
}
337337

338338
@Test
339-
fun RPC_should_be_able_to_receive_100_000_ints_with_batching_in_reasonable_time() = runTest {
340-
val n = 100_000
339+
fun RPC_should_be_able_to_receive_100_000_ints_with_batching_in_reasonable_time() = runTest(timeout = EXTENDED_TIMEOUT) {
340+
val n = iterations_100_000
341341
assertEquals(client.getNIntsBatched(n).last().last(), n)
342342
}
343343

@@ -400,6 +400,15 @@ abstract class KrpcTransportTestBase {
400400
assertEquals(listOf(1), client.nullableList(listOf(1)))
401401
}
402402

403+
@Test
404+
open fun testNullableEnums() = runTest {
405+
assertNull(client.nullableEnum(null))
406+
assertEquals(
407+
KrpcTestService.TestEnum.ENUM_VALUE_1,
408+
client.nullableEnum(KrpcTestService.TestEnum.ENUM_VALUE_1),
409+
)
410+
}
411+
403412
@Test
404413
fun testServerCallCancellation() = runTest {
405414
val flag: Channel<Boolean> = Channel()
@@ -500,3 +509,4 @@ abstract class KrpcTransportTestBase {
500509
private val EXTENDED_TIMEOUT = if (isJs) 500.seconds else 200.seconds
501510

502511
internal expect val isJs: Boolean
512+
internal expect val iterations_100_000 : Int

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,7 @@ class ProtoBufLocalTransportTest : LocalTransportTest() {
6565

6666
@Test
6767
override fun testNullableLists(): TestResult = runTest { }
68+
69+
@Test
70+
override fun testNullableEnums(): TestResult = runTest { }
6871
}

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/TransportTest.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,18 @@ class TransportTest {
258258

259259
val server = serverOf(transports)
260260

261-
delay(1000)
261+
repeat(10) {
262+
// give way to requests
263+
yield()
264+
}
262265
val echoServices = server.registerServiceAndReturn<Echo, _> { EchoImpl() }
263266
assertEquals("foo", firstResult.await())
264267
assertEquals(1, echoServices.single().received.value)
265268

266-
delay(1000)
269+
repeat(10) {
270+
// give way to requests
271+
yield()
272+
}
267273
val secondServices = server.registerServiceAndReturn<Second, _> { SecondServer() }
268274
assertEquals("bar", secondResult.await())
269275
assertEquals(1, secondServices.single().received.value)
@@ -272,6 +278,7 @@ class TransportTest {
272278
}
273279

274280
private val handshakeClassSerialName = KrpcProtocolMessage.Handshake.serializer().descriptor.serialName
281+
275282
@Suppress("RegExpRedundantEscape") // fails on js otherwise
276283
private val clientHandshake = ".*\\[Client\\] \\[Send\\] \\{\"type\":\"$handshakeClassSerialName\".*".toRegex()
277284

@@ -299,7 +306,7 @@ class TransportTest {
299306
server.registerServiceAndReturn<Second, _> { SecondServer() }
300307

301308
client.withService<Echo>().apply { echo("foo"); echo("bar") }
302-
client.withService<Second>().apply{ second("bar"); second("baz") }
309+
client.withService<Second>().apply { second("bar"); second("baz") }
303310

304311
assertEquals(1, transportInitialized.value)
305312
assertEquals(1, configInitialized.value)

krpc/krpc-test/src/commonTest/kotlin/kotlinx/rpc/krpc/test/cancellation/CancellationTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,33 @@ class CancellationTest {
127127
service.cancellationInOutgoingStream(
128128
stream = flow {
129129
emit(42)
130+
println("[testCancellationInClientStream] emit 42")
130131
emit(43)
132+
println("[testCancellationInClientStream] emit 43")
131133
},
132134
cancelled = flow {
133135
emit(1)
136+
println("[testCancellationInClientStream] emit 1")
134137
serverInstance().firstIncomingConsumed.await()
138+
println("[testCancellationInClientStream] firstIncomingConsumed")
135139
throw CancellationException("cancellationInClientStream")
136140
},
137141
)
138142
}
139143

140144
requestJob.join()
145+
println("[testCancellationInClientStream] Request job finished")
141146
serverInstance().consumedAll.await()
147+
println("[testCancellationInClientStream] Server consumed all")
142148

143149
assertFalse(requestJob.isCancelled, "Expected requestJob not to be cancelled")
144150
assertContentEquals(listOf(42, 43), serverInstance().consumedIncomingValues)
145151
}
152+
println("[testCancellationInClientStream] Scope finished")
146153

147154
checkAlive()
148155
stopAllAndJoin()
156+
println("[testCancellationInClientStream] All done")
149157

150158
assertEquals(1, serverInstance().cancellationsCounter.value, "Expected 1 request to be cancelled")
151159
}
@@ -154,19 +162,23 @@ class CancellationTest {
154162
@Test
155163
fun testCancelClient() = runCancellationTest {
156164
val firstRequestJob = launch {
165+
println("[testCancelClient] firstRequestJob started")
157166
service.longRequest()
158167
}
159168

160169
val secondService = client.withService<CancellationService>()
161170

162171
val secondRequestJob = launch {
172+
println("[testCancelClient] secondRequestJob started")
163173
secondService.longRequest()
164174
}
165175

166176
val clientFlowJob = launch {
167177
service.outgoingStream(flow {
168178
emit(0)
179+
println("[testCancelClient] emit 0")
169180
serverInstance().fence.await()
181+
println("[testCancelClient] fence awaited")
170182
emit(1)
171183
})
172184
}
@@ -181,18 +193,27 @@ class CancellationTest {
181193
}
182194
}
183195

196+
println("[testCancelClient] Requests sent")
184197
serverInstance().waitCounter.await(4)
198+
println("[testCancelClient] Requests reached")
185199
client.close()
186200
client.awaitCompletion()
201+
println("[testCancelClient] Client stopped")
187202
server.awaitCompletion()
203+
println("[testCancelClient] Server stopped")
188204
firstRequestJob.join()
205+
println("[testCancelClient] First request finished")
189206
secondRequestJob.join()
207+
println("[testCancelClient] Second request finished")
190208
clientFlowJob.join()
209+
println("[testCancelClient] Client flow finished")
191210

192211
serverInstance().fence.complete(Unit)
193212
serverFlowJob.join()
213+
println("[testCancelClient] Server flow finished")
194214

195215
serverInstance().cancellationsCounter.await(4)
216+
println("[testCancelClient] Server cancellations counted")
196217

197218
assertTrue(firstRequestJob.isCancelled, "Expected firstRequestJob to be cancelled")
198219
assertTrue(secondRequestJob.isCancelled, "Expected secondRequestJob to be cancelled")
@@ -203,26 +224,31 @@ class CancellationTest {
203224

204225
checkAlive(clientAlive = false, serverAlive = false)
205226
stopAllAndJoin()
227+
println("[testCancelClient] All done")
206228

207229
assertEquals(4, serverInstance().cancellationsCounter.value, "Expected 4 requests to be cancelled")
208230
}
209231

210232
@Test
211233
fun testCancelServer() = runCancellationTest {
212234
val firstRequestJob = launch {
235+
println("[testCancelServer] firstRequestJob started")
213236
service.longRequest()
214237
}
215238

216239
val secondService = client.withService<CancellationService>()
217240

218241
val secondRequestJob = launch {
242+
println("[testCancelServer] secondRequestJob started")
219243
secondService.longRequest()
220244
}
221245

222246
val clientFlowJob = launch {
223247
service.outgoingStream(flow {
224248
emit(0)
249+
println("[testCancelServer] emit 0")
225250
serverInstance().fence.await()
251+
println("[testCancelServer] fence awaited")
226252
emit(1)
227253
})
228254
}
@@ -237,18 +263,27 @@ class CancellationTest {
237263
}
238264
}
239265

266+
println("[testCancelServer] Requests sent")
240267
serverInstance().waitCounter.await(4) // wait for requests to reach server
268+
println("[testCancelServer] Requests reached")
241269
server.close()
242270
server.awaitCompletion()
271+
println("[testCancelServer] Server stopped")
243272
client.awaitCompletion()
273+
println("[testCancelServer] Client stopped")
244274
firstRequestJob.join()
275+
println("[testCancelServer] First request finished")
245276
secondRequestJob.join()
277+
println("[testCancelServer] Second request finished")
246278
clientFlowJob.join()
279+
println("[testCancelServer] Client flow finished")
247280

248281
serverInstance().fence.complete(Unit)
249282
serverFlowJob.join()
283+
println("[testCancelServer] Server flow finished")
250284

251285
serverInstance().cancellationsCounter.await(4)
286+
println("[testCancelServer] Server cancellations counted")
252287

253288
assertTrue(firstRequestJob.isCancelled, "Expected firstRequestJob to be cancelled")
254289
assertTrue(secondRequestJob.isCancelled, "Expected secondRequestJob to be cancelled")
@@ -259,6 +294,7 @@ class CancellationTest {
259294

260295
checkAlive(clientAlive = false, serverAlive = false)
261296
stopAllAndJoin()
297+
println("[testCancelServer] All done")
262298

263299
assertEquals(4, serverInstance().cancellationsCounter.value, "Expected 4 requests to be cancelled")
264300
}

krpc/krpc-test/src/jsMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.js.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
package kotlinx.rpc.krpc.test
66

77
actual val isJs: Boolean = true
8+
internal actual val iterations_100_000: Int = 10_000

krpc/krpc-test/src/jsTest/kotlin/kotlinx/rpc/krpc/test/TransportTest.js.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
package kotlinx.rpc.krpc.test
66

7-
internal actual val testIterations: Int = 100
7+
internal actual val testIterations: Int = 10

0 commit comments

Comments
 (0)