Skip to content

Commit 3429bd4

Browse files
committed
Fixed flaky tests, optimized internal structure
1 parent af9deb5 commit 3429bd4

File tree

27 files changed

+176
-92
lines changed

27 files changed

+176
-92
lines changed

gradle-conventions/src/main/kotlin/util/targets/configure.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package util.targets
77
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
88
import org.gradle.api.Action
99
import org.gradle.api.Project
10-
import org.gradle.jvm.toolchain.JavaLanguageVersion
1110
import org.gradle.kotlin.dsl.the
1211
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
1312
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
@@ -31,7 +30,9 @@ private fun KotlinMultiplatformExtension.configureTargets(config: KmpConfig): Li
3130
if (config.js) {
3231
js(IR) {
3332
nodejs()
34-
browser()
33+
if (!config.kotlinMasterBuild) {
34+
browser()
35+
}
3536

3637
binaries.library()
3738
}.also { targets.add(it) }

gradle-conventions/src/main/kotlin/util/targets/wasm.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ fun KmpConfig.configureWasm() {
2424
wasmJs {
2525
configureJsAndWasmJsTasks()
2626

27-
browser()
27+
if (!kotlinMasterBuild) {
28+
browser()
29+
}
30+
2831
nodejs()
2932
if (wasmJsD8) {
3033
// this platform needs some care KRPC-210

krpc/krpc-client/src/commonMain/kotlin/kotlinx/rpc/krpc/client/KrpcClient.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import kotlinx.coroutines.CancellationException
99
import kotlinx.coroutines.CompletableDeferred
1010
import kotlinx.coroutines.CoroutineName
1111
import kotlinx.coroutines.CoroutineScope
12-
import kotlinx.coroutines.DelicateCoroutinesApi
13-
import kotlinx.coroutines.GlobalScope
14-
import kotlinx.coroutines.InternalCoroutinesApi
1512
import kotlinx.coroutines.SupervisorJob
1613
import kotlinx.coroutines.cancel
1714
import kotlinx.coroutines.cancelAndJoin
@@ -79,7 +76,6 @@ public abstract class InitializedKrpcClient(
7976
* serializing data, tracking streams, processing exceptions, and other protocol responsibilities.
8077
* Leaves out the delivery of encoded messages to the specific implementations with [KrpcTransport].
8178
*/
82-
@OptIn(InternalCoroutinesApi::class)
8379
public abstract class KrpcClient : RpcClient, KrpcEndpoint {
8480
/**
8581
* Called once to provide [KrpcTransport] for this client.
@@ -154,21 +150,22 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
154150

155151
val context = SupervisorJob(transport.coroutineContext.job)
156152

157-
context.job.invokeOnCompletion(onCancelling = true) {
158-
if (clientCancelled) {
159-
return@invokeOnCompletion
160-
}
161-
162-
clientCancelled = true
153+
context.job.invokeOnCompletion {
154+
try {
155+
if (!clientCancelled && !clientCancelledByServer) {
156+
sendCancellation(CancellationType.ENDPOINT, null, null, closeTransportAfterSending = true)
157+
}
163158

164-
if (!clientCancelledByServer) {
165-
sendCancellation(CancellationType.ENDPOINT, null, null, closeTransportAfterSending = true)
166-
}
159+
clientCancelled = true
160+
} catch (_ : Exception) {
161+
// ignore, we are already cancelled
162+
} finally {
163+
requestChannels.values.forEach {
164+
val cause = CancellationException("Client cancelled")
165+
it.close(cause)
166+
it.cancel(cause)
167+
}
167168

168-
@OptIn(DelicateCoroutinesApi::class)
169-
@Suppress("detekt.GlobalCoroutineUsage")
170-
GlobalScope.launch(CoroutineName("client-request-channels-closing")) {
171-
requestChannels.values.forEach { it.close(CancellationException("Client cancelled")) }
172169
requestChannels.clear()
173170
}
174171
}
@@ -333,6 +330,7 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
333330
throw e
334331
} finally {
335332
channel.close()
333+
channel.cancel()
336334
requestChannels.remove(callId)
337335
connector.unsubscribeFromMessages(call.descriptor.fqName, callId)
338336
}
@@ -484,7 +482,7 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
484482
} catch (e: CancellationException) {
485483
internalScope.ensureActive()
486484

487-
failure = ManualCancellationException(e)
485+
failure = e
488486

489487
// stop the flow and its coroutine, other flows are not affected
490488
throw e

krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.kt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public fun serializeException(cause: Throwable): SerializedException {
1313
val message = cause.message ?: "Unknown exception"
1414
val stacktrace = cause.stackElements()
1515
val serializedCause = cause.cause?.let { serializeException(it) }
16-
val className = cause::class.rpcInternalTypeName ?: ""
16+
val className = if (cause is CancellationException || cause is kotlin.coroutines.cancellation.CancellationException) {
17+
CancellationException::class.rpcInternalTypeName ?: "kotlinx.coroutines.CancellationException"
18+
} else {
19+
cause::class.rpcInternalTypeName ?: ""
20+
}
1721

1822
return SerializedException(cause.toString(), message, stacktrace, serializedCause, className)
1923
}
@@ -22,16 +26,15 @@ internal expect fun Throwable.stackElements(): List<StackElement>
2226

2327
internal expect fun SerializedException.deserializeUnsafe(): Throwable
2428

25-
internal fun SerializedException.nonJvmManualCancellationExceptionDeserialize(): ManualCancellationException? {
26-
if (className == ManualCancellationException::class.rpcInternalTypeName) {
27-
val cancellation = cause?.deserializeUnsafe()
28-
?: error("ManualCancellationException must have a cause")
29+
internal fun SerializedException.cancellationExceptionDeserialize(): CancellationException? {
30+
if (className == CancellationException::class.rpcInternalTypeName
31+
|| className == kotlin.coroutines.cancellation.CancellationException::class.rpcInternalTypeName
32+
) {
33+
val cause = this@cancellationExceptionDeserialize.cause?.deserializeUnsafe()
2934

30-
return ManualCancellationException(
31-
CancellationException(
32-
message = cancellation.message,
33-
cause = cancellation.cause,
34-
)
35+
return CancellationException(
36+
message = message,
37+
cause = cause,
3538
)
3639
}
3740

@@ -44,29 +47,19 @@ public fun SerializedException.deserialize(): Throwable {
4447
deserializeUnsafe()
4548
}
4649

47-
val result = if (cause.isFailure) {
50+
return if (cause.isFailure) {
4851
cause.exceptionOrNull()!!
4952
} else {
50-
val ex = cause.getOrNull()!!
51-
if (ex is ManualCancellationException) {
52-
ex.cause
53-
} else {
54-
ex
55-
}
53+
cause.getOrNull()!!
5654
}
57-
58-
return result
5955
}
6056

61-
@InternalRpcApi
62-
public class ManualCancellationException(override val cause: CancellationException): RuntimeException()
63-
6457
internal expect class DeserializedException(
6558
toStringMessage: String,
6659
message: String,
6760
stacktrace: List<StackElement>,
6861
cause: SerializedException?,
69-
className: String
62+
className: String,
7063
) : Throwable {
7164
override val message: String
7265
}

krpc/krpc-core/src/commonTest/kotlin/kotlinx/rpc/krpc/KrpcSendHandlerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal abstract class KrpcSendHandlerBaseTest {
111111
}
112112

113113
protected fun runTest(
114-
timeout: Duration = 10.seconds,
114+
timeout: Duration = 30.seconds,
115115
body: suspend TestScope.(Channel<KrpcTransportMessage>, KrpcSendHandler) -> Unit,
116116
) = runTestWithCoroutinesProbes(timeout = timeout) {
117117
val channel = Channel<KrpcTransportMessage>(

krpc/krpc-core/src/jsMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.js.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ internal actual class DeserializedException actual constructor(
2323
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2424

2525
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
26-
return nonJvmManualCancellationExceptionDeserialize()
26+
return cancellationExceptionDeserialize()
2727
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2828
}

krpc/krpc-core/src/jvmMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.jvm.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ internal actual fun Throwable.stackElements(): List<StackElement> = stackTrace.m
3737
}
3838

3939
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
40+
val cancellationException = cancellationExceptionDeserialize()
41+
if (cancellationException != null) {
42+
return cancellationException
43+
}
44+
4045
try {
4146
val clazz = Class.forName(className)
4247
val fieldsCount = clazz.fieldsCountOrDefault(throwableFields)

krpc/krpc-core/src/nativeMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.native.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ internal actual class DeserializedException actual constructor(
2222
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2323

2424
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
25-
return nonJvmManualCancellationExceptionDeserialize()
25+
return cancellationExceptionDeserialize()
2626
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2727
}

krpc/krpc-core/src/wasmJsMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.wasm.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ internal actual class DeserializedException actual constructor(
2323
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2424

2525
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
26-
return nonJvmManualCancellationExceptionDeserialize()
26+
return cancellationExceptionDeserialize()
2727
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2828
}

krpc/krpc-core/src/wasmWasiMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.wasmWasi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ internal actual class DeserializedException actual constructor(
2020
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2121

2222
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
23-
return nonJvmManualCancellationExceptionDeserialize()
23+
return cancellationExceptionDeserialize()
2424
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2525
}

0 commit comments

Comments
 (0)