From c4d9e5e9467130de09b7810097c0b3779219ffe0 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 17 May 2020 18:22:23 +0200 Subject: Fix SW client "request" --- build.gradle.kts | 8 ++++++++ .../org/luxons/sevenwonders/client/SevenWondersClient.kt | 13 ++++++++++--- .../org/luxons/sevenwonders/server/SevenWondersTest.kt | 14 ++++---------- .../org/luxons/sevenwonders/server/test/TestUtils.kt | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index e44a8519..162aa2dc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,6 +23,14 @@ subprojects { kotlinOptions.freeCompilerArgs = compilerArgs } + tasks.withType { + testLogging { + events("failed", "standardError") + exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL + showStackTraces = true + } + } + afterEvaluate { // The import ordering expected by ktlint is alphabetical, which doesn't match IDEA's formatter. // Since it is not configurable, we have to disable the rule. diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt index e13ab505..3dca33c1 100644 --- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt +++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt @@ -1,5 +1,9 @@ package org.luxons.sevenwonders.client +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.serialization.DeserializationStrategy @@ -38,16 +42,19 @@ class SevenWondersClient { } } +@OptIn(ExperimentalCoroutinesApi::class) private suspend inline fun StompSessionWithKxSerialization.request( sendDestination: String, receiveDestination: String, payload: T? = null, serializer: SerializationStrategy, deserializer: DeserializationStrategy -): U { - val sub = subscribe(receiveDestination, deserializer) +): U = coroutineScope { + val sub = async(start = CoroutineStart.UNDISPATCHED) { + subscribe(receiveDestination, deserializer).first() + } convertAndSend(sendDestination, payload, serializer) - return sub.first() + sub.await() } class SevenWondersSession(private val stompSession: StompSessionWithKxSerialization) { diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt index b2b86131..90fd7df1 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/SevenWondersTest.kt @@ -1,10 +1,7 @@ package org.luxons.sevenwonders.server -import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.* import kotlinx.coroutines.flow.produceIn -import kotlinx.coroutines.launch -import kotlinx.coroutines.withTimeout -import kotlinx.coroutines.withTimeoutOrNull import org.junit.runner.RunWith import org.luxons.sevenwonders.client.SevenWondersClient import org.luxons.sevenwonders.client.SevenWondersSession @@ -13,11 +10,9 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.SpringBootTest.WebEnvironment import org.springframework.boot.web.server.LocalServerPort import org.springframework.test.context.junit4.SpringRunner -import kotlin.test.assertEquals -import kotlin.test.assertNotNull -import kotlin.test.assertNull -import kotlin.test.Test +import kotlin.test.* +@OptIn(FlowPreview::class) @RunWith(SpringRunner::class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class SevenWondersTest { @@ -83,7 +78,6 @@ class SevenWondersTest { disconnect(ownerSession) } - @OptIn(FlowPreview::class) @Test fun createGame_seenByConnectedPlayers() = runAsyncTest { val otherSession = newPlayer("OtherPlayer") @@ -107,7 +101,6 @@ class SevenWondersTest { disconnect(ownerSession, otherSession) } - @OptIn(FlowPreview::class) @Test fun startGame_3players() = runAsyncTest { val session1 = newPlayer("Player1") @@ -123,6 +116,7 @@ class SevenWondersTest { launch { session.awaitGameStart(lobby.id) val turns = session.watchTurns().produceIn(this) + delay(100) // ensures the subscription happened session.sayReady() val turn = turns.receive() assertNotNull(turn) diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/test/TestUtils.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/test/TestUtils.kt index 5f6c5be9..7a962368 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/test/TestUtils.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/test/TestUtils.kt @@ -13,7 +13,7 @@ fun mockSimpMessagingTemplate(): SimpMessagingTemplate = SimpMessagingTemplate(o override fun send(message: Message<*>, timeout: Long): Boolean = true }) -fun runAsyncTest(timeoutMillis: Long = 1000, block: suspend CoroutineScope.() -> Unit) = runBlocking { +fun runAsyncTest(timeoutMillis: Long = 3000, block: suspend CoroutineScope.() -> Unit) = runBlocking { val result = withTimeoutOrNull(timeoutMillis, block) assertNotNull(result, "Test timed out, exceeded ${timeoutMillis}ms") } -- cgit