From ccfae0f26594d9daae5f67f27fbbd1815985ce6e Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 12:42:48 +0100 Subject: [PATCH 1/6] chore(gradle/testclient): fix jar task configuration The jar task was resolving the default configuration in the configuration phase, leading to spurious failure when executing any task in the testclient directly. --- helpers/test-client/build.gradle.kts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helpers/test-client/build.gradle.kts b/helpers/test-client/build.gradle.kts index 3a9311ed1..7d527afbc 100644 --- a/helpers/test-client/build.gradle.kts +++ b/helpers/test-client/build.gradle.kts @@ -28,14 +28,14 @@ tasks { jar { dependsOn(createStartScripts) doFirst { + val libs = arrayListOf("plugins/${project.property("game")}.jar", "software-challenge-server.jar", "server.jar") + libs.addAll(configurations.default.get().map { "lib/" + it.name }) + manifest.attributes["Class-Path"] = libs.joinToString(" ") copy { from("src/logback-tests.xml") into("build/libs") } } - val libs = arrayListOf("plugins/${project.property("game")}.jar", "software-challenge-server.jar", "server.jar") - libs.addAll(configurations.default.get().map { "lib/" + it.name }) - manifest.attributes["Class-Path"] = libs.joinToString(" ") } run.configure { From 4f45c3b312581cde6919ac71ceacb3937ba4fed4 Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 12:54:23 +0100 Subject: [PATCH 2/6] fix(testclient): remove dependency on plugin It was only because of two constants... --- helpers/test-client/build.gradle.kts | 2 +- helpers/test-client/src/sc/TestClient.java | 7 ++++--- server/build.gradle.kts | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/helpers/test-client/build.gradle.kts b/helpers/test-client/build.gradle.kts index 7d527afbc..ce49d2a62 100644 --- a/helpers/test-client/build.gradle.kts +++ b/helpers/test-client/build.gradle.kts @@ -14,7 +14,7 @@ application { } dependencies { - implementation(project(":plugin")) + // TODO this dependency is only for accessing the Configuration, remove it implementation(project(":server")) } diff --git a/helpers/test-client/src/sc/TestClient.java b/helpers/test-client/src/sc/TestClient.java index b596d8585..002a6567f 100644 --- a/helpers/test-client/src/sc/TestClient.java +++ b/helpers/test-client/src/sc/TestClient.java @@ -9,7 +9,6 @@ import sc.networking.INetworkInterface; import sc.networking.TcpNetwork; import sc.networking.clients.XStreamClient; -import sc.plugin2021.util.Constants; import sc.protocol.requests.*; import sc.protocol.responses.*; import sc.server.Configuration; @@ -263,7 +262,8 @@ protected void onObject(ProtocolMessage message) { logger.error("{} crashed, look into {}", player.name, logDir); exit(2); } - if (slept > Constants.GAME_TIMEOUT) { + // TODO move timeout to GamePlugin and obtain it + if (slept > 200.000) { logger.error("The game seems to hang, exiting!"); exit(2); } @@ -363,7 +363,8 @@ private boolean isSignificant() { players: for (int i = 0; i < 2; i++) { double binominalCD = 0.0; - for (int k = 0; k <= players[i].score.getScoreValues().get(0).getValue().intValue() / Constants.WIN_SCORE; k++) { + // TODO use global WIN_SCORE constant instead of hardcoding 2 + for (int k = 0; k <= players[i].score.getScoreValues().get(0).getValue().intValue() / 2; k++) { binominalCD += binominalPD.applyAsDouble(k); if (binominalCD > significance) continue players; diff --git a/server/build.gradle.kts b/server/build.gradle.kts index b15cd4053..d370b2484 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -16,7 +16,7 @@ application { } dependencies { - implementation(project(":sdk")) + api(project(":sdk")) runtimeOnly(project(":plugin")) testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") // legacy java tests From 4a3b9bcfde34a877e783dec95f0702c2909fcdf8 Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 13:08:16 +0100 Subject: [PATCH 3/6] chore(gradle): slight modernisations --- gradle/build.gradle.kts | 58 ++++++++++++---------------- helpers/test-client/build.gradle.kts | 2 +- server/build.gradle.kts | 12 +++--- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/gradle/build.gradle.kts b/gradle/build.gradle.kts index 64818dc69..5e3bbe30e 100644 --- a/gradle/build.gradle.kts +++ b/gradle/build.gradle.kts @@ -1,7 +1,7 @@ +import org.gradle.kotlin.dsl.support.unzipTo import org.jetbrains.dokka.gradle.DokkaTask import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import sc.gradle.ScriptsTask -import java.io.InputStream import java.util.concurrent.atomic.AtomicBoolean plugins { @@ -36,23 +36,6 @@ val documentedProjects = listOf("sdk", "plugin") val enableTestClient by extra { versionObject.minor > 0 } val enableIntegrationTesting = !project.hasProperty("nointegration") && (versionObject.minor > 0 || enableTestClient) -subprojects { - apply(plugin = "java-library") - apply(plugin = "kotlin") - apply(plugin = "com.github.ben-manes.versions") - apply(plugin = "se.patrikerdes.use-latest-versions") - - dependencies { - testImplementation(project(":sdk", "testConfig")) - } - - tasks { - test { - useJUnitPlatform() - } - } -} - val doAfterEvaluate = ArrayList<(Project) -> Unit>() tasks { val startServer by creating { @@ -212,7 +195,7 @@ tasks { testLogDir.mkdirs() val unzipped = tmpDir.resolve("software-challenge-server") unzipped.deleteRecursively() - Runtime.getRuntime().exec("unzip software-challenge-server.zip -d $unzipped", null, deployDir).waitFor() + unzipTo(unzipped, deployDir.resolve("software-challenge-server.zip")) println("Testing TestClient...") val testClient = @@ -251,14 +234,31 @@ tasks { // == Cross-project configuration == -allprojects { - tasks.withType { - kotlinOptions { - jvmTarget = javaTargetVersion.toString() - freeCompilerArgs = listOf("-Xjvm-default=all") - } +subprojects { + apply(plugin = "java-library") + apply(plugin = "kotlin") + apply(plugin = "com.github.ben-manes.versions") + apply(plugin = "se.patrikerdes.use-latest-versions") + + dependencies { + testImplementation(project(":sdk", "testConfig")) } + tasks { + test { + useJUnitPlatform() + } + + withType { + kotlinOptions { + jvmTarget = javaTargetVersion.toString() + freeCompilerArgs = listOf("-Xjvm-default=all") + } + } + } +} + +allprojects { repositories { jcenter() maven("http://dist.wso2.org/maven2") @@ -312,14 +312,6 @@ allprojects { // == Utilities == -fun InputStream.dump(name: String? = null) { - if (name != null) - println("\n$name:") - while (available() > 0) - print(read().toChar()) - close() -} - fun Task.dependOnSubprojects() { if (this.project == rootProject) doAfterEvaluate.add { diff --git a/helpers/test-client/build.gradle.kts b/helpers/test-client/build.gradle.kts index ce49d2a62..62d9d81f1 100644 --- a/helpers/test-client/build.gradle.kts +++ b/helpers/test-client/build.gradle.kts @@ -10,7 +10,7 @@ sourceSets.main { } application { - mainClassName = "sc.TestClient" + mainClass.set("sc.TestClient") } dependencies { diff --git a/server/build.gradle.kts b/server/build.gradle.kts index d370b2484..1a657d4e6 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -4,17 +4,17 @@ plugins { application } -sourceSets { - main.get().java.srcDir("src") - test.get().java.srcDir("test") -} - application { - mainClassName = "sc.server.Application" + mainClass.set("sc.server.Application") applicationDefaultJvmArgs = listOf("-Dfile.encoding=UTF-8", "-XX:+PrintGC", "-XX:+PrintGCDetails", "-XX:+PrintGCDateStamps", "-Xloggc:gc.log") } +sourceSets { + main.get().java.srcDir("src") + test.get().java.srcDir("test") +} + dependencies { api(project(":sdk")) runtimeOnly(project(":plugin")) From 9123c6b302e4b55dacb76538f33429b90ea172ff Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 13:16:59 +0100 Subject: [PATCH 4/6] chore(gradle): resolve cross-project task-dependencies properly --- gradle/build.gradle.kts | 6 +++--- player/build.gradle.kts | 2 +- server/build.gradle.kts | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gradle/build.gradle.kts b/gradle/build.gradle.kts index 5e3bbe30e..3a787f09f 100644 --- a/gradle/build.gradle.kts +++ b/gradle/build.gradle.kts @@ -126,12 +126,12 @@ tasks { val server = ProcessBuilder( "java", - "-Dlogback.configurationFile=${project("server").projectDir.resolve("configuration/logback-trace.xml")}", - "-jar", project("server").tasks.jar.get().archiveFile.get().asFile.absolutePath + "-Dlogback.configurationFile=${project(":server").projectDir.resolve("configuration/logback-trace.xml")}", + "-jar", (project(":server").getTasksByName("jar", false).single() as Jar).archiveFile.get().asFile.absolutePath ) .redirectOutput(testLogDir.resolve("server.log")) .redirectError(testLogDir.resolve("server-err.log")) - .directory(project("server").buildDir.resolve("runnable")) + .directory(project(":server").buildDir.resolve("runnable")) .start() Thread.sleep(400) val startClient: (Int) -> Process = { diff --git a/player/build.gradle.kts b/player/build.gradle.kts index d472a86fa..1c8c96049 100644 --- a/player/build.gradle.kts +++ b/player/build.gradle.kts @@ -50,7 +50,7 @@ tasks { from("src") into("src") }, copySpec { - from(configurations.default, arrayOf("sdk", "plugin").map { project(":$it").tasks.getByName("sourcesJar").outputs.files }) + from(configurations.default, arrayOf("sdk", "plugin").map { project(":$it").getTasksByName("sourcesJar", false).single().outputs.files }) into("lib") }) if(!project.hasProperty("nodoc")) { diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 1a657d4e6..362dada57 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -50,14 +50,14 @@ tasks { val deploy by creating(Zip::class) { group = "distribution" - dependsOn(project(":test-client").tasks.jar, ":player:shadowJar", makeRunnable) + dependsOn(":test-client:jar", ":player:shadowJar", makeRunnable) destinationDirectory.set(deployDir) archiveBaseName.set("software-challenge-server") from(runnableDir) - if(project.property("enableTestClient") as Boolean) - from(project(":test-client").buildDir.resolve("libs")) doFirst { - from(project(":player").tasks["shadowJar"].outputs) + if(project.property("enableTestClient") as Boolean) + from((project(":test-client").getTasksByName("jar", false).single() as Jar).destinationDirectory) + from(project(":player").getTasksByName("shadowJar", false).single().outputs) exec { commandLine("git", "rev-parse", "HEAD") standardOutput = runnableDir.resolve("version").outputStream() From b1678a27e599a04b9c998a740a372cb993ee8985 Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 19:42:42 +0100 Subject: [PATCH 5/6] chore(gradle): improve testTestClient task --- gradle/build.gradle.kts | 20 +++++++++++--------- helpers/test-client/build.gradle.kts | 9 +++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/gradle/build.gradle.kts b/gradle/build.gradle.kts index 3a787f09f..e83d6fa97 100644 --- a/gradle/build.gradle.kts +++ b/gradle/build.gradle.kts @@ -6,7 +6,6 @@ import java.util.concurrent.atomic.AtomicBoolean plugins { maven - `java-library` kotlin("jvm") version "1.4.20" id("org.jetbrains.dokka") version "0.10.1" id("scripts-task") @@ -112,6 +111,7 @@ tasks { dependsOn(deploy) } + // TODO create a global constant which can be shared with testclient & co - maybe a resource? val maxGameLength = 150L val clearTestLogs by creating(Delete::class) { @@ -196,18 +196,20 @@ tasks { val unzipped = tmpDir.resolve("software-challenge-server") unzipped.deleteRecursively() unzipTo(unzipped, deployDir.resolve("software-challenge-server.zip")) - + println("Testing TestClient...") val testClient = - ProcessBuilder( - project("test-client").tasks.getByName("createStartScripts").content.split(" ") - + listOf("--start-server", "--tests", testClientGames.toString(), "--port", "13055") - ) - .redirectOutput(testLogDir.resolve("test-client.log")) - .redirectError(testLogDir.resolve("test-client-err.log")) - .directory(unzipped).start() + ProcessBuilder( + (project(":test-client").getTasksByName("createStartScripts", false).single() as ScriptsTask).content.split(' ') + + arrayOf("--start-server", "--tests", testClientGames.toString(), "--port", "13055") + ) + .redirectOutput(testLogDir.resolve("test-client.log")) + .redirectError(testLogDir.resolve("test-client-err.log")) + .directory(unzipped) + .start() if (testClient.waitFor(maxGameLength * testClientGames, TimeUnit.SECONDS)) { val value = testClient.exitValue() + // TODO check whether TestClient actually played games if (value == 0) println("TestClient successfully tested!") else diff --git a/helpers/test-client/build.gradle.kts b/helpers/test-client/build.gradle.kts index 62d9d81f1..624ce9648 100644 --- a/helpers/test-client/build.gradle.kts +++ b/helpers/test-client/build.gradle.kts @@ -28,12 +28,13 @@ tasks { jar { dependsOn(createStartScripts) doFirst { - val libs = arrayListOf("plugins/${project.property("game")}.jar", "software-challenge-server.jar", "server.jar") - libs.addAll(configurations.default.get().map { "lib/" + it.name }) - manifest.attributes["Class-Path"] = libs.joinToString(" ") + manifest.attributes["Class-Path"] = + configurations.default.get().map { "lib/" + it.name } + .plus("server.jar") + .joinToString(" ") copy { from("src/logback-tests.xml") - into("build/libs") + into(destinationDirectory) } } } From 340b436849a9dc9e52cbd07553680042a3388e9c Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Wed, 20 Jan 2021 19:46:59 +0100 Subject: [PATCH 6/6] TEMP chore(gradle): remove custom startScripts task Current issues: - the start-script hardcodes classpath to lib: https://github.com/gradle/gradle/issues/7033 - script assumes it is in a bin directory, but I want it in root ("pwd -P") https://docs.gradle.org/current/dsl/org.gradle.jvm.application.tasks.CreateStartScripts.html --- gradle/build.gradle.kts | 11 +++++------ helpers/test-client/build.gradle.kts | 8 ++++---- server/build.gradle.kts | 19 ++++++++++--------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/gradle/build.gradle.kts b/gradle/build.gradle.kts index e83d6fa97..80a05373f 100644 --- a/gradle/build.gradle.kts +++ b/gradle/build.gradle.kts @@ -1,14 +1,12 @@ import org.gradle.kotlin.dsl.support.unzipTo import org.jetbrains.dokka.gradle.DokkaTask import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -import sc.gradle.ScriptsTask import java.util.concurrent.atomic.AtomicBoolean plugins { maven kotlin("jvm") version "1.4.20" id("org.jetbrains.dokka") version "0.10.1" - id("scripts-task") id("com.github.ben-manes.versions") version "0.36.0" id("se.patrikerdes.use-latest-versions") version "0.2.15" @@ -198,11 +196,12 @@ tasks { unzipTo(unzipped, deployDir.resolve("software-challenge-server.zip")) println("Testing TestClient...") + val command = mutableListOf("java").apply { + addAll((project(":test-client").getTasksByName("createStartScripts", false).single() as CreateStartScripts).defaultJvmOpts!!) + addAll(arrayOf("--start-server", "--tests", testClientGames.toString(), "--port", "13055")) + } val testClient = - ProcessBuilder( - (project(":test-client").getTasksByName("createStartScripts", false).single() as ScriptsTask).content.split(' ') + - arrayOf("--start-server", "--tests", testClientGames.toString(), "--port", "13055") - ) + ProcessBuilder(command) .redirectOutput(testLogDir.resolve("test-client.log")) .redirectError(testLogDir.resolve("test-client-err.log")) .directory(unzipped) diff --git a/helpers/test-client/build.gradle.kts b/helpers/test-client/build.gradle.kts index 624ce9648..9717faaf2 100644 --- a/helpers/test-client/build.gradle.kts +++ b/helpers/test-client/build.gradle.kts @@ -19,10 +19,10 @@ dependencies { } tasks { - val createStartScripts by creating(ScriptsTask::class) { - destinationDir = file("build/libs") - fileName = "start-tests" - content = "java -Dfile.encoding=UTF-8 -Dlogback.configurationFile=logback-tests.xml -jar test-client.jar" + val createStartScripts by creating(CreateStartScripts::class) { + outputDir = jar.get().destinationDirectory.asFile.get() + applicationName = "start-tests" + defaultJvmOpts = listOf("-Dfile.encoding=UTF-8", "-Dlogback.configurationFile=logback-tests.xml") } jar { diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 362dada57..5b71eee7b 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -1,7 +1,7 @@ -import sc.gradle.ScriptsTask - plugins { application + // TODO https://github.com/CAU-Kiel-Tech-Inf/backend/issues/265 + distribution } application { @@ -27,10 +27,10 @@ val deployDir: File by project tasks { val runnableDir = buildDir.resolve("runnable") - val createStartScripts by creating(ScriptsTask::class) { - destinationDir = runnableDir - fileName = "start" - content = "java -Dfile.encoding=UTF-8 -Dlogback.configurationFile=logback.xml -jar server.jar" + startScripts { + outputDir = runnableDir + applicationName = "start-server" + defaultJvmOpts = listOf("-Dfile.encoding=UTF-8", "-Dlogback.configurationFile=logback.xml") } val copyConfig by creating(Copy::class) { @@ -43,7 +43,7 @@ tasks { val makeRunnable by creating(Copy::class) { group = "distribution" - dependsOn(jar, copyConfig, createStartScripts) + dependsOn(jar, copyConfig, startScripts) from(configurations.default) into(runnableDir.resolve("lib")) } @@ -91,9 +91,10 @@ tasks { } jar { - destinationDirectory.set(runnableDir) + destinationDirectory.set(runnableDir.resolve("lib")) doFirst { - manifest.attributes["Class-Path"] = configurations.default.get().joinToString(" ") { "lib/" + it.name } + manifest.attributes["Class-Path"] = + configurations.default.get().joinToString(" ") { "lib/" + it.name } } }