Skip to content

Commit

Permalink
WIP test server as shared build service
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Aug 15, 2023
1 parent 7924cbb commit d5ec623
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions buildSrc/src/main/kotlin/krossbow-ws-tests.gradle.kts
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() }

0 comments on commit d5ec623

Please sign in to comment.