Skip to content

Commit 6197eea

Browse files
committed
Make test resource config override dev service config (and system properties)
1 parent d296cdb commit 6197eea

File tree

10 files changed

+84
-66
lines changed

10 files changed

+84
-66
lines changed

core/deployment/src/main/java/io/quarkus/deployment/builditem/DevServicesResultBuildItem.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,6 @@ public Map<String, Function<Startable, String>> getApplicationConfigProvider() {
174174

175175
public Map<String, String> getConfig(Startable startable) {
176176
SupplierMap<String, String> map = new SupplierMap<>();
177-
if (config != null && !config.isEmpty()) {
178-
map.putAll(config);
179-
}
180177
for (Map.Entry<String, Function<Startable, String>> entry : applicationConfigProvider.entrySet()) {
181178
map.put(entry.getKey(), () -> entry.getValue().apply(startable));
182179
}

core/runtime/src/main/java/io/quarkus/devservice/runtime/config/DevServicesConfigBuilder.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ public SmallRyeConfigBuilder configBuilder(SmallRyeConfigBuilder builder) {
1414

1515
@Override
1616
public int priority() {
17-
// What's the right priority? This is a cheeky dynamic override, so a high priority seems correct, but dev services are supposed to fill in gaps in existing information.
18-
// Dev services should be looking at those sources and not doing anything if there's existing config,
19-
// so a very low priority is also arguably correct.
20-
// In principle the priority actually shouldn't matter much, but in practice it needs to not be higher than Arquillian config overrides or some tests fail
21-
2217
return 10;
2318
}
2419
}

core/runtime/src/main/java/io/quarkus/devservice/runtime/config/DevServicesConfigSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String getName() {
5656

5757
@Override
5858
public int getOrdinal() {
59-
// See discussion on DevServicesConfigBuilder about what the right value here is
60-
return 10;
59+
// This is a dynamic override, so it needs a high priority to be able to override ephemeral port (0) with real values
60+
return 410; // a bit more than system properties, but less than the TestResource config overrides done via RuntimeOverrideConfigSource
6161
}
6262
}

core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeOverrideConfigSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Set<String> getPropertyNames() {
4949

5050
@Override
5151
public int getOrdinal() {
52-
return 399; //one less that system properties
52+
return 420; // higher than dev services config source
5353
}
5454

5555
@Override

devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/EffectiveConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ private EffectiveConfig(Builder builder) {
4949
// (see also https://quarkus.io/guides/config-reference#configuration-sources)
5050
// 600 -> forcedProperties
5151
// 500 -> taskProperties
52+
// 420 -> RuntimeOverride (used by TestResource and other Startup actions)
53+
// 410 -> Dev Service overrides
5254
// 400 -> System.getProperties() (provided by default sources)
5355
// 300 -> System.getenv() (provided by default sources)
5456
// 290 -> quarkusBuildProperties

extensions/amazon-lambda/common-deployment/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<groupId>io.quarkus</groupId>
4040
<artifactId>quarkus-amazon-lambda-event-server</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>io.quarkus</groupId>
44+
<artifactId>quarkus-devservices-deployment</artifactId>
45+
</dependency>
4246
<dependency>
4347
<groupId>io.quarkus</groupId>
4448
<artifactId>quarkus-junit5-internal</artifactId>

extensions/amazon-lambda/common-deployment/src/main/java/io/quarkus/amazon/lambda/deployment/DevServicesLambdaProcessor.java

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;
44

5-
import java.util.HashMap;
5+
import java.io.IOException;
66
import java.util.Map;
77
import java.util.Optional;
8-
import java.util.function.Supplier;
98

109
import org.jboss.logging.Logger;
1110

@@ -18,18 +17,15 @@
1817
import io.quarkus.deployment.annotations.BuildStep;
1918
import io.quarkus.deployment.annotations.Produce;
2019
import io.quarkus.deployment.annotations.Record;
21-
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
2220
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
2321
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
2422
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
23+
import io.quarkus.deployment.builditem.Startable;
2524
import io.quarkus.runtime.LaunchMode;
2625

2726
public class DevServicesLambdaProcessor {
2827
private static final Logger log = Logger.getLogger(DevServicesLambdaProcessor.class);
2928

30-
static MockEventServer server;
31-
static LaunchMode startMode;
32-
3329
@BuildStep(onlyIfNot = IsNormal.class)
3430
@Record(STATIC_INIT)
3531
public void enableHotReplacementChecker(LaunchModeBuildItem launchMode,
@@ -53,61 +49,81 @@ private boolean legacyTestingEnabled() {
5349

5450
@Produce(ServiceStartBuildItem.class)
5551
@BuildStep(onlyIfNot = IsNormal.class) // This is required for testing so run it even if devservices.enabled=false
56-
public void startEventServer(LaunchModeBuildItem launchMode,
52+
public void startEventServer(LaunchModeBuildItem launchModeBuildItem,
5753
LambdaConfig config,
5854
Optional<EventServerOverrideBuildItem> override,
59-
BuildProducer<DevServicesResultBuildItem> devServicePropertiesProducer,
60-
CuratedApplicationShutdownBuildItem closeBuildItem)
61-
throws Exception {
62-
if (!launchMode.getLaunchMode().isDevOrTest())
55+
BuildProducer<DevServicesResultBuildItem> devServicePropertiesProducer) {
56+
LaunchMode launchMode = launchModeBuildItem.getLaunchMode();
57+
if (!launchMode.isDevOrTest())
6358
return;
6459
if (legacyTestingEnabled())
6560
return;
6661
if (!config.mockEventServer().enabled()) {
6762
return;
6863
}
69-
if (server != null) {
70-
return;
71-
}
72-
Supplier<MockEventServer> supplier = null;
64+
65+
MockEventServer server;
7366
if (override.isPresent()) {
74-
supplier = override.get().getServer();
67+
server = override.get().getServer().get();
7568
} else {
76-
supplier = () -> new MockEventServer();
69+
server = new MockEventServer();
7770
}
7871

79-
server = supplier.get();
80-
int port = launchMode.getLaunchMode() == LaunchMode.TEST ? config.mockEventServer().testPort()
72+
int configuredPort = launchMode == LaunchMode.TEST ? config.mockEventServer().testPort()
8173
: config.mockEventServer().devPort();
82-
startMode = launchMode.getLaunchMode();
83-
server.start(port);
84-
int actualPort = server.getPort();
85-
String baseUrl = "localhost:" + actualPort + MockEventServer.BASE_PATH;
86-
Map<String, String> properties = new HashMap<>();
87-
properties.put(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, baseUrl);
88-
89-
if (actualPort != port) {
90-
String portPropertyValue = String.valueOf(actualPort);
91-
String portPropertySuffix = launchMode.getLaunchMode() == LaunchMode.TEST ? "test-port" : "dev-port";
92-
String propName = "quarkus.lambda.mock-event-server." + portPropertySuffix;
93-
System.setProperty(propName, portPropertyValue);
74+
String portPropertySuffix = launchMode == LaunchMode.TEST ? "test-port" : "dev-port";
75+
String propName = "quarkus.lambda.mock-event-server." + portPropertySuffix;
76+
77+
// No compose support, and no using of external services, so no need to discover existing services
78+
79+
DevServicesResultBuildItem buildItem = DevServicesResultBuildItem.owned().feature(Feature.AMAZON_LAMBDA)
80+
.serviceName(Feature.AMAZON_LAMBDA.getName())
81+
.serviceConfig(config)
82+
.startable(() -> new StartableEventServer(
83+
server, configuredPort))
84+
.configProvider(
85+
Map.of(propName, s -> String.valueOf(s.getExposedPort()),
86+
AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API,
87+
StartableEventServer::getConnectionInfo))
88+
.build();
89+
90+
devServicePropertiesProducer.produce(buildItem);
91+
92+
}
93+
94+
private static class StartableEventServer implements Startable {
95+
96+
private final MockEventServer server;
97+
private final int configuredPort;
98+
99+
public StartableEventServer(MockEventServer server, int configuredPort) {
100+
this.server = server;
101+
this.configuredPort = configuredPort;
94102
}
95103

96-
devServicePropertiesProducer.produce(
97-
new DevServicesResultBuildItem(Feature.AMAZON_LAMBDA.getName(), null, properties));
98-
Runnable closeTask = () -> {
99-
if (server != null) {
100-
try {
101-
server.close();
102-
} catch (Throwable e) {
103-
log.error("Failed to stop the Lambda Mock Event Server", e);
104-
} finally {
105-
server = null;
106-
}
107-
}
108-
startMode = null;
109-
server = null;
110-
};
111-
closeBuildItem.addCloseTask(closeTask, true);
104+
@Override
105+
public void start() {
106+
server.start(configuredPort);
107+
log.debugf("Starting event server on port %d", configuredPort);
108+
}
109+
110+
public int getExposedPort() {
111+
return server.getPort();
112+
}
113+
114+
@Override
115+
public String getConnectionInfo() {
116+
return "localhost:" + getExposedPort() + MockEventServer.BASE_PATH;
117+
}
118+
119+
@Override
120+
public String getContainerId() {
121+
return "";
122+
}
123+
124+
@Override
125+
public void close() throws IOException {
126+
server.close();
127+
}
112128
}
113129
}

extensions/amazon-lambda/event-server/src/main/java/io/quarkus/amazon/lambda/runtime/MockEventServer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ public void start(int port) {
7474
vertx = Vertx.vertx(new VertxOptions().setMaxWorkerExecuteTime(60).setMaxWorkerExecuteTimeUnit(TimeUnit.MINUTES));
7575
HttpServerOptions options = new HttpServerOptions();
7676
options.setPort(port == 0 ? -1 : port);
77-
Optional<MemorySize> maybeMaxHeadersSize = ConfigProvider.getConfig()
78-
.getOptionalValue("quarkus.http.limits.max-header-size", MemorySize.class);
79-
if (maybeMaxHeadersSize.isPresent()) {
80-
options.setMaxHeaderSize(maybeMaxHeadersSize.get().asBigInteger().intValueExact());
77+
78+
ClassLoader original = Thread.currentThread().getContextClassLoader();
79+
try {
80+
Thread.currentThread().setContextClassLoader(MockEventServer.class.getClassLoader());
81+
Optional<MemorySize> maybeMaxHeadersSize = ConfigProvider.getConfig()
82+
.getOptionalValue("quarkus.http.limits.max-header-size", MemorySize.class);
83+
84+
if (maybeMaxHeadersSize.isPresent()) {
85+
options.setMaxHeaderSize(maybeMaxHeadersSize.get().asBigInteger().intValueExact());
86+
}
87+
} finally {
88+
Thread.currentThread().setContextClassLoader(original);
8189
}
8290
httpServer = vertx.createHttpServer(options);
8391
router = Router.router(vertx);

integration-tests/amazon-lambda/src/test/java/io/quarkus/it/amazon/lambda/AmazonLambdaEphemeralPortTestCase.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import static io.restassured.RestAssured.given;
44
import static org.hamcrest.CoreMatchers.containsString;
55

6-
import org.junit.jupiter.api.Disabled;
76
import org.junit.jupiter.api.Test;
87

98
import io.quarkus.it.amazon.lambda.profiles.EphemeralPortProfile;
109
import io.quarkus.test.junit.QuarkusTest;
1110
import io.quarkus.test.junit.TestProfile;
1211

13-
@Disabled("Failing since 3.22, should be fixed by by https://github.com/quarkusio/quarkus/issues/48006")
1412
@TestProfile(EphemeralPortProfile.class)
1513
@QuarkusTest
1614
public class AmazonLambdaEphemeralPortTestCase {

integration-tests/amazon-lambda/src/test/java/io/quarkus/it/amazon/lambda/AmazonLambdaWithProfileSimpleTestCase.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import static io.restassured.RestAssured.given;
44
import static org.hamcrest.CoreMatchers.containsString;
55

6-
import org.junit.jupiter.api.Disabled;
76
import org.junit.jupiter.api.Test;
87

98
import io.quarkus.it.amazon.lambda.profiles.TrivialProfile;
109
import io.quarkus.test.junit.QuarkusTest;
1110
import io.quarkus.test.junit.TestProfile;
1211

13-
@Disabled("Failing, tracked by https://github.com/quarkusio/quarkus/issues/48006")
1412
@TestProfile(TrivialProfile.class)
1513
@QuarkusTest
1614
public class AmazonLambdaWithProfileSimpleTestCase {

0 commit comments

Comments
 (0)