Skip to content

Commit

Permalink
Merge pull request #31645 from FroMage/31013
Browse files Browse the repository at this point in the history
RuntimeUpdatesProcessor now supports hot replacement problems
  • Loading branch information
FroMage authored Mar 14, 2023
2 parents 110fc5f + 25bc923 commit 2eb4839
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class RuntimeUpdatesProcessor implements HotReplacementContext, Closeable
private final DevModeType devModeType;
volatile Throwable compileProblem;
volatile Throwable testCompileProblem;
volatile Throwable hotReloadProblem;

private volatile Predicate<ClassInfo> disableInstrumentationForClassPredicate = new AlwaysFalsePredicate<>();
private volatile Predicate<Index> disableInstrumentationForIndexPredicate = new AlwaysFalsePredicate<>();
Expand Down Expand Up @@ -388,7 +389,8 @@ public List<Path> getResourcesDir() {
public Throwable getDeploymentProblem() {
//we differentiate between these internally, however for the error reporting they are the same
return compileProblem != null ? compileProblem
: IsolatedDevModeMain.deploymentProblem;
: IsolatedDevModeMain.deploymentProblem != null ? IsolatedDevModeMain.deploymentProblem
: hotReloadProblem;
}

@Override
Expand Down Expand Up @@ -551,13 +553,17 @@ public boolean doScan(boolean userInitiated, boolean forceRestart) {

return true;
} else if (!filesChanged.isEmpty()) {
for (Consumer<Set<String>> consumer : noRestartChangesConsumers) {
try {
try {
for (Consumer<Set<String>> consumer : noRestartChangesConsumers) {
consumer.accept(filesChanged);
} catch (Throwable t) {
log.error("Changed files consumer failed", t);
}
hotReloadProblem = null;
getCompileOutput().setMessage(null);
} catch (Throwable t) {
hotReloadProblem = t;
getCompileOutput().setMessage(t.getMessage());
}

log.infof("Files changed but restart not needed - notified extensions in: %ss ",
Timing.convertToBigDecimalSeconds(System.nanoTime() - startNanoseconds));
} else if (instrumentationChange) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.extest.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.extest.runtime.TestHotReplacementSetup;

public class HotReplacementProcessor {
@BuildStep
public HotDeploymentWatchedFileBuildItem registerHotReplacementFile() {
return new HotDeploymentWatchedFileBuildItem(TestHotReplacementSetup.HOT_REPLACEMENT_FILE, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.extest.runtime;

public class HotReplacementException extends Exception {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.extest.runtime;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

import io.quarkus.dev.ErrorPageGenerators;
import io.quarkus.dev.spi.HotReplacementContext;
import io.quarkus.dev.spi.HotReplacementSetup;

public class TestHotReplacementSetup implements HotReplacementSetup {

public final static String HOT_REPLACEMENT_FILE = "hot.replacement";

private static final String HOT_REPLACEMENT_EXCEPTION = HotReplacementException.class.getName();

private HotReplacementContext context;

@Override
public void setupHotDeployment(HotReplacementContext context) {
context.consumeNoRestartChanges(this::noRestartChanges);
this.context = context;
ErrorPageGenerators.register(HOT_REPLACEMENT_EXCEPTION, this::generatePage);
}

public void noRestartChanges(Set<String> changedFiles) {
if (changedFiles.contains(HOT_REPLACEMENT_FILE)) {
for (Path resourcePath : context.getResourcesDir()) {
Path myFile = resourcePath.resolve(HOT_REPLACEMENT_FILE);
if (Files.exists(myFile)) {
try {
String contents = Files.readString(myFile);
if ("throw".equals(contents))
throw new RuntimeException(new HotReplacementException());
return;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
}

private String generatePage(Throwable x) {
return "Generated page for exception " + x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.extest.runtime.TestHotReplacementSetup
29 changes: 29 additions & 0 deletions integration-tests/test-extension/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down Expand Up @@ -96,6 +101,30 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>io/quarkus/it/extension/HotReplacementSetupDevModeTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>quarkus-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>io/quarkus/it/extension/HotReplacementSetupDevModeTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<!-- See io.quarkus.extest.runtime.classpath.RecordedClasspathEntries -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.it.extension;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.extest.runtime.TestHotReplacementSetup;
import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;

public class HotReplacementSetupDevModeTest {

@RegisterExtension
static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar
.addClasses(SystemPropertyTestEndpoint.class)
.addAsResource(new StringAsset("nothing"), TestHotReplacementSetup.HOT_REPLACEMENT_FILE));

@Test
public void watched() {
RestAssured.get("/core/sysprop")
.then()
.statusCode(200);
TEST.modifyResourceFile(TestHotReplacementSetup.HOT_REPLACEMENT_FILE, text -> "throw");
RestAssured.get("/core/sysprop")
.then()
.statusCode(500)
.body(Matchers.containsString("Generated page for exception"));
TEST.modifyResourceFile(TestHotReplacementSetup.HOT_REPLACEMENT_FILE, text -> "nothing");
RestAssured.get("/core/sysprop")
.then()
.statusCode(200);
}
}

0 comments on commit 2eb4839

Please sign in to comment.