Skip to content

Commit

Permalink
Fix lambda continuous testing failure
Browse files Browse the repository at this point in the history
If the tests start before the dev mode has initialized the system
property would be seen by dev mode, and dev mode could open it's
endpoint on 8081. This causes the tests to be run against the dev mode
endpoint which breaks change tracking.

Also fixes a few other minor things.
  • Loading branch information
stuartwdouglas committed Sep 9, 2021
1 parent 0f810e1 commit 395655c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.logging.LogCleanupFilterBuildItem;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.dev.testing.ContinuousTestingSharedStateManager;
import io.quarkus.dev.testing.TracingHandler;
import io.quarkus.gizmo.Gizmo;

Expand Down Expand Up @@ -82,6 +83,8 @@ void startTesting(TestConfig config, LiveReloadBuildItem liveReloadBuildItem,
testSupport.stop();
}
}
QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
((QuarkusClassLoader) cl.parent()).addCloseTask(ContinuousTestingSharedStateManager::reset);
}

@BuildStep(onlyIf = IsTest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.runtime.LaunchMode;
Expand Down Expand Up @@ -51,7 +51,7 @@ private boolean legacyTestingEnabled() {
public void startEventServer(LaunchModeBuildItem launchMode,
LambdaConfig config,
Optional<EventServerOverrideBuildItem> override,
BuildProducer<DevServicesNativeConfigResultBuildItem> devServicePropertiesProducer,
BuildProducer<DevServicesConfigResultBuildItem> devServicePropertiesProducer,
BuildProducer<ServiceStartBuildItem> serviceStartBuildItemBuildProducer) throws Exception {
if (!launchMode.getLaunchMode().isDevOrTest())
return;
Expand All @@ -73,9 +73,8 @@ public void startEventServer(LaunchModeBuildItem launchMode,
startMode = launchMode.getLaunchMode();
server.start(port);
String baseUrl = "localhost:" + port + MockEventServer.BASE_PATH;
System.setProperty(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, baseUrl);
devServicePropertiesProducer.produce(
new DevServicesNativeConfigResultBuildItem(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, baseUrl));
new DevServicesConfigResultBuildItem(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, baseUrl));
Runnable closeTask = () -> {
if (server != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.databind.ObjectReader;

import io.quarkus.runtime.Application;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.ShutdownContext;

public abstract class AbstractLambdaPollLoop {
Expand Down Expand Up @@ -261,7 +262,7 @@ boolean abortGracefully(Exception ex) {
// if we are running in test mode, or native mode outside of the lambda container, then don't output stack trace for socket errors

boolean lambdaEnv = System.getenv("AWS_LAMBDA_RUNTIME_API") != null;
boolean testEnv = System.getProperty(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API) != null;
boolean testEnv = LaunchMode.current() == LaunchMode.TEST;
boolean graceful = ((ex instanceof SocketException || ex instanceof ConnectException) && testEnv)
|| (ex instanceof UnknownHostException && !lambdaEnv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.runtime.LaunchMode;

/**
* Various constants and util methods used for communication with the AWS API.
*/
Expand Down Expand Up @@ -80,13 +84,15 @@ static String functionVersion() {
}

public static boolean isTestMode() {
return System.getProperty(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API) != null;
//need this config check for native tests
return LaunchMode.current() == LaunchMode.TEST
|| ConfigProvider.getConfig().getOptionalValue(QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, String.class).isPresent();
}

private static String runtimeApi() {
String testApi = System.getProperty(QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API);
if (testApi != null) {
return testApi;
var testApi = ConfigProvider.getConfig().getOptionalValue(QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, String.class);
if (testApi.isPresent()) {
return testApi.get();
}
return System.getenv("AWS_LAMBDA_RUNTIME_API");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.ContinuousTestingTestUtils;
Expand All @@ -30,7 +30,8 @@ public JavaArchive get() {
}
});

@Test
//run this twice, to make sure everything is cleaned up properly
@RepeatedTest(2)
public void testLambda() throws Exception {
ContinuousTestingTestUtils utils = new ContinuousTestingTestUtils();
var result = utils.waitForNextCompletion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -24,21 +25,6 @@ public class MockEventServer implements Closeable {
protected static final Logger log = Logger.getLogger(MockEventServer.class);
public static final int DEFAULT_PORT = 8081;

public void start() {
int port = DEFAULT_PORT;
start(port);
}

public void start(int port) {
this.port = port;
vertx = Vertx.vertx();
httpServer = vertx.createHttpServer();
router = Router.router(vertx);
setupRoutes();
httpServer.requestHandler(router).listen(port).result();
log.info("Mock Lambda Event Server Started");
}

private Vertx vertx;
private int port;
protected HttpServer httpServer;
Expand All @@ -55,6 +41,25 @@ public MockEventServer() {
queue = new LinkedBlockingQueue<>();
}

public void start() {
int port = DEFAULT_PORT;
start(port);
}

public void start(int port) {
this.port = port;
vertx = Vertx.vertx();
httpServer = vertx.createHttpServer();
router = Router.router(vertx);
setupRoutes();
try {
httpServer.requestHandler(router).listen(port).toCompletionStage().toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
log.info("Mock Lambda Event Server Started");
}

public HttpServer getHttpServer() {
return httpServer;
}
Expand Down Expand Up @@ -233,8 +238,18 @@ public void processError(RoutingContext ctx, RoutingContext pending, Buffer buff
@Override
public void close() throws IOException {
log.info("Stopping Mock Lambda Event Server");
httpServer.close().result();
vertx.close().result();
blockingPool.shutdown();
try {
httpServer.close().toCompletionStage().toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
} finally {
try {
vertx.close().toCompletionStage().toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
} finally {
blockingPool.shutdown();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setupTestRoutes(
// Add continuous testing
routeBuildItemBuildProducer.produce(nonApplicationRootPathBuildItem.routeBuilder()
.route("dev/test")
.handler(recorder.continousTestHandler(shutdownContextBuildItem))
.handler(recorder.continuousTestHandler(shutdownContextBuildItem))
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Handler<RoutingContext> devConsoleHandler(String devConsoleFinalDestinati
return new DevConsoleStaticHandler(devConsoleFinalDestination);
}

public Handler<RoutingContext> continousTestHandler(ShutdownContext context) {
public Handler<RoutingContext> continuousTestHandler(ShutdownContext context) {

ContinuousTestWebSocketHandler handler = new ContinuousTestWebSocketHandler();
ContinuousTestingSharedStateManager.addStateListener(handler);
Expand Down

0 comments on commit 395655c

Please sign in to comment.