Skip to content

Commit 6210ad4

Browse files
committed
Some tests updates
1 parent 5b44363 commit 6210ad4

File tree

12 files changed

+104
-57
lines changed

12 files changed

+104
-57
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ abstract class KrpcConnectorBaseTest {
352352
isServer = true,
353353
)
354354

355-
body(clientConnector, serverConnector)
356-
357-
transport.coroutineContext.job.cancelAndJoin()
355+
try {
356+
body(clientConnector, serverConnector)
357+
} finally {
358+
transport.coroutineContext.job.cancelAndJoin()
359+
}
358360
}
359361
}
360362

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ internal abstract class KrpcReceiveBufferBaseTest {
7777
bufferSize = { bufferSize },
7878
)
7979

80-
body(buffer)
81-
82-
buffer.close(null)
80+
try {
81+
body(buffer)
82+
} finally {
83+
buffer.close(null)
84+
}
8385
}
8486
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,15 @@ internal abstract class KrpcReceiveHandlerBaseTest {
257257
val handler = KrpcStoringReceiveHandler(buffer, sender)
258258
val config = TestConfig(handler, buffer, channel, sender, this)
259259

260-
body(config)
261-
262-
handler.close(HandlerKey.Generic, null)
263-
buffer.close(null)
264-
channel.cancel()
265-
channel.close()
266-
senderJob.cancelAndJoin()
260+
try {
261+
body(config)
262+
} finally {
263+
handler.close(HandlerKey.Generic, null)
264+
buffer.close(null)
265+
channel.cancel()
266+
channel.close()
267+
senderJob.cancelAndJoin()
268+
}
267269
}
268270
}
269271

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,13 @@ internal abstract class KrpcSendHandlerBaseTest {
120120
)
121121

122122
val handler = KrpcSendHandler(channel)
123-
body(channel, handler)
124-
handler.close()
125-
channel.cancel()
126-
channel.close()
123+
try {
124+
body(channel, handler)
125+
} finally {
126+
handler.close()
127+
channel.cancel()
128+
channel.close()
129+
}
127130
}
128131
}
129132

krpc/krpc-ktor/krpc-ktor-core/src/jvmTest/kotlin/kotlinx/rpc/krpc/ktor/KtorTransportTest.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import kotlinx.coroutines.debug.DebugProbes
2121
import kotlinx.coroutines.test.runTest
2222
import kotlinx.rpc.annotations.Rpc
2323
import kotlinx.rpc.krpc.client.KrpcClient
24-
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLogger
24+
import kotlinx.rpc.krpc.internal.logging.RpcInternalCommonLogger
2525
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLoggerContainer
26+
import kotlinx.rpc.krpc.internal.logging.dumpLogger
2627
import kotlinx.rpc.krpc.ktor.client.installKrpc
2728
import kotlinx.rpc.krpc.ktor.client.rpc
2829
import kotlinx.rpc.krpc.ktor.client.rpcConfig
@@ -31,8 +32,6 @@ import kotlinx.rpc.krpc.ktor.server.rpc
3132
import kotlinx.rpc.krpc.serialization.json.json
3233
import kotlinx.rpc.withService
3334
import org.junit.Assert.assertEquals
34-
import org.junit.platform.commons.logging.Logger
35-
import org.junit.platform.commons.logging.LoggerFactory
3635
import java.net.ServerSocket
3736
import java.util.concurrent.Executors
3837
import java.util.concurrent.TimeUnit
@@ -245,17 +244,10 @@ class KtorTransportTest {
245244
return port
246245
}
247246

