Establish buildable modules and shared host-service boundaries
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package de.neitzel.fxdesigner.host;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HostServicesTest {
|
||||
@Test
|
||||
void scopesAreIndependentAndCloseInReverseOrder() {
|
||||
var seen = new ArrayList<Integer>();
|
||||
var first = new SessionScope();
|
||||
var other = new SessionScope();
|
||||
first.own(() -> seen.add(1));
|
||||
first.own(() -> seen.add(2));
|
||||
other.own(() -> seen.add(3));
|
||||
first.close();
|
||||
first.close();
|
||||
assertEquals(List.of(2, 1), seen);
|
||||
other.close();
|
||||
assertEquals(List.of(2, 1, 3), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanupContinuesAfterFailureAndLateResourcesClose() {
|
||||
var scope = new SessionScope();
|
||||
var seen = new ArrayList<Integer>();
|
||||
scope.own(() -> seen.add(1));
|
||||
scope.own(
|
||||
() -> {
|
||||
throw new Exception("broken");
|
||||
});
|
||||
assertThrows(IllegalStateException.class, scope::close);
|
||||
assertEquals(List.of(1), seen);
|
||||
assertThrows(IllegalStateException.class, () -> scope.own(() -> seen.add(2)));
|
||||
assertEquals(List.of(1, 2), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void runtimeRolesNeverFallBack() {
|
||||
var runtime = new HostServices.RuntimeInstallation(Path.of("/explicit"), 21);
|
||||
var r =
|
||||
new ConfiguredRuntimes(
|
||||
Map.of(new ConfiguredRuntimes.Key("java", HostServices.RuntimeRole.TARGET), runtime));
|
||||
assertEquals(
|
||||
runtime, r.resolve("java", HostServices.RuntimeRole.TARGET).toCompletableFuture().join());
|
||||
assertThrows(
|
||||
CompletionException.class,
|
||||
() -> r.resolve("java", HostServices.RuntimeRole.BUILD).toCompletableFuture().join());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCapturesBothStreamsAndCancellation() throws Exception {
|
||||
String java = Path.of(System.getProperty("java.home"), "bin", "java").toString();
|
||||
var lines = new CopyOnWriteArrayList<HostServices.BuildOutput>();
|
||||
var builds = new ProcessBuilds();
|
||||
var run =
|
||||
builds.start(
|
||||
new HostServices.BuildRequest(
|
||||
Path.of("."),
|
||||
List.of(java, "-cp", System.getProperty("java.class.path"), Child.class.getName()),
|
||||
Map.of()),
|
||||
lines::add);
|
||||
var done = run.completion().toCompletableFuture().get(10, TimeUnit.SECONDS);
|
||||
assertEquals(0, done.exitCode());
|
||||
assertFalse(done.cancelled());
|
||||
assertTrue(lines.stream().anyMatch(x -> x.stderr() && x.line().equals("error output")));
|
||||
assertTrue(lines.stream().anyMatch(x -> !x.stderr() && x.line().equals("normal output")));
|
||||
var pending =
|
||||
builds.start(
|
||||
new HostServices.BuildRequest(
|
||||
Path.of("."),
|
||||
List.of(
|
||||
java,
|
||||
"-cp",
|
||||
System.getProperty("java.class.path"),
|
||||
Child.class.getName(),
|
||||
"wait"),
|
||||
Map.of()),
|
||||
x -> {});
|
||||
pending.cancel();
|
||||
assertTrue(pending.completion().toCompletableFuture().get(10, TimeUnit.SECONDS).cancelled());
|
||||
}
|
||||
|
||||
public static class Child {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length > 0) Thread.sleep(30000);
|
||||
System.out.println("normal output");
|
||||
System.err.println("error output");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user