Skip to content

Commit f070f4e

Browse files
authored
Merge pull request #174 from ktorio/vgoncharova/KTOR-6033
Updated GraalVM Example to run tests on GraalVM with native image build
2 parents f4560b8 + 69fcea4 commit f070f4e

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

graalvm/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ A demo project that shows how to combine Ktor server applications with [GraalVM]
1515
3. The previous step produces an executable file named `graal-server` which can then be run. Open up
1616
`http://0.0.0.0:8080` to test the server.
1717

18+
4. Run the command `./gradlew nativeTestCompile` (or `gradlew nativeTestCompile` on Windows) to build an executable file for tests.
19+
20+
5. That step produces an executable file named `graal-test-server` which can then be run.
21+
1822
### Current limitations
1923

2024
Using the `Netty` engine is not compatible with GraalVM. Please following

graalvm/build.gradle.kts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
application
33
kotlin("jvm") version "1.7.20"
44
id("io.ktor.plugin") version "2.3.2"
5-
id("org.graalvm.buildtools.native") version "0.9.11"
5+
id("org.graalvm.buildtools.native") version "0.9.19"
66
}
77

88
group = "io.ktor"
@@ -19,21 +19,54 @@ dependencies {
1919
implementation("ch.qos.logback:logback-classic:1.4.6")
2020
implementation("io.ktor:ktor-server-core-jvm")
2121
implementation("io.ktor:ktor-server-cio-jvm")
22+
23+
testImplementation("io.ktor:ktor-server-test-host-jvm")
24+
testImplementation("org.jetbrains.kotlin:kotlin-test")
2225
}
2326

2427
graalvmNative {
2528
binaries {
29+
2630
named("main") {
2731
fallback.set(false)
2832
verbose.set(true)
2933

34+
buildArgs.add("--initialize-at-build-time=ch.qos.logback")
35+
buildArgs.add("--initialize-at-build-time=io.ktor,kotlin")
36+
buildArgs.add("--initialize-at-build-time=org.slf4j.LoggerFactory")
37+
38+
buildArgs.add("-H:+InstallExitHandlers")
39+
buildArgs.add("-H:+ReportUnsupportedElementsAtRuntime")
40+
buildArgs.add("-H:+ReportExceptionStackTraces")
41+
42+
imageName.set("graalvm-server")
43+
}
44+
45+
named("test"){
46+
fallback.set(false)
47+
verbose.set(true)
48+
49+
buildArgs.add("--initialize-at-build-time=ch.qos.logback")
3050
buildArgs.add("--initialize-at-build-time=io.ktor,kotlin")
51+
buildArgs.add("--initialize-at-build-time=org.slf4j.LoggerFactory")
3152

3253
buildArgs.add("-H:+InstallExitHandlers")
3354
buildArgs.add("-H:+ReportUnsupportedElementsAtRuntime")
3455
buildArgs.add("-H:+ReportExceptionStackTraces")
3556

36-
imageName.set("graal-server")
57+
val path = "${projectDir}/src/test/resources/META-INF/native-image/"
58+
buildArgs.add("-H:ReflectionConfigurationFiles=${path}reflect-config.json")
59+
buildArgs.add("-H:ResourceConfigurationFiles=${path}resource-config.json")
60+
61+
imageName.set("graalvm-test-server")
3762
}
3863
}
64+
65+
tasks.withType<Test>().configureEach {
66+
useJUnitPlatform()
67+
testLogging {
68+
events("passed", "skipped", "failed")
69+
}
70+
}
71+
3972
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.ktorgraal
2+
3+
import io.ktor.client.request.*
4+
import io.ktor.client.statement.*
5+
import io.ktor.server.testing.*
6+
import io.ktorgraal.plugins.configureRouting
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
10+
class ConfigureRoutingTest {
11+
12+
@Test
13+
fun testGetHi() = testApplication {
14+
application {
15+
configureRouting()
16+
}
17+
client.get("/").apply {
18+
assertEquals("Hello GraalVM!", bodyAsText())
19+
}
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"name": "kotlin.internal.jdk8.JDK8PlatformImplementations",
4+
"allDeclaredConstructors": true,
5+
"allPublicConstructors": true,
6+
"allDeclaredFields": true,
7+
"allPublicFields": true,
8+
"allDeclaredMethods": true,
9+
"allPublicMethods": true
10+
},
11+
{
12+
"name": "io.ktor.utils.io.pool.DefaultPool",
13+
"fields": [
14+
{
15+
"name": "top",
16+
"allowUnsafeAccess": true
17+
}
18+
]
19+
},
20+
{
21+
"name": "kotlin.reflect.jvm.internal.ReflectionFactoryImpl",
22+
"allDeclaredConstructors":true
23+
},
24+
{
25+
"name": "kotlin.KotlinVersion[]"
26+
},
27+
{
28+
"name": "kotlin.KotlinVersion$Companion"
29+
},
30+
{
31+
"name": "kotlin.KotlinVersion$Companion[]"
32+
}
33+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"resources":[
3+
{"pattern":"META-INF/.*.kotlin_module$"},
4+
{"pattern":"META-INF/services/.*"},
5+
{"pattern":".*.kotlin_builtins"}
6+
]
7+
}

0 commit comments

Comments
 (0)