implement real headless process gateway and supervision
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package p.studio.app.runtime;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.automation.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class HeadlessProcessSessionTest {
|
||||
@TempDir Path root;
|
||||
static final HeadlessLimits LIMITS = new HeadlessLimits(600, 4096, 4096, 1000, 500, 150);
|
||||
final List<HeadlessProcessSession> sessions = new ArrayList<>();
|
||||
HeadlessProcessSession start(String mode) throws Exception {
|
||||
var process = new ProcessBuilder(HeadlessFixture.java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), mode).directory(root.toFile()).start();
|
||||
var session = new HeadlessProcessSession(process, new ObjectMapper(), LIMITS, "build"); sessions.add(session); return session;
|
||||
}
|
||||
@AfterEach void cleanup() { sessions.forEach(session -> session.abort("test cleanup")); }
|
||||
@Test void fragmentedProtocolPreservesUnsignedFrameIdentityLayoutAndSeparateStderr() throws Exception {
|
||||
var session = start("normal"); session.initializeAndBoot(root);
|
||||
var outcome = session.advance(3, ignored -> {});
|
||||
assertEquals(AutomationOutcome.Kind.SUCCEEDED, outcome.kind());
|
||||
assertEquals("18446744073709551615", outcome.capture().logicalFrameIndex().toString());
|
||||
assertEquals("9223372036854775808", outcome.capture().frameId().toString());
|
||||
assertEquals(3, outcome.capture().stridePixels());
|
||||
assertTrue(outcome.diagnostics().stream().anyMatch(s -> s.contains("fixture stderr")));
|
||||
var wire = p.studio.app.mcp.McpToolResult.capture(new ObjectMapper(), outcome.capture(), false).wire();
|
||||
assertEquals(outcome.capture().frameId(), wire.path("structuredContent").path("value").path("frameId").bigIntegerValue());
|
||||
session.shutdown().toCompletableFuture().get(3, TimeUnit.SECONDS); assertFalse(session.alive());
|
||||
}
|
||||
@Test void rejectsBadHandshakeMalformedOversizedAndNonUtf8OutputAndCleansUp() throws Exception {
|
||||
for (var mode : List.of("major", "capabilities", "limits", "malformed", "oversized", "utf8", "wrong-session", "no-hello")) {
|
||||
var session = start(mode);
|
||||
assertThrows(Exception.class, () -> session.initializeAndBoot(root), mode);
|
||||
session.abort("startup rejection"); assertFalse(session.alive(), mode);
|
||||
}
|
||||
}
|
||||
@Test void crashRetainsOnlyObservedProgressAndNeverRestartsOrReplays() throws Exception {
|
||||
var session = start("crash"); session.initializeAndBoot(root);
|
||||
var known = new AtomicReference<TrackedOperation.Progress>();
|
||||
assertThrows(AutomationException.class, () -> session.advance(30, known::set));
|
||||
assertEquals(2L, known.get().completedFrames()); assertFalse(session.alive());
|
||||
assertThrows(AutomationException.class, () -> session.call(TaskTools.HeadlessAction.STATE, Map.of()));
|
||||
}
|
||||
@Test void deadlinesStopHungCallsAndCancellationPreservesKnownPartialProgress() throws Exception {
|
||||
var call = start("hang-call"); call.initializeAndBoot(root);
|
||||
assertThrows(AutomationException.class, () -> call.call(TaskTools.HeadlessAction.STATE, Map.of())); assertFalse(call.alive());
|
||||
var session = start("hang-cancel"); session.initializeAndBoot(root);
|
||||
var known = new AtomicReference<TrackedOperation.Progress>();
|
||||
assertThrows(AutomationException.class, () -> session.advance(30, known::set));
|
||||
assertEquals(2L, known.get().completedFrames()); assertFalse(session.alive());
|
||||
}
|
||||
@Test void cooperativeCancelReturnsFinalImageAndRejectsConcurrentWork() throws Exception {
|
||||
var session = start("cooperative"); session.initializeAndBoot(root);
|
||||
var entered = new CountDownLatch(1);
|
||||
try (var workers = Executors.newSingleThreadExecutor()) {
|
||||
var result = workers.submit(() -> session.advance(30, ignored -> entered.countDown()));
|
||||
assertTrue(entered.await(2, TimeUnit.SECONDS));
|
||||
assertEquals(AutomationFailure.Code.BUSY, assertThrows(AutomationException.class,
|
||||
() -> session.call(TaskTools.HeadlessAction.STATE, Map.of())).failure().code());
|
||||
session.cancel();
|
||||
var outcome = result.get(2, TimeUnit.SECONDS);
|
||||
assertEquals(AutomationOutcome.Kind.CANCELLED, outcome.kind()); assertEquals(2, outcome.completedFrames()); assertNotNull(outcome.capture());
|
||||
}
|
||||
session.shutdown().toCompletableFuture().get(3, TimeUnit.SECONDS); assertFalse(session.alive());
|
||||
}
|
||||
@Test void malformedProgressAndCaptureInvalidateSession() throws Exception {
|
||||
for (var mode : List.of("bad-progress", "bad-png")) {
|
||||
var session = start(mode); session.initializeAndBoot(root);
|
||||
assertThrows(AutomationException.class, () -> session.advance(3, ignored -> {})); assertFalse(session.alive());
|
||||
}
|
||||
}
|
||||
@Test void shutdownEscalatesAndKillsOwnedDescendantsWithoutTouchingUnrelatedProcesses() throws Exception {
|
||||
var unrelated = new ProcessBuilder(HeadlessFixture.java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), "child").start();
|
||||
try {
|
||||
var session = start("descendant"); session.initializeAndBoot(root);
|
||||
long child = Long.parseLong(Files.readString(root.resolve("descendant.pid")));
|
||||
assertTrue(ProcessHandle.of(child).orElseThrow().isAlive());
|
||||
assertThrows(ExecutionException.class, () -> session.shutdown().toCompletableFuture().get(4, TimeUnit.SECONDS));
|
||||
assertFalse(session.alive()); assertFalse(ProcessHandle.of(child).map(ProcessHandle::isAlive).orElse(false));
|
||||
assertTrue(unrelated.isAlive());
|
||||
} finally { unrelated.destroyForcibly(); unrelated.waitFor(2, TimeUnit.SECONDS); }
|
||||
}
|
||||
@Test void missingConfiguredInstallationIsExplicitAndNeverDiscoversAnotherRuntime() throws Exception {
|
||||
var project = new p.studio.api.project.ProjectReference("game", "1", "pbs", 1, p.studio.api.project.ProjectTarget.Game, root);
|
||||
var build = p.studio.api.shipper.ShipperPrepareResult.failed(List.of(), List.of());
|
||||
var missing = new HeadlessProcessGateway(new ObjectMapper(), ignored -> p.studio.api.project.ProjectLocalStudioSetup.defaults());
|
||||
assertEquals(AutomationFailure.Code.RUNTIME_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> missing.boot(project, "build", build)).failure().code());
|
||||
var unavailable = new HeadlessProcessGateway(new ObjectMapper(), ignored -> new p.studio.api.project.ProjectLocalStudioSetup(root.toString(), null));
|
||||
assertEquals(AutomationFailure.Code.RUNTIME_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> unavailable.boot(project, "build", build)).failure().code());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user