Bind PackageServer to ephemeral port to avoid port conflicts (#227)

This is a comprehensive solution to the "flaky PackageServer tests"
problem. It rules out port conflicts and imposes no limits on test
parallelism. The same solution can be used for other test servers
in the future.

Major changes:
- Turn `PackageServer` from a singleton into a class that is
  instantiated per test class or test method.
- Start the server the first time its `port` property is read.
  Bind the server to an ephemeral port instead of port 12110.
- For every test that uses `PackageServer`, pass the server port to
  `--test-port`, `HttpClient.Builder.setTestPort`, the `CliBaseOptions`
  or `ExecutorOptions` constructor, or the Gradle plugin's `testPort` property.
  Wire all of these to `RequestRewritingClient`'s `testPort` constructor parameter.
- Enhance `RequestRewritingClient` to replace port 12110 with `testPort`
  in request URIs unless `testPort` is -1 (its default).
- Introduce `ExecutorOptions.Builder`.
  This makes executor options more comfortable to create
  and allows to hide options such as `testPort`.
- Deprecate the `ExecutorOptions` constructor to steer users towards the builder.
- Get rid of `ExecutorOptions2`, which is no longer needed.
- Clean up `EmbeddedExecutorTest` with the help of the builder.
This commit is contained in:
translatenix
2024-03-13 10:40:55 -07:00
committed by GitHub
parent 1e608b2aae
commit 014b3a8816
26 changed files with 756 additions and 581 deletions

View File

@@ -113,6 +113,12 @@ data class CliBaseOptions(
/** Tells whether to run the CLI in test mode. This is an internal option. */
val testMode: Boolean = false,
/**
* Unless -1, rewrites HTTP requests that specify port 12110 to the given port. This is an
* internal test option.
*/
val testPort: Int = -1,
/**
* The CA certificates to trust.
*
@@ -179,6 +185,7 @@ data class CliBaseOptions(
*/
val httpClient: HttpClient by lazy {
with(HttpClient.builder()) {
setTestPort(testPort)
if (normalizedCaCertificates.isEmpty()) {
addDefaultCliCertificates()
} else {

View File

@@ -17,6 +17,7 @@ package org.pkl.commons.cli.commands
import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.long
import com.github.ajalt.clikt.parameters.types.path
import java.io.File
@@ -135,6 +136,13 @@ class BaseOptions : OptionGroup() {
.path()
.multiple()
// hidden option used by native tests
private val testPort: Int by
option(names = arrayOf("--test-port"), help = "Internal test option", hidden = true)
.single()
.int()
.default(-1)
fun baseOptions(
modules: List<URI>,
projectOptions: ProjectOptions? = null,
@@ -155,6 +163,7 @@ class BaseOptions : OptionGroup() {
moduleCacheDir = cacheDir ?: defaults.normalizedModuleCacheDir,
noCache = noCache,
testMode = testMode,
testPort = testPort,
omitProjectSettings = projectOptions?.omitProjectSettings ?: false,
noProject = projectOptions?.noProject ?: false,
caCertificates = caCertificates