mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
perf(doctor): run checks concurrently and share docker lookups (#32)
This commit is contained in:
1 parent
3c4118ff1a
commit
dd38f6dc27
20 files changed
+623
-51
No files matched your search
@@ -0,0 +1,195 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.DoctorCommand;
|
||||
import io.floci.cli.docker.CachingDockerClient;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.docker.DockerException;
|
||||
import io.floci.cli.doctor.Check;
|
||||
import io.floci.cli.doctor.CheckResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** BL-028: doctor selects before running, survives a crashing check, and keeps its output order. */
|
||||
class DoctorCommandTest {
|
||||
|
||||
/** No docker subprocess: the endpoint lookup sees no container. */
|
||||
private static final DockerClient NO_DOCKER = new DockerClient() {
|
||||
@Override
|
||||
public Optional<ContainerInfo> inspectContainer(String name) {
|
||||
return Optional.empty();
|
||||
}
|
||||
};
|
||||
|
||||
private record Named(String name, AtomicInteger runs, Check body) implements Check {
|
||||
Named(String name, Check body) {
|
||||
this(name, new AtomicInteger(), body);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CheckResult run(String endpoint, String container) {
|
||||
runs.incrementAndGet();
|
||||
return body.run(endpoint, container);
|
||||
}
|
||||
}
|
||||
|
||||
private static Named ok(String name) {
|
||||
return new Named(name, (e, c) -> CheckResult.ok(name, "fine"));
|
||||
}
|
||||
|
||||
private record Run(int exit, String out, String err) {}
|
||||
|
||||
private static Run doctor(List<Check> checks, String... args) {
|
||||
CommandLine cmd = new CommandLine(new DoctorCommand(ProductProfile.AWS, NO_DOCKER, checks));
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(outBuf));
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
try {
|
||||
return new Run(cmd.execute(args), outBuf.toString(), errBuf.toString());
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkOptionRunsOnlyTheNamedCheck() {
|
||||
Named a = ok("a.check");
|
||||
Named b = ok("b.check");
|
||||
|
||||
Run r = doctor(List.of(a, b), "--check", "b.check");
|
||||
|
||||
assertEquals(0, r.exit(), r.err());
|
||||
assertEquals(0, a.runs().get());
|
||||
assertEquals(1, b.runs().get());
|
||||
assertTrue(r.out().contains("b.check"), r.out());
|
||||
assertFalse(r.out().contains("a.check"), r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownCheckNameExitsTwoAndListsTheNames() {
|
||||
Run r = doctor(List.of(ok("a.check"), ok("b.check")), "--check", "nope");
|
||||
|
||||
assertEquals(2, r.exit());
|
||||
assertTrue(r.err().contains("Unknown check 'nope'"), r.err());
|
||||
assertTrue(r.err().contains("a.check, b.check"), r.err());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCrashingCheckIsReportedAndTheOthersStillRun() {
|
||||
Named boom = new Named("boom.check", (e, c) -> { throw new IllegalStateException("kaput"); });
|
||||
Named after = ok("after.check");
|
||||
|
||||
Run r = doctor(List.of(boom, after), "-o", "json");
|
||||
|
||||
assertEquals(1, after.runs().get());
|
||||
assertTrue(r.out().contains("\"boom.check\""), r.out());
|
||||
assertTrue(r.out().contains("check failed: kaput"), r.out());
|
||||
assertTrue(r.out().contains("\"after.check\""), r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultsKeepListOrderEvenWhenLaterChecksFinishFirst() {
|
||||
CountDownLatch secondDone = new CountDownLatch(1);
|
||||
Named slow = new Named("first.check", (e, c) -> {
|
||||
try {
|
||||
// Finishes only after the second check has completed.
|
||||
assertTrue(secondDone.await(5, TimeUnit.SECONDS));
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return CheckResult.ok("first.check", "slow");
|
||||
});
|
||||
Named fast = new Named("second.check", (e, c) -> {
|
||||
secondDone.countDown();
|
||||
return CheckResult.ok("second.check", "fast");
|
||||
});
|
||||
|
||||
Run r = doctor(List.of(slow, fast));
|
||||
|
||||
// Both checks must have succeeded: a timed-out latch would surface as "check failed".
|
||||
assertEquals(0, r.exit(), r.out() + r.err());
|
||||
assertFalse(r.out().contains("check failed"), r.out());
|
||||
assertTrue(r.out().indexOf("first.check") < r.out().indexOf("second.check"), r.out());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCachingClientRunsEachLookupOnceAcrossThreads() throws Exception {
|
||||
AtomicInteger versionCalls = new AtomicInteger();
|
||||
AtomicInteger inspectCalls = new AtomicInteger();
|
||||
// Counts what reaches the delegate, i.e. what would spawn a docker subprocess.
|
||||
DockerClient counting = new DockerClient() {
|
||||
@Override
|
||||
public String dockerVersion() {
|
||||
versionCalls.incrementAndGet();
|
||||
sleepBriefly();
|
||||
return "27.0.0";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ContainerInfo> inspectContainer(String name) {
|
||||
inspectCalls.incrementAndGet();
|
||||
sleepBriefly();
|
||||
return Optional.empty();
|
||||
}
|
||||
};
|
||||
CachingDockerClient cache = new CachingDockerClient(counting);
|
||||
|
||||
// Futures, not bare threads: an assertion failing on a worker must fail this test.
|
||||
try (ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
List<Future<?>> workers = new ArrayList<>();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
workers.add(pool.submit(() -> {
|
||||
assertEquals("27.0.0", cache.dockerVersion());
|
||||
assertTrue(cache.inspectContainer("floci").isEmpty());
|
||||
return null;
|
||||
}));
|
||||
}
|
||||
for (Future<?> worker : workers) worker.get();
|
||||
}
|
||||
|
||||
assertEquals(1, versionCalls.get());
|
||||
assertEquals(1, inspectCalls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCachingClientRemembersFailuresToo() {
|
||||
AtomicInteger calls = new AtomicInteger();
|
||||
CachingDockerClient cache = new CachingDockerClient(new DockerClient() {
|
||||
@Override
|
||||
public String dockerVersion() throws DockerException {
|
||||
calls.incrementAndGet();
|
||||
throw new DockerException("daemon down");
|
||||
}
|
||||
});
|
||||
|
||||
assertThrows(DockerException.class, cache::dockerVersion);
|
||||
assertThrows(DockerException.class, cache::dockerVersion);
|
||||
assertEquals(1, calls.get());
|
||||
}
|
||||
|
||||
private static void sleepBriefly() {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.cli.doctor.CheckResult;
|
||||
import io.floci.cli.doctor.CheckStatus;
|
||||
import io.floci.cli.doctor.checks.EndpointReachableCheck;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EndpointReachableCheckTest {
|
||||
|
||||
private static CheckResult checkAgainst(int status, String body) throws IOException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/_floci/health", exchange -> {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
return new EndpointReachableCheck("/_floci")
|
||||
.run("http://127.0.0.1:" + server.getAddress().getPort(), "floci");
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsTheServerVersionFromItsHealthDocument() throws Exception {
|
||||
CheckResult r = checkAgainst(200, "{\"version\":\"9.9.9\",\"services\":[]}");
|
||||
|
||||
assertEquals(CheckStatus.ok, r.status());
|
||||
assertTrue(r.message().contains("v9.9.9"), r.message());
|
||||
}
|
||||
|
||||
/** A 200 with a body that is not JSON is still a reachable server. */
|
||||
@Test
|
||||
void aReachableServerWithAnUnreadableBodyStillPasses() throws Exception {
|
||||
CheckResult r = checkAgainst(200, "OK");
|
||||
|
||||
assertEquals(CheckStatus.ok, r.status(), r.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anErrorStatusFails() throws Exception {
|
||||
assertEquals(CheckStatus.fail, checkAgainst(503, "down").status());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user