From a4d609a37a8f3cb04aa85eabcb559d51e17cffe1 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Fri, 30 Oct 2026 10:57:07 -0500 Subject: [PATCH] test(port): reuse shared start gate for Docker allocations (#7013) * test(docker): bound concurrent port allocation workers Propagate worker failures, synchronize allocation admission under one monotonic deadline, and retain cleanup failures without hiding the primary cause. Preserve all existing allocator scenarios and the 20-worker uniqueness check. Signed-off-by: Andre Kurait * test(port): reuse shared start gate for allocation workers Use the existing StartGate helper to release allocation tasks together, collect worker errors and bound executor shutdown. Remove the duplicate runner and its eight helper-only tests, preserving all eight allocator scenarios. Signed-off-by: Andre Kurait * test(port): restore all-worker readiness barrier Wait for all 20 allocation tasks at a timed CyclicBarrier before allocating. Keep StartGate, collected worker failure assertions and reservation cleanup unchanged. Address upstream review5714078242. Signed-off-by: Andre Kurait --------- Signed-off-by: Andre Kurait --- .../core/common/docker/PortAllocatorTest.java | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/PortAllocatorTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/PortAllocatorTest.java index 5121b6a3eb..bb6453e59e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/docker/PortAllocatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/PortAllocatorTest.java @@ -1,16 +1,17 @@ package io.github.hectorvent.floci.core.common.docker; +import io.github.hectorvent.floci.testing.StartGate; import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import static org.junit.jupiter.api.Assertions.*; @@ -50,37 +51,28 @@ class PortAllocatorTest { void concurrentAllocationsAreUnique() throws Exception { PortAllocator allocator = new PortAllocator(); int threads = 20; - // Range must be at least as wide as the thread count int base = 19900; int max = base + threads - 1; - Set ports = ConcurrentHashMap.newKeySet(); - CountDownLatch ready = new CountDownLatch(threads); - CountDownLatch go = new CountDownLatch(1); - ExecutorService executor = Executors.newFixedThreadPool(threads); - List> futures = new ArrayList<>(); - - for (int i = 0; i < threads; i++) { - futures.add(executor.submit(() -> { - ready.countDown(); - try { go.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - int p = allocator.allocate(base, max); - ports.add(p); - })); + CyclicBarrier ready = new CyclicBarrier(threads); + try { + StartGate.Results results = StartGate.runConcurrently(threads, index -> { + try { + ready.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError("Allocation worker interrupted before all workers were ready", e); + } catch (BrokenBarrierException | TimeoutException e) { + throw new AssertionError("Allocation workers did not all reach the readiness barrier", e); + } + return ports.add(allocator.allocate(base, max)); + }); + assertTrue(results.errors().isEmpty(), () -> "Allocation workers failed: " + results.errors()); + assertEquals(threads, ports.size(), + "Concurrent allocations must all return unique ports; got duplicates: " + ports); + } finally { + ports.forEach(allocator::release); } - - ready.await(); // all threads are lined up - go.countDown(); // release them simultaneously - - for (Future f : futures) { - f.get(); - } - executor.shutdown(); - - assertEquals(threads, ports.size(), - "Concurrent allocations must all return unique ports; got duplicates: " + ports); - - ports.forEach(allocator::release); } @Test @@ -146,4 +138,5 @@ class PortAllocatorTest { assertEquals(List.of(19900), tried); assertEquals(19900, allocator.allocate(19900, 19901), "the chosen port must be released"); } + }