-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP test server as shared build service
- Loading branch information
1 parent
7924cbb
commit d5ec623
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject | ||
|
||
plugins { | ||
|
||
} | ||
|
||
abstract class WSTestServerService : BuildService<BuildServiceParameters.None>, AutoCloseable { | ||
|
||
val host: String | ||
val webPort: Int | ||
val wsPort: Int | ||
|
||
init { | ||
host = "localhost" // TODO | ||
webPort = 8080 // TODO | ||
wsPort = 9001 // TODO | ||
} | ||
|
||
override fun close() { | ||
|
||
} | ||
} | ||
|
||
val wsTestServer = gradle.sharedServices.registerIfAbsent("wsTestServer", WSTestServerService::class) { | ||
// maxParallelUsages.set(1) // the test server doesn't support parallel tests | ||
} | ||
|
||
// ensure autobahn test server is launched for websocket tests | ||
tasks.withType<AbstractTestTask> { | ||
usesService(wsTestServer) | ||
} | ||
|
||
// provide autobahn test server coordinates to the tests (can vary if DOCKER_HOST is set - like on CI macOS) | ||
tasks.withType<org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest> { | ||
// autobahn doesn't support parallel tests (/getCaseStatus fails with immediate Close frame) | ||
// https://github.com/crossbario/autobahn-testsuite/issues/119 | ||
maxParallelForks = 1 | ||
|
||
doFirst { | ||
val autobahnContainer = getAutobahnTestServerContainerInfo() | ||
environment("WS_TEST_SERVER_HOST", autobahnContainer.host) | ||
environment("WS_TEST_SERVER_TCP_8080", autobahnContainer.webPort) | ||
environment("WS_TEST_SERVER_TCP_9001", autobahnContainer.wsPort) | ||
} | ||
} | ||
|
||
tasks.withType<org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest> { | ||
doFirst { | ||
val autobahnContainer = getAutobahnTestServerContainerInfo() | ||
environment("WS_TEST_SERVER_HOST", autobahnContainer.host) | ||
environment("WS_TEST_SERVER_TCP_8080", autobahnContainer.webPort) | ||
environment("WS_TEST_SERVER_TCP_9001", autobahnContainer.wsPort) | ||
} | ||
} | ||
|
||
tasks.withType<org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest> { | ||
doFirst { | ||
val autobahnContainer = getAutobahnTestServerContainerInfo() | ||
// SIMCTL_CHILD_ prefix to pass those variables from test process to the iOS/tvOS/watchOS emulators | ||
environment("SIMCTL_CHILD_WS_TEST_SERVER_HOST", autobahnContainer.host) | ||
environment("SIMCTL_CHILD_WS_TEST_SERVER_TCP_8080", autobahnContainer.webPort) | ||
environment("SIMCTL_CHILD_WS_TEST_SERVER_TCP_9001", autobahnContainer.wsPort) | ||
} | ||
} | ||
|
||
//private val jsTestTasks = tasks.withType<org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest>().map { it } | ||
// | ||
//jsTestTasks.forEach { testTask -> | ||
tasks.withType<org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest> { | ||
val testTask = this | ||
val npmProjectDir = testTask.compilation.npmProject.dir | ||
val autobahnServerConfigFile = npmProjectDir.resolve("autobahn-server.json") | ||
val taskPrefix = npmProjectDir.name.removePrefix("krossbow-autobahn-tests").toCamelCase() | ||
val generateConfigTaskName = "${taskPrefix}_autobahnConfig" | ||
|
||
// There is a browser test task AND a node test task for each source set, and they use the same NPM project dir. | ||
// There is a legacy AND an IR test task for each source set, but they DON'T use the same NPM project dir. | ||
// We only need to create the task once per generated NPM project, but we need all test tasks using this project to | ||
// depend on the config generation task. | ||
@Suppress("UnstableApiUsage") | ||
val generateAutobahnConfigJson = tasks.findByName(generateConfigTaskName) ?: tasks.create(generateConfigTaskName) { | ||
group = "autobahn config js" | ||
doNotTrackState("this task cannot be cached because it depends on the autobahn test server's runtime state") | ||
usesService(wsTestServer) | ||
outputs.file(autobahnServerConfigFile) | ||
doFirst { | ||
val autobahnContainer = getAutobahnTestServerContainerInfo() | ||
file(autobahnServerConfigFile).writeText( | ||
"""{ | ||
"host":"${autobahnContainer.host}", | ||
"webPort":${autobahnContainer.webPort}, | ||
"wsPort":${autobahnContainer.wsPort} | ||
}""".trimMargin() | ||
) | ||
} | ||
} | ||
|
||
testTask.dependsOn(generateAutobahnConfigJson) | ||
} | ||
|
||
fun getAutobahnTestServerContainerInfo(): ServiceInfo = TODO() | ||
|
||
data class ServiceInfo( | ||
val host: String, | ||
val webPort: Int, | ||
val wsPort: Int, | ||
) | ||
|
||
fun String.toCamelCase() = replace(Regex("""-(\w)""")) { match -> match.groupValues[1].toUpperCase() } |