mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
perf: restart without a fixed pause and wait no longer than --timeout (#36)
This commit is contained in:
1 parent
fc67e72a62
commit
01d6133548
9 files changed
+407
-34
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledOnOs;
|
||||
import org.junit.jupiter.api.condition.OS;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** An abandoned docker call must not leave its process running after the CLI moves on. */
|
||||
class DockerClientProcessTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
@DisabledOnOs(OS.WINDOWS)
|
||||
void anInterruptedCallKillsItsDockerProcess() throws Exception {
|
||||
Path pidFile = tempDir.resolve("pid");
|
||||
Path fakeDocker = tempDir.resolve("docker");
|
||||
Files.writeString(fakeDocker, "#!/bin/sh\necho $$ > '" + pidFile + "'\nexec sleep 30\n");
|
||||
assertTrue(fakeDocker.toFile().setExecutable(true));
|
||||
DockerClient docker = new DockerClient(fakeDocker.toString());
|
||||
|
||||
Thread call = Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
docker.inspectContainer("floci");
|
||||
} catch (DockerException expected) {
|
||||
// "Interrupted"
|
||||
}
|
||||
});
|
||||
long pid = awaitPid(pidFile);
|
||||
|
||||
call.interrupt();
|
||||
call.join(5_000);
|
||||
|
||||
assertFalse(call.isAlive(), "the call should return once interrupted");
|
||||
long deadline = System.currentTimeMillis() + 3_000;
|
||||
while (isAlive(pid) && System.currentTimeMillis() < deadline) {
|
||||
Thread.sleep(50);
|
||||
}
|
||||
assertFalse(isAlive(pid), "docker process " + pid + " outlived the interrupted call");
|
||||
}
|
||||
|
||||
private static long awaitPid(Path pidFile) throws Exception {
|
||||
long deadline = System.currentTimeMillis() + 5_000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (Files.exists(pidFile)) {
|
||||
String text = Files.readString(pidFile).trim();
|
||||
if (!text.isEmpty()) return Long.parseLong(text);
|
||||
}
|
||||
Thread.sleep(20);
|
||||
}
|
||||
throw new AssertionError("fake docker never started");
|
||||
}
|
||||
|
||||
private static boolean isAlive(long pid) {
|
||||
return ProcessHandle.of(pid).map(ProcessHandle::isAlive).orElse(false);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user