From 80eee041d5dc747397120ecfb4f8777fd7a1895e Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Tue, 3 Nov 2026 06:36:09 -0600 Subject: [PATCH] test(ec2): clean up failed concurrent image workers (#7374) --- ...DeregisterAndCopyImageIntegrationTest.java | 43 +++--------- .../hectorvent/floci/testing/StartGate.java | 31 ++++++++- .../floci/testing/StartGateTest.java | 68 +++++++++++++++++++ 3 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/testing/StartGateTest.java diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java index 2d12e5840b..ecf6428a6c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DeregisterAndCopyImageIntegrationTest.java @@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.ec2.model.Image; import io.github.hectorvent.floci.services.ec2.model.Instance; import io.github.hectorvent.floci.services.ec2.model.NetworkInterface; import io.github.hectorvent.floci.services.ec2.model.Tag; +import io.github.hectorvent.floci.testing.StartGate; import io.quarkus.test.junit.QuarkusTest; import io.restassured.specification.RequestSpecification; import jakarta.inject.Inject; @@ -20,11 +21,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.UUID; -import java.util.concurrent.Callable; -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 java.util.concurrent.atomic.AtomicReference; @@ -548,7 +544,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest { for (int round = 0; round < RACE_ROUNDS; round++) { String name = uniqueName("floci-race-dst"); AtomicInteger duplicates = new AtomicInteger(); - List created = runConcurrently(RACE_THREADS, () -> { + StartGate.Results created = StartGate.runConcurrently(RACE_THREADS, index -> { try { return service.copyImage(WEST, EAST, source.getImageId(), name, null, false, List.of()); } catch (AwsException e) { @@ -558,7 +554,8 @@ class Ec2DeregisterAndCopyImageIntegrationTest { } }); - List winners = created.stream().filter(Objects::nonNull).toList(); + assertTrue(created.errors().isEmpty(), () -> "copy workers failed: " + created.errors()); + List winners = created.values().stream().filter(Objects::nonNull).toList(); assertEquals(1, winners.size(), "exactly one copy may claim the name; round " + round + " produced " + winners.size()); assertEquals(RACE_THREADS - 1, duplicates.get()); @@ -581,7 +578,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest { AtomicInteger successes = new AtomicInteger(); AtomicInteger unavailable = new AtomicInteger(); - runConcurrently(RACE_THREADS, () -> { + StartGate.Results results = StartGate.runConcurrently(RACE_THREADS, index -> { try { service.deregisterImage(EAST, image.getImageId(), true); successes.incrementAndGet(); @@ -592,6 +589,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest { return null; }); + assertTrue(results.errors().isEmpty(), () -> "deregistration workers failed: " + results.errors()); assertEquals(1, successes.get(), "round " + round + " deregistered the same AMI twice"); assertEquals(RACE_THREADS - 1, unavailable.get()); assertTrue(service.describeImages(EAST, List.of(image.getImageId()), List.of()).isEmpty()); @@ -606,12 +604,12 @@ class Ec2DeregisterAndCopyImageIntegrationTest { "x86_64", "/dev/xvda", null); String prefix = uniqueName("floci-fanout-dst"); - AtomicInteger seq = new AtomicInteger(); - List copies = runConcurrently(24, - () -> service.copyImage(WEST, EAST, source.getImageId(), - prefix + "-" + seq.getAndIncrement(), null, false, List.of())); + StartGate.Results copies = StartGate.runConcurrently(24, + index -> service.copyImage(WEST, EAST, source.getImageId(), + prefix + "-" + index, null, false, List.of())); - Set ids = copies.stream().map(Image::getImageId).collect(Collectors.toSet()); + assertTrue(copies.errors().isEmpty(), () -> "copy workers failed: " + copies.errors()); + Set ids = copies.values().stream().map(Image::getImageId).collect(Collectors.toSet()); assertEquals(24, ids.size(), "every concurrent copy must get a distinct id"); assertEquals(24, service.describeImages(WEST, List.of(), List.of("self")).stream() .filter(img -> img.getName().startsWith(prefix)) @@ -636,25 +634,6 @@ class Ec2DeregisterAndCopyImageIntegrationTest { "x86_64", "/dev/xvda", null); } - private List runConcurrently(int threads, Callable body) throws Exception { - ExecutorService pool = Executors.newFixedThreadPool(threads); - CountDownLatch start = new CountDownLatch(1); - List> futures = new ArrayList<>(); - for (int i = 0; i < threads; i++) { - futures.add(pool.submit(() -> { - start.await(); - return body.call(); - })); - } - start.countDown(); - List results = new ArrayList<>(); - for (Future future : futures) { - results.add(future.get(30, TimeUnit.SECONDS)); - } - pool.shutdown(); - return results; - } - /** * A DeregisterImage landing after a launch's first tombstone check must leave nothing behind. * The rejection used to arrive only once the launch had already persisted its root volume, its diff --git a/src/test/java/io/github/hectorvent/floci/testing/StartGate.java b/src/test/java/io/github/hectorvent/floci/testing/StartGate.java index 8f224eaf41..f204485d82 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/StartGate.java +++ b/src/test/java/io/github/hectorvent/floci/testing/StartGate.java @@ -33,6 +33,7 @@ public final class StartGate { CountDownLatch doneGate = new CountDownLatch(threadCount); Object[] values = new Object[threadCount]; List errors = Collections.synchronizedList(new ArrayList<>()); + Throwable failure = null; try { for (int i = 0; i < threadCount; i++) { int index = i; @@ -50,10 +51,34 @@ public final class StartGate { startGate.countDown(); assertTrue(doneGate.await(30, TimeUnit.SECONDS), "concurrent tasks did not complete within 30s"); + } catch (InterruptedException | RuntimeException | Error e) { + failure = e; + throw e; } finally { - pool.shutdownNow(); - assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS), - "thread pool did not terminate"); + try { + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS), + "thread pool did not terminate"); + } catch (InterruptedException | RuntimeException | Error cleanupFailure) { + if (failure == null) { + failure = cleanupFailure; + throw cleanupFailure; + } + failure.addSuppressed(cleanupFailure); + if (cleanupFailure instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + } finally { + if (failure != null) { + synchronized (errors) { + for (Throwable workerFailure : errors) { + if (workerFailure != failure) { + failure.addSuppressed(workerFailure); + } + } + } + } + } } @SuppressWarnings("unchecked") List results = (List) Arrays.asList(values); diff --git a/src/test/java/io/github/hectorvent/floci/testing/StartGateTest.java b/src/test/java/io/github/hectorvent/floci/testing/StartGateTest.java new file mode 100644 index 0000000000..536df5ebaf --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/StartGateTest.java @@ -0,0 +1,68 @@ +package io.github.hectorvent.floci.testing; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.*; + +class StartGateTest { + + @Test + void interruptedWaitRetainsWorkerFailure() throws Exception { + AssertionError workerFailure = new AssertionError("worker failed during interrupted wait"); + CountDownLatch entered = new CountDownLatch(2); + CountDownLatch release = new CountDownLatch(1); + FutureTask> operation = new FutureTask<>( + () -> StartGate.runConcurrently(2, index -> { + entered.countDown(); + if (index == 0) { + throw workerFailure; + } + awaitRelease(release); + return index; + })); + Thread caller = new Thread(operation, "start-gate-interrupted-caller"); + caller.start(); + try { + assertTrue(entered.await(5, TimeUnit.SECONDS)); + caller.interrupt(); + ExecutionException failure = assertThrows(ExecutionException.class, + () -> operation.get(10, TimeUnit.SECONDS)); + assertInstanceOf(InterruptedException.class, failure.getCause()); + assertTrue(Arrays.asList(failure.getCause().getSuppressed()).contains(workerFailure)); + } finally { + release.countDown(); + caller.interrupt(); + caller.join(10_000); + assertFalse(caller.isAlive()); + } + } + + @Test + void completedWorkersStillReturnValuesAndErrors() throws InterruptedException { + AssertionError workerFailure = new AssertionError("completed failure"); + StartGate.Results result = StartGate.runConcurrently(3, index -> { + if (index == 1) { + throw workerFailure; + } + return index; + }); + assertEquals(Arrays.asList(0, null, 2), result.values()); + assertEquals(1, result.errors().size()); + assertSame(workerFailure, result.errors().getFirst()); + } + + private static void awaitRelease(CountDownLatch release) { + try { + release.await(); + } catch (InterruptedException expected) { + // StartGate must cancel blocked workers after timeout or caller interruption. + Thread.currentThread().interrupt(); + } + } +}