Implement isolated Build and Refresh with immutable leased snapshots
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package de.neitzel.fxdesigner.build;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class SnapshotStoreTest {
|
||||
@TempDir Path root;
|
||||
|
||||
@Test
|
||||
void copiesSurviveOutputReplacementAndLeasesPreventEvictionAcrossStoreInstances()
|
||||
throws Exception {
|
||||
Path output = Files.createDirectories(root.resolve("output"));
|
||||
Files.writeString(output.resolve("A.class"), "old");
|
||||
var store = new SnapshotStore(root.resolve("store"));
|
||||
var first =
|
||||
store.publish("desktop", Map.of("source", "hash"), List.of(output), Map.of(), () -> true);
|
||||
var lease = store.lease(first.id());
|
||||
Files.writeString(output.resolve("A.class"), "new");
|
||||
var second = store.publish("desktop", Map.of(), List.of(output), Map.of(), () -> true);
|
||||
assertEquals("old", Files.readString(lease.runtime().getFirst().resolve("A.class")));
|
||||
assertEquals(0, new SnapshotStore(root.resolve("store")).evict());
|
||||
lease.close();
|
||||
assertEquals(1, store.evict());
|
||||
try (var next = store.lease(second.id())) {
|
||||
assertEquals("new", Files.readString(next.runtime().getFirst().resolve("A.class")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedCaptureDoesNotReplaceLastSuccessAndAbandonedStagingIsRecoverable() throws Exception {
|
||||
Path jar = Files.writeString(root.resolve("lib.jar"), "dependency");
|
||||
var store = new SnapshotStore(root.resolve("store"));
|
||||
var first = store.publish("c", Map.of(), List.of(jar), Map.of(), () -> true);
|
||||
assertThrows(
|
||||
java.io.IOException.class,
|
||||
() -> store.publish("c", Map.of(), List.of(jar), Map.of(), () -> false));
|
||||
assertEquals(first.id(), store.state("c").lastSuccessful());
|
||||
Path abandoned = Files.createDirectory(root.resolve("store/.staging-crashed"));
|
||||
store.evict();
|
||||
assertFalse(Files.exists(abandoned));
|
||||
store.invalidate("c", "Failed build");
|
||||
assertTrue(new SnapshotStore(root.resolve("store")).state("c").outdated());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutationDuringCaptureAndSnapshotTamperingAreRejected() throws Exception {
|
||||
Path output = Files.createDirectories(root.resolve("classes"));
|
||||
Path a = Files.writeString(output.resolve("A.class"), "original");
|
||||
var store = new SnapshotStore(root.resolve("store"));
|
||||
var calls = new AtomicInteger();
|
||||
assertThrows(
|
||||
java.io.IOException.class,
|
||||
() ->
|
||||
store.publish(
|
||||
"c",
|
||||
Map.of(),
|
||||
List.of(output),
|
||||
Map.of(),
|
||||
() -> true,
|
||||
() -> {
|
||||
if (calls.incrementAndGet() == 3)
|
||||
try {
|
||||
Files.writeString(a, "changed");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return false;
|
||||
}));
|
||||
assertNull(store.state("c").lastSuccessful());
|
||||
var snapshot = store.publish("c", Map.of(), List.of(output), Map.of(), () -> true);
|
||||
try (var lease = store.lease(snapshot.id())) {
|
||||
Files.writeString(lease.runtime().getFirst().resolve("A.class"), "tampered");
|
||||
}
|
||||
assertThrows(java.io.IOException.class, () -> store.lease(snapshot.id()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputIsolationRejectsSymlinksAndRecordsAdditionsAndSameContentRewrites() throws Exception {
|
||||
Path source = Files.createDirectories(root.resolve("source"));
|
||||
Path file = Files.writeString(source.resolve("A.java"), "same");
|
||||
var before = InputTree.scan(source);
|
||||
Files.setLastModifiedTime(
|
||||
file,
|
||||
java.nio.file.attribute.FileTime.fromMillis(
|
||||
Files.getLastModifiedTime(file).toMillis() + 1000));
|
||||
assertNotEquals(before, InputTree.scan(source));
|
||||
Files.createSymbolicLink(source.resolve("link"), file);
|
||||
assertThrows(java.io.IOException.class, () -> InputTree.scan(source));
|
||||
}
|
||||
|
||||
public static final class LeaseKeeper {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (var lease = new SnapshotStore(Path.of(args[0])).lease(args[1])) {
|
||||
System.out.println("LEASED");
|
||||
System.out.flush();
|
||||
System.in.read();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void workerProcessLeaseSurvivesAnotherHostAndReleasesAfterOwnerDeath() throws Exception {
|
||||
Path jar = Files.writeString(root.resolve("lib.jar"), "one");
|
||||
Path cache = root.resolve("store");
|
||||
var store = new SnapshotStore(cache);
|
||||
var first = store.publish("c", Map.of(), List.of(jar), Map.of(), () -> true);
|
||||
var child =
|
||||
new ProcessBuilder(
|
||||
Path.of(System.getProperty("java.home"), "bin/java").toString(),
|
||||
"-cp",
|
||||
System.getProperty("java.class.path"),
|
||||
LeaseKeeper.class.getName(),
|
||||
cache.toString(),
|
||||
first.id())
|
||||
.redirectErrorStream(true)
|
||||
.start();
|
||||
try {
|
||||
var line =
|
||||
java.util.concurrent.CompletableFuture.supplyAsync(
|
||||
() -> {
|
||||
try {
|
||||
return new java.io.BufferedReader(
|
||||
new java.io.InputStreamReader(child.getInputStream()))
|
||||
.readLine();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.get(10, java.util.concurrent.TimeUnit.SECONDS);
|
||||
assertEquals("LEASED", line);
|
||||
store.publish("c", Map.of(), List.of(jar), Map.of(), () -> true);
|
||||
assertEquals(0, store.evict());
|
||||
child.destroyForcibly();
|
||||
assertTrue(child.waitFor(10, java.util.concurrent.TimeUnit.SECONDS));
|
||||
assertEquals(1, new SnapshotStore(cache).evict());
|
||||
} finally {
|
||||
child.destroyForcibly();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void targetNamedSourcePackageIsNotConfusedWithMavenOutput() throws Exception {
|
||||
Files.writeString(root.resolve("pom.xml"), "<project/>");
|
||||
Files.createDirectories(root.resolve("target/classes"));
|
||||
Files.writeString(root.resolve("target/classes/Old.class"), "old");
|
||||
Files.createDirectories(root.resolve("src/main/java/example/target"));
|
||||
Files.writeString(root.resolve("src/main/java/example/target/Model.java"), "class Model {}");
|
||||
var inputs = InputTree.scan(root);
|
||||
assertTrue(inputs.containsKey("src/main/java/example/target/Model.java"));
|
||||
assertFalse(inputs.containsKey("target/classes/Old.class"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void oldMavenCaptureWithoutDependencyProofIsRejected() throws Exception {
|
||||
Path metadata = Files.createDirectory(root.resolve("metadata"));
|
||||
Files.writeString(metadata.resolve("old.properties"), "format=1\n");
|
||||
assertThrows(java.io.IOException.class, () -> MavenCapture.read(metadata, root, root, null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user