248-
private fun setupLogger(): Logger {
249-
val logger = LoggerFactory.getLogger(KtorTransportTest::class.java)
247+
private fun setupLogger(): RpcInternalCommonLogger {
248+
val logger = RpcInternalCommonLogger.logger(KtorTransportTest::class)
250249

251-
RpcInternalDumpLoggerContainer.set(object : RpcInternalDumpLogger {
252-
253-
override val isEnabled: Boolean = true
254-
255-
override fun dump(vararg tags: String, message: () -> String) {
256-
logger.info { "[${tags.joinToString()}] ${message()}" }
257-
}
258-
})
250+
RpcInternalDumpLoggerContainer.set(logger.dumpLogger())
259251

260252
return logger
261253
}

krpc/krpc-logging/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/logging/RpcInternalDumpLogger.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ public interface RpcInternalDumpLogger {
1313
public fun dump(vararg tags: String, message: () -> String)
1414
}
1515

16+
@InternalRpcApi
17+
public fun RpcInternalCommonLogger.dumpLogger(): RpcInternalDumpLogger {
18+
return object : RpcInternalDumpLogger {
19+
override val isEnabled: Boolean = true
20+
21+
override fun dump(vararg tags: String, message: () -> String) {
22+
this@dumpLogger.info { "${tags.joinToString(" ") { "[$it]" }} ${message()}" }
23+
}
24+
}
25+
}
26+
1627
@InternalRpcApi
1728
public object RpcInternalDumpLoggerContainer {
1829
private var logger: RpcInternalDumpLogger? = null

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
package kotlinx.rpc.krpc.test
66

7+
import kotlinx.coroutines.cancelAndJoin
78
import kotlinx.coroutines.currentCoroutineContext
9+
import kotlinx.coroutines.job
810
import kotlinx.coroutines.test.runTest
911
import kotlinx.coroutines.withContext
1012
import kotlinx.rpc.krpc.rpcClientConfig
@@ -51,9 +53,17 @@ class CoroutineContextPropagationTest {
5153
}
5254
}
5355
}
54-
withContext(CoroutineElement("client")) {
55-
client.withService(Echo::class).echo("request")
56+
try {
57+
withContext(CoroutineElement("client")) {
58+
client.withService(Echo::class).echo("request")
59+
}
60+
assertEquals(CoroutineElement("transport"), actualContext)
61+
} finally {
62+
server.close()
63+
client.close()
64+
server.awaitCompletion()
65+
client.awaitCompletion()
66+
transport.coroutineContext.job.cancelAndJoin()
5667
}
57-
assertEquals(CoroutineElement("transport"), actualContext)
5868
}
5969
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ abstract class ProtocolTestBase {
3939
block: suspend TestBody.() -> Unit,
4040
): TestResult {
4141
return kotlinx.coroutines.test.runTest {
42-
val finished = TestBody(clientConfig, serverConfig, this).apply { block() }
42+
val finished = TestBody(clientConfig, serverConfig, this)
4343

44-
finished.transport.coroutineContext.job.cancelAndJoin()
44+
try {
45+
finished.block()
46+
} finally {
47+
finished.transport.coroutineContext.job.cancelAndJoin()
48+
}
4549
}
4650
}
4751

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ class TransportTest {
105105
}
106106
})
107107

108-
block(logs)
109-
110-
RpcInternalDumpLoggerContainer.set(null)
111-
logsJob.cancelAndJoin()
112-
logsChannel.close()
108+
try {
109+
block(logs)
110+
} finally {
111+
RpcInternalDumpLoggerContainer.set(null)
112+
logsJob.cancelAndJoin()
113+
logsChannel.close()
114+
}
113115
}
114116
}
115117

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.rpc.krpc.KrpcConfigBuilder
1111
import kotlinx.rpc.krpc.internal.logging.RpcInternalCommonLogger
1212
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLogger
1313
import kotlinx.rpc.krpc.internal.logging.RpcInternalDumpLoggerContainer
14+
import kotlinx.rpc.krpc.internal.logging.dumpLogger
1415
import kotlinx.rpc.krpc.rpcClientConfig
1516
import kotlinx.rpc.krpc.rpcServerConfig
1617
import kotlinx.rpc.krpc.serialization.json.json
@@ -25,8 +26,11 @@ import kotlin.time.Duration.Companion.seconds
2526
fun runCancellationTest(body: suspend CancellationToolkit.() -> Unit): TestResult {
2627
return runTest(timeout = 3.seconds) {
2728
debugCoroutines()
28-
CancellationToolkit(this).apply {
29-
body()
29+
val toolkit = CancellationToolkit(this)
30+
try {
31+
body(toolkit)
32+
} finally {
33+
toolkit.close()
3034
}
3135
}
3236
}
@@ -35,13 +39,7 @@ class CancellationToolkit(scope: CoroutineScope) : CoroutineScope by scope {
3539
private val logger = RpcInternalCommonLogger.logger("CancellationTest")
3640

3741
init {
38-
RpcInternalDumpLoggerContainer.set(object : RpcInternalDumpLogger {
39-
override val isEnabled: Boolean = true
40-
41-
override fun dump(vararg tags: String, message: () -> String) {
42-
logger.info { "${tags.joinToString(" ") { "[$it]" }} ${message()}" }
43-
}
44-
})
42+
RpcInternalDumpLoggerContainer.set(logger.dumpLogger())
4543
}
4644

4745
private val configBuilder: KrpcConfigBuilder.() -> Unit = {
@@ -75,4 +73,13 @@ class CancellationToolkit(scope: CoroutineScope) : CoroutineScope by scope {
7573
}
7674
}
7775
}
76+
77+
suspend fun close() {
78+
RpcInternalDumpLoggerContainer.set(null)
79+
client.close()
80+
server.close()
81+
client.awaitCompletion()
82+
server.awaitCompletion()
83+
transport.coroutineContext.job.cancelAndJoin()
84+
}
7885
}

0 commit comments

Comments
 (0)