Improve configuration and tests for native-image (#509)

* Don't expose JDK internal classes; instead solve msgpack issue with `--initialize-at-run-time`.
* Use quick build mode for non-release builds:  40% faster compilation, 20% smaller executable.
* Remove options that were commented out.
* Also run ServerTest against native executable
This commit is contained in:
Daniel Chao
2024-06-03 17:08:30 -07:00
committed by GitHub
parent a48748cb9c
commit d81a12352c
11 changed files with 232 additions and 170 deletions

View File

@@ -15,8 +15,6 @@
*/
package org.pkl.server
import java.io.PipedInputStream
import java.io.PipedOutputStream
import java.net.URI
import java.nio.file.Path
import java.util.concurrent.ExecutorService
@@ -27,21 +25,20 @@ import kotlin.io.path.outputStream
import kotlin.io.path.writeText
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.msgpack.core.MessagePack
import org.pkl.commons.test.PackageServer
import org.pkl.core.http.HttpClient
import org.pkl.core.module.PathElement
class ServerTest {
companion object {
private const val useDirectTransport = false
abstract class AbstractServerTest {
private val executor: ExecutorService =
if (useDirectTransport) {
companion object {
/** Set to `true` to bypass messagepack serialization when running [JvmServerTest]. */
const val USE_DIRECT_TRANSPORT = false
val executor: ExecutorService =
if (USE_DIRECT_TRANSPORT) {
createDirectExecutor()
} else {
Executors.newCachedThreadPool()
@@ -49,38 +46,12 @@ class ServerTest {
@AfterAll
@JvmStatic
@Suppress("unused")
fun afterAll() {
executor.shutdown()
}
}
private val transports: Pair<MessageTransport, MessageTransport> = run {
if (useDirectTransport) {
MessageTransports.direct()
} else {
val in1 = PipedInputStream()
val out1 = PipedOutputStream(in1)
val in2 = PipedInputStream()
val out2 = PipedOutputStream(in2)
MessageTransports.stream(in1, out2) to MessageTransports.stream(in2, out1)
}
}
private val client: TestTransport = TestTransport(transports.first)
private val server: Server = Server(transports.second, HttpClient.dummyClient())
@BeforeEach
fun before() {
executor.execute { server.start() }
executor.execute { client.start() }
}
@AfterEach
fun after() {
client.close()
server.close()
}
abstract val client: TestTransport
@Test
fun `create and close evaluator`() {

View File

@@ -0,0 +1,51 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.server
import java.io.PipedInputStream
import java.io.PipedOutputStream
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.pkl.core.http.HttpClient
class JvmServerTest : AbstractServerTest() {
private val transports: Pair<MessageTransport, MessageTransport> = run {
if (USE_DIRECT_TRANSPORT) {
MessageTransports.direct()
} else {
val in1 = PipedInputStream()
val out1 = PipedOutputStream(in1)
val in2 = PipedInputStream()
val out2 = PipedOutputStream(in2)
MessageTransports.stream(in1, out2) to MessageTransports.stream(in2, out1)
}
}
override val client: TestTransport = TestTransport(transports.first)
private val server: Server = Server(transports.second, HttpClient.dummyClient())
@BeforeEach
fun beforeEach() {
executor.execute { server.start() }
executor.execute { client.start() }
}
@AfterEach
fun after() {
client.close()
server.close()
}
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.server
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.pkl.commons.test.PklExecutablePaths
class NativeServerTest : AbstractServerTest() {
private lateinit var server: Process
override lateinit var client: TestTransport
@BeforeEach
fun beforeEach() {
val executable = PklExecutablePaths.firstExisting.toString()
server = ProcessBuilder(executable, "server").start()
client = TestTransport(MessageTransports.stream(server.inputStream, server.outputStream))
executor.execute { client.start() }
}
@AfterEach
fun afterEach() {
client.close()
server.destroy()
}
}

View File

@@ -19,8 +19,8 @@ import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.BlockingQueue
import org.assertj.core.api.Assertions.assertThat
internal class TestTransport(private val delegate: MessageTransport) : AutoCloseable {
private val incomingMessages: BlockingQueue<Message> = ArrayBlockingQueue(10)
class TestTransport(private val delegate: MessageTransport) : AutoCloseable {
val incomingMessages: BlockingQueue<Message> = ArrayBlockingQueue(10)
fun start() {
delegate.start({ incomingMessages.put(it) }, { incomingMessages.put(it) })