Skip to content

Commit

Permalink
Merge pull request #174 from ktorio/vgoncharova/KTOR-6033
Browse files Browse the repository at this point in the history
Updated GraalVM Example to run tests on GraalVM with native image build
  • Loading branch information
VGoncharova authored Jul 6, 2023
2 parents f4560b8 + 69fcea4 commit f070f4e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
4 changes: 4 additions & 0 deletions graalvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ A demo project that shows how to combine Ktor server applications with [GraalVM]
3. The previous step produces an executable file named `graal-server` which can then be run. Open up
`http://0.0.0.0:8080` to test the server.

4. Run the command `./gradlew nativeTestCompile` (or `gradlew nativeTestCompile` on Windows) to build an executable file for tests.

5. That step produces an executable file named `graal-test-server` which can then be run.

### Current limitations

Using the `Netty` engine is not compatible with GraalVM. Please following
Expand Down
37 changes: 35 additions & 2 deletions graalvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
application
kotlin("jvm") version "1.7.20"
id("io.ktor.plugin") version "2.3.2"
id("org.graalvm.buildtools.native") version "0.9.11"
id("org.graalvm.buildtools.native") version "0.9.19"
}

group = "io.ktor"
Expand All @@ -19,21 +19,54 @@ dependencies {
implementation("ch.qos.logback:logback-classic:1.4.6")
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-cio-jvm")

testImplementation("io.ktor:ktor-server-test-host-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}

graalvmNative {
binaries {

named("main") {
fallback.set(false)
verbose.set(true)

buildArgs.add("--initialize-at-build-time=ch.qos.logback")
buildArgs.add("--initialize-at-build-time=io.ktor,kotlin")
buildArgs.add("--initialize-at-build-time=org.slf4j.LoggerFactory")

buildArgs.add("-H:+InstallExitHandlers")
buildArgs.add("-H:+ReportUnsupportedElementsAtRuntime")
buildArgs.add("-H:+ReportExceptionStackTraces")

imageName.set("graalvm-server")
}

named("test"){
fallback.set(false)
verbose.set(true)

buildArgs.add("--initialize-at-build-time=ch.qos.logback")
buildArgs.add("--initialize-at-build-time=io.ktor,kotlin")
buildArgs.add("--initialize-at-build-time=org.slf4j.LoggerFactory")

buildArgs.add("-H:+InstallExitHandlers")
buildArgs.add("-H:+ReportUnsupportedElementsAtRuntime")
buildArgs.add("-H:+ReportExceptionStackTraces")

imageName.set("graal-server")
val path = "${projectDir}/src/test/resources/META-INF/native-image/"
buildArgs.add("-H:ReflectionConfigurationFiles=${path}reflect-config.json")
buildArgs.add("-H:ResourceConfigurationFiles=${path}resource-config.json")

imageName.set("graalvm-test-server")
}
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}

}
21 changes: 21 additions & 0 deletions graalvm/src/test/kotlin/io/ktorgraal/ConfigureRoutingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.ktorgraal

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.server.testing.*
import io.ktorgraal.plugins.configureRouting
import kotlin.test.Test
import kotlin.test.assertEquals

class ConfigureRoutingTest {

@Test
fun testGetHi() = testApplication {
application {
configureRouting()
}
client.get("/").apply {
assertEquals("Hello GraalVM!", bodyAsText())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"name": "kotlin.internal.jdk8.JDK8PlatformImplementations",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredFields": true,
"allPublicFields": true,
"allDeclaredMethods": true,
"allPublicMethods": true
},
{
"name": "io.ktor.utils.io.pool.DefaultPool",
"fields": [
{
"name": "top",
"allowUnsafeAccess": true
}
]
},
{
"name": "kotlin.reflect.jvm.internal.ReflectionFactoryImpl",
"allDeclaredConstructors":true
},
{
"name": "kotlin.KotlinVersion[]"
},
{
"name": "kotlin.KotlinVersion$Companion"
},
{
"name": "kotlin.KotlinVersion$Companion[]"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"resources":[
{"pattern":"META-INF/.*.kotlin_module$"},
{"pattern":"META-INF/services/.*"},
{"pattern":".*.kotlin_builtins"}
]
}

0 comments on commit f070f4e

Please sign in to comment.