Skip to content

Commit

Permalink
Isolate testThatNewResourcesAreServed in FlakyDevMojoIT
Browse files Browse the repository at this point in the history
Also modernize things a bit now that we are using Java 17+.
  • Loading branch information
gsmet committed Apr 12, 2024
1 parent b5d7429 commit b4ae2ce
Show file tree
Hide file tree
Showing 14 changed files with 730 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -936,22 +937,24 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
try {
Path relative = root.relativize(path);
Path target = outputDir.resolve(relative);

seen.remove(target);
if (!timestampSet.watchedPaths.containsKey(path)) {
moduleResources.add(target);
if (!Files.exists(target) || Files.getLastModifiedTime(target).toMillis() < Files
.getLastModifiedTime(path).toMillis()) {

long current = Files.getLastModifiedTime(path).toMillis();

if (!Files.exists(target) || Files.getLastModifiedTime(target).toMillis() < current) {
if (Files.isDirectory(path)) {
Files.createDirectories(target);
} else {
Files.createDirectories(target.getParent());
// A new file is added to a resource root
// We need to use the OS-agnostic path to match the HotDeploymentWatchedFileBuildItem
ret.add(toOSAgnosticPathStr(relative.toString()));
byte[] data = Files.readAllBytes(path);
try (FileOutputStream out = new FileOutputStream(target.toFile())) {
out.write(data);
}
Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES);

if (copyResourceNotification != null) {
copyResourceNotification.accept(module, relative.toString());
}
Expand All @@ -967,11 +970,15 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
for (Path i : seen) {
moduleResources.remove(i);
if (!Files.isDirectory(i)) {
Files.delete(i);
try {
Files.delete(i);
} catch (IOException e) {
log.error("Failed to delete resources", e);
}
}
}
} catch (IOException e) {
log.error("Failed to copy resources", e);
log.error("Unable to walk through the directory", e);
}
}

Expand All @@ -988,6 +995,7 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
try {
long current = Files.getLastModifiedTime(watchedPath.filePath).toMillis();
long last = watchedPath.lastModified;

if (current > last) {
// Use either the absolute path or the OS-agnostic path to match the HotDeploymentWatchedFileBuildItem
ret.add(isAbsolute ? watchedPath.filePath.toString() : watchedPath.getOSAgnosticMatchPath());
Expand Down Expand Up @@ -1372,7 +1380,8 @@ private boolean isAbsolute() {

@Override
public String toString() {
return "WatchedPath [matchPath=" + matchPath + ", filePath=" + filePath + ", restartNeeded=" + restartNeeded + "]";
return "WatchedPath [matchPath=" + matchPath + ", filePath=" + filePath + ", restartNeeded=" + restartNeeded
+ ", lastModified=" + lastModified + "]";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static io.quarkus.maven.it.ApplicationNameAndVersionTestUtil.assertApplicationPropertiesSetCorrectly;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -943,37 +941,6 @@ public void testThatExternalConfigOverridesConfigInJar() throws MavenInvocationE
.until(() -> devModeClient.getHttpResponse("/app/hello/greeting").contains(uuid));
}

@Test
public void testThatNewResourcesAreServed() throws MavenInvocationException, IOException {
testDir = initProject("projects/classic", "projects/project-classic-run-resource-change");
runAndCheck();

// Create a new resource
File source = new File(testDir, "src/main/resources/META-INF/resources/lorem.txt");
FileUtils.write(source,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"UTF-8");
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt"), containsString("Lorem ipsum"));

// Update the resource
String uuid = UUID.randomUUID().toString();
FileUtils.write(source, uuid, "UTF-8");
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt"), equalTo(uuid));

// Delete the resource
source.delete();
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt", 404));
}

@Test
public void testThatConfigFileDeletionsAreDetected() throws MavenInvocationException, IOException {
testDir = initProject("projects/dev-mode-file-deletion");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.maven.it;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.apache.maven.shared.invoker.MavenInvocationException;
import org.junit.jupiter.api.Test;

import io.quarkus.test.devmode.util.DevModeClient;

/**
* This test has been isolated as it is very flaky and causing issues with Develocity PTS.
*/
@DisableForNative
public class FlakyDevMojoIT extends RunAndCheckMojoTestBase {

protected DevModeClient devModeClient = new DevModeClient(getPort());

@Test
public void testThatNewResourcesAreServed() throws MavenInvocationException, IOException {
testDir = initProject("projects/classic-with-log", "projects/project-classic-run-resource-change");
runAndCheck();

// Create a new resource
Path source = testDir.toPath().resolve("src/main/resources/META-INF/resources/lorem.txt");
Files.writeString(source,
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt"), containsString("Lorem ipsum"));

// Update the resource
String uuid = UUID.randomUUID().toString();
Files.writeString(source, uuid);
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt"), equalTo(uuid));

// Delete the resource
Files.delete(source);
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(TestUtils.getDefaultTimeout(), TimeUnit.MINUTES)
.until(() -> devModeClient.getHttpResponse("/lorem.txt", 404));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OTHER_GREETING=Hola
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>acme</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<compiler-plugin.version>${compiler-plugin.version}</compiler-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.target}</maven.compiler.target>

<!-- do not update this dependency, it needs to be stable for testing -->
<webjar.jquery-ui.version>1.13.0</webjar.jquery-ui.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- insert managed dependencies here -->
<dependency>
<groupId>\${quarkus.platform.group-id}</groupId>
<artifactId>\${quarkus.platform.artifact-id}</artifactId>
<version>\${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- insert test dependencies here -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-context-propagation</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-websockets</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-vertx-context</artifactId>
<version>1.13.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery-ui</artifactId>
<version>\${webjar.jquery-ui.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>\${compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>\${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<properties>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
<profile>
<id>customOutputDir</id>
<build>
<directory>target-other</directory>
</build>
</profile>
</profiles>
</project>

0 comments on commit b4ae2ce

Please sign in to comment.