Skip to content

Commit fa2a9b0

Browse files
authored
Tests improvements (#102)
* Rewrite tests to use suspend before and after tests * use mocha for nodejs tests * fix several issues for nodejs IR regarding cancelling * minor: payload creation from ByteArrays * minor: change a little JS logger to not print null all time where exception is null
1 parent e8de0d9 commit fa2a9b0

File tree

39 files changed

+419
-388
lines changed

39 files changed

+419
-388
lines changed

benchmarks/src/kotlinMain/kotlin/io/rsocket/kotlin/benchmarks/RSocketKotlinBenchmark.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class RSocketKotlinBenchmark : RSocketBenchmark<Payload>() {
7070
}
7171
}
7272

73-
override fun createPayload(size: Int): Payload = if (size == 0) Payload.Empty else Payload(
74-
ByteArray(size / 2).also { Random.nextBytes(it) },
75-
ByteArray(size / 2).also { Random.nextBytes(it) }
73+
override fun createPayload(size: Int): Payload = if (size == 0) Payload.Empty else Payload.wrap(
74+
data = ByteArray(size / 2).also { Random.nextBytes(it) },
75+
metadata = ByteArray(size / 2).also { Random.nextBytes(it) }
7676
)
7777

7878
override fun releasePayload(payload: Payload) {

build.gradle.kts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.jfrog.bintray.gradle.*
1818
import com.jfrog.bintray.gradle.tasks.*
1919
import org.gradle.api.publish.maven.internal.artifact.*
2020
import org.jetbrains.kotlin.gradle.dsl.*
21-
import org.jetbrains.kotlin.gradle.plugin.*
2221
import org.jetbrains.kotlin.gradle.targets.js.*
2322
import org.jetbrains.kotlin.gradle.targets.jvm.*
2423
import org.jfrog.gradle.plugin.artifactory.dsl.*
@@ -62,8 +61,21 @@ subprojects {
6261
is KotlinJsTarget -> {
6362
useCommonJs()
6463
//configure running tests for JS
65-
nodejs { testTask { useKarma { useChromeHeadless() } } }
66-
browser { testTask { useKarma { useChromeHeadless() } } }
64+
nodejs {
65+
testTask {
66+
useMocha {
67+
timeout = "600s"
68+
}
69+
}
70+
}
71+
browser {
72+
testTask {
73+
useKarma {
74+
useConfigDirectory(rootDir.resolve("js").resolve("karma.config.d"))
75+
useChromeHeadless()
76+
}
77+
}
78+
}
6779
}
6880
is KotlinJvmTarget -> {
6981
compilations.all {
@@ -81,7 +93,7 @@ subprojects {
8193

8294
useExperimentalAnnotation("kotlin.RequiresOptIn")
8395

84-
if (name.contains("test", ignoreCase = true)) {
96+
if (name.contains("test", ignoreCase = true) || project.name == "rsocket-test") {
8597
useExperimentalAnnotation("kotlin.time.ExperimentalTime")
8698
useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
8799
useExperimentalAnnotation("kotlinx.coroutines.ExperimentalCoroutinesApi")
@@ -94,20 +106,10 @@ subprojects {
94106
}
95107
}
96108

97-
98-
//should be not needed after ~1.4.20 when kotlin-test will be added automatically
99-
val commonTest by sourceSets.getting {
100-
dependencies {
101-
implementation(kotlin("test-common"))
102-
implementation(kotlin("test-annotations-common"))
103-
}
104-
}
105-
targets.all {
106-
compilations.findByName("test")?.dependencies {
107-
when (platformType) {
108-
KotlinPlatformType.jvm -> implementation(kotlin("test-junit"))
109-
KotlinPlatformType.js -> implementation(kotlin("test-js"))
110-
else -> Unit
109+
if (project.name != "rsocket-test") {
110+
val commonTest by sourceSets.getting {
111+
dependencies {
112+
implementation(project(":rsocket-test"))
111113
}
112114
}
113115
}

js/karma.config.d/karma.conf.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
config.client = config.client || {}
2+
config.client.mocha = config.client.mocha || {}
3+
config.client.mocha.timeout = '600s'
4+
config.browserNoActivityTimeout = 600000
5+
config.browserDisconnectTimeout = 600000

playground/src/jvmMain/kotlin/tcp/TCPClient.kt renamed to playground/src/commonMain/kotlin/TCP.kt

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,42 @@
1414
* limitations under the License.
1515
*/
1616

17-
package tcp
18-
19-
import doSomething
2017
import io.ktor.network.selector.*
2118
import io.ktor.network.sockets.*
2219
import io.ktor.util.*
2320
import io.rsocket.kotlin.connection.*
21+
import io.rsocket.kotlin.core.*
2422
import io.rsocket.kotlin.payload.*
25-
import kotlinx.coroutines.*
26-
import java.util.concurrent.*
27-
28-
@OptIn(KtorExperimentalAPI::class)
29-
fun main(): Unit = runBlocking {
30-
val dispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher()
31-
val socket = aSocket(ActorSelectorManager(dispatcher)).tcp().connect("127.0.0.1", 2323)
32-
33-
val client = socket.connection.connectClient()
34-
try {
35-
client.doSomething()
36-
} catch (e: Throwable) {
37-
dispatcher.close()
38-
throw e
39-
}
23+
import kotlin.coroutines.*
24+
25+
@OptIn(KtorExperimentalAPI::class, InternalAPI::class)
26+
suspend fun runTcpClient(dispatcher: CoroutineContext): Unit {
27+
aSocket(SelectorManager(dispatcher))
28+
.tcp()
29+
.connect("127.0.0.1", 2323)
30+
.connection
31+
.connectClient()
32+
.doSomething()
4033
}
4134

35+
@OptIn(KtorExperimentalAPI::class, InternalAPI::class)
36+
suspend fun runTcpServer(dispatcher: CoroutineContext): Unit {
37+
aSocket(SelectorManager(dispatcher))
38+
.tcp()
39+
.bind("127.0.0.1", 2323)
40+
.rSocket(acceptor = rSocketAcceptor)
41+
}
4242

4343
//to test nodejs tcp server
44-
@OptIn(KtorExperimentalAPI::class)
45-
fun main2(): Unit = runBlocking {
46-
val socket = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect("127.0.0.1", 9000)
47-
48-
val client = socket.connection.connectClient()
44+
@OptIn(KtorExperimentalAPI::class, InternalAPI::class)
45+
suspend fun testNodeJsServer(dispatcher: CoroutineContext) {
46+
val client =
47+
aSocket(SelectorManager(dispatcher))
48+
.tcp()
49+
.connect("127.0.0.1", 9000)
50+
.connection
51+
.connectClient()
4952

5053
val response = client.requestResponse(Payload("Hello from JVM"))
51-
5254
println(response.data.readText())
5355
}

playground/src/commonMain/kotlin/ws/WSClient.common.kt renamed to playground/src/commonMain/kotlin/WS.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
package ws
18-
19-
import doSomething
2017
import io.ktor.client.*
2118
import io.ktor.client.engine.*
2219
import io.ktor.client.features.websocket.*
2320
import io.ktor.util.*
2421
import io.rsocket.kotlin.core.*
2522

26-
expect val engine: HttpClientEngineFactory<*>
27-
2823
@OptIn(KtorExperimentalAPI::class)
29-
suspend fun run() {
24+
suspend fun runWSClient(engine: HttpClientEngineFactory<*>) {
3025
val client = HttpClient(engine) {
3126
install(WebSockets)
3227
install(RSocketClientSupport)

playground/src/jsMain/kotlin/ws/WSClient.kt renamed to playground/src/jsMain/kotlin/WSApp.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
package ws
18-
19-
import io.ktor.client.engine.*
2017
import io.ktor.client.engine.js.*
2118

22-
actual val engine: HttpClientEngineFactory<*> = Js
23-
24-
suspend fun main() = run()
19+
suspend fun main(): Unit = runWSClient(Js)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2015-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import kotlinx.coroutines.*
18+
19+
suspend fun main(): Unit = runTcpClient(Dispatchers.IO)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2015-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import kotlinx.coroutines.*
18+
19+
suspend fun main(): Unit = runTcpServer(Dispatchers.IO)

playground/src/jvmMain/kotlin/ws/WSClient.kt renamed to playground/src/jvmMain/kotlin/WSClientApp.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
package ws
18-
19-
import io.ktor.client.engine.*
2017
import io.ktor.client.engine.cio.*
21-
import io.ktor.util.*
22-
23-
@OptIn(KtorExperimentalAPI::class)
24-
actual val engine: HttpClientEngineFactory<*> = CIO
25-
26-
suspend fun main() = run()
2718

19+
suspend fun main(): Unit = runWSClient(CIO)

0 commit comments

Comments
 (0)