Skip to content

Commit 0a87991

Browse files
author
olme04
authored
improves client side tests (#179)
test JS WS Client transport update pipelines to run ktor JS tests Co-authored-by: olme04 <olme04>
1 parent d83b2c7 commit 0a87991

File tree

11 files changed

+219
-12
lines changed

11 files changed

+219
-12
lines changed

.github/workflows/gradle-main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
dependencies-cache-enabled: false
7979
configuration-cache-enabled: false
8080
- name: Test rsocket-transport-ktor module
81-
if: matrix.target != 'mingwX64' && matrix.target != 'jsIrNode' && matrix.target != 'jsIrBrowser' && matrix.target != 'jsLegacyNode' && matrix.target != 'jsLegacyBrowser' && (success() || failure())
81+
if: matrix.target != 'mingwX64' && (success() || failure())
8282
timeout-minutes: 10
8383
uses: gradle/gradle-build-action@v1
8484
with:

.github/workflows/gradle-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
dependencies-cache-enabled: false
7474
configuration-cache-enabled: false
7575
- name: Test rsocket-transport-ktor module
76-
if: matrix.target != 'mingwX64' && matrix.target != 'jsIrNode' && matrix.target != 'jsIrBrowser' && matrix.target != 'jsLegacyNode' && matrix.target != 'jsLegacyBrowser' && (success() || failure())
76+
if: matrix.target != 'mingwX64' && (success() || failure())
7777
timeout-minutes: 10
7878
uses: gradle/gradle-build-action@v1
7979
with:

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
dependencies-cache-enabled: false
7373
configuration-cache-enabled: false
7474
- name: Test rsocket-transport-ktor module
75-
if: matrix.target != 'mingwX64' && matrix.target != 'jsIrNode' && matrix.target != 'jsIrBrowser' && matrix.target != 'jsLegacyNode' && matrix.target != 'jsLegacyBrowser' && (success() || failure())
75+
if: matrix.target != 'mingwX64' && (success() || failure())
7676
timeout-minutes: 10
7777
uses: gradle/gradle-build-action@v1
7878
with:

build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ subprojects {
6363
//targets configuration
6464
extensions.configure<KotlinMultiplatformExtension> {
6565
val isAutoConfigurable = project.name.startsWith("rsocket") //manual config of others
66-
val jvmOnly = project.name == "rsocket-transport-ktor-server" //server is jvm only
67-
66+
val jvmOnly =
67+
project.name == "rsocket-transport-ktor-server" || //server is jvm only
68+
project.name == "rsocket-test-server"
6869
//windows target isn't supported by ktor-network
6970
val supportMingw = project.name != "rsocket-transport-ktor" && project.name != "rsocket-transport-ktor-client"
7071

@@ -140,7 +141,7 @@ subprojects {
140141

141142
//common configuration
142143
extensions.configure<KotlinMultiplatformExtension> {
143-
val isTestProject = project.name == "rsocket-test"
144+
val isTestProject = project.name == "rsocket-test" || project.name == "rsocket-test-server"
144145
val isLibProject = project.name.startsWith("rsocket")
145146
val isPlaygroundProject = project.name == "playground"
146147
val isExampleProject = "examples" in project.path

examples/multiplatform-chat/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ kotlin {
8080
val clientJsMain by getting {
8181
dependsOn(clientMain)
8282
dependencies {
83-
implementation("io.ktor:ktor-client-js:$ktorVersion")
83+
implementation("io.ktor:ktor-client-core:$ktorVersion")
8484
}
8585
}
8686

playground/build.gradle.kts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ kotlin {
4646
implementation(project(":rsocket-core"))
4747
implementation(project(":rsocket-transport-local"))
4848
implementation(project(":rsocket-transport-ktor-client"))
49+
50+
implementation("io.ktor:ktor-client-core:$ktorVersion") //for WS support
4951
}
5052
}
5153
val jvmMain by getting {
@@ -56,10 +58,5 @@ kotlin {
5658
implementation("io.ktor:ktor-server-cio:$ktorVersion")
5759
}
5860
}
59-
val jsMain by getting {
60-
dependencies {
61-
implementation("io.ktor:ktor-client-js:$ktorVersion") //for WS support
62-
}
63-
}
6461
}
6562
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 org.jetbrains.kotlin.gradle.plugin.mpp.*
18+
import java.io.*
19+
import java.net.*
20+
21+
plugins {
22+
kotlin("multiplatform")
23+
}
24+
25+
val ktorVersion: String by rootProject
26+
27+
kotlin {
28+
sourceSets {
29+
val jvmMain by getting {
30+
dependencies {
31+
implementation(project(":rsocket-test"))
32+
implementation(project(":rsocket-transport-ktor-server"))
33+
34+
implementation("io.ktor:ktor-server-cio:$ktorVersion")
35+
}
36+
}
37+
}
38+
}
39+
40+
open class RSocketTestServer : DefaultTask() {
41+
@Internal
42+
var server: Closeable? = null
43+
private set
44+
45+
@Internal
46+
lateinit var classpath: FileCollection
47+
48+
@TaskAction
49+
fun exec() {
50+
try {
51+
println("[TestServer] start")
52+
val loader = URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), ClassLoader.getSystemClassLoader())
53+
server = loader.loadClass("io.rsocket.kotlin.test.server.AppKt").getMethod("start").invoke(null) as Closeable
54+
println("[TestServer] started")
55+
} catch (cause: Throwable) {
56+
println("[TestServer] failed: ${cause.message}")
57+
cause.printStackTrace()
58+
}
59+
}
60+
}
61+
62+
val startTestServer by tasks.registering(RSocketTestServer::class) {
63+
dependsOn(tasks["jvmJar"])
64+
classpath = (kotlin.targets["jvm"].compilations["test"] as KotlinJvmCompilation).runtimeDependencyFiles
65+
}
66+
67+
val testTasks = setOf(
68+
"jsLegacyNodeTest",
69+
"jsIrNodeTest",
70+
"jsLegacyBrowserTest",
71+
"jsIrBrowserTest",
72+
)
73+
74+
rootProject.allprojects {
75+
if (name == "rsocket-transport-ktor") {
76+
tasks.matching { it.name in testTasks }.all {
77+
dependsOn(startTestServer)
78+
}
79+
}
80+
}
81+
82+
gradle.buildFinished {
83+
startTestServer.get().server?.run {
84+
close()
85+
println("[TestServer] stop")
86+
}
87+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
package io.rsocket.kotlin.test.server
18+
19+
import io.ktor.application.*
20+
import io.ktor.routing.*
21+
import io.ktor.server.cio.*
22+
import io.ktor.server.engine.*
23+
import io.ktor.websocket.*
24+
import io.rsocket.kotlin.core.*
25+
import io.rsocket.kotlin.test.*
26+
import io.rsocket.kotlin.transport.ktor.*
27+
import io.rsocket.kotlin.transport.ktor.server.*
28+
import kotlinx.coroutines.*
29+
import java.io.*
30+
31+
fun main() {
32+
start().await()
33+
}
34+
35+
fun start(): TestServer {
36+
val server = TestServer()
37+
server.start()
38+
return server
39+
}
40+
41+
class TestServer : Closeable {
42+
private val job = Job()
43+
private var wsServer: ApplicationEngine? = null
44+
private val rSocketServer = RSocketServer {
45+
// loggerFactory = PrintLogger.withLevel(LoggingLevel.DEBUG)
46+
}
47+
48+
fun start(): Unit = runCatching {
49+
val scope = CoroutineScope(job)
50+
51+
//start TCP server
52+
rSocketServer.bindIn(scope, TcpServerTransport(port = 8000)) { TestRSocket() }
53+
54+
//start WS server
55+
wsServer = scope.embeddedServer(CIO, port = 9000) {
56+
install(WebSockets)
57+
install(RSocketSupport) { server = rSocketServer }
58+
59+
routing {
60+
rSocket { TestRSocket() }
61+
}
62+
}.start()
63+
64+
Thread.sleep(1000) //await start
65+
}.onFailure { close() }.getOrThrow()
66+
67+
fun await() {
68+
runBlocking { job.join() }
69+
}
70+
71+
override fun close() {
72+
runBlocking { job.cancelAndJoin() }
73+
wsServer?.stop(0, 1000)
74+
}
75+
}

rsocket-transport-ktor/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ kotlin {
5858
}
5959

6060
description = "Ktor RSocket transport implementations (TCP, Websocket)"
61+
62+
evaluationDependsOn(":rsocket-test-server")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
package io.rsocket.kotlin.transport.ktor
18+
19+
import io.ktor.client.*
20+
import io.ktor.client.engine.js.*
21+
import io.ktor.client.features.websocket.*
22+
import io.rsocket.kotlin.test.*
23+
import io.rsocket.kotlin.transport.ktor.client.*
24+
import kotlinx.coroutines.*
25+
26+
class ClientWebSocketTransportTest : TransportTest() {
27+
28+
private val httpClient = HttpClient(Js) {
29+
install(WebSockets)
30+
install(RSocketSupport) { connector = CONNECTOR }
31+
}
32+
33+
override suspend fun before() {
34+
client = httpClient.rSocket(port = 9000)
35+
}
36+
37+
override suspend fun after() {
38+
super.after()
39+
httpClient.close()
40+
httpClient.coroutineContext.job.cancelAndJoin()
41+
}
42+
43+
}

0 commit comments

Comments
 (0)