diff --git a/src/test/java/io/floci/aws/services/elb/ElbClassicServiceTest.java b/src/test/java/io/floci/aws/services/elb/ElbClassicServiceTest.java index eb04f02d7f..3f07ca9b82 100644 --- a/src/test/java/io/floci/aws/services/elb/ElbClassicServiceTest.java +++ b/src/test/java/io/floci/aws/services/elb/ElbClassicServiceTest.java @@ -32,6 +32,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -266,34 +267,58 @@ class ElbClassicServiceTest { https.setSslCertificateId("arn:aws:iam::000000000000:server-certificate/scanned"); ExecutorService pool = Executors.newFixedThreadPool(2); - CountDownLatch start = new CountDownLatch(1); + // Started together, the writer could finish before the scanner first ran. So the scanner + // scans once before the writes start and then until they end, and the writer keeps writing + // until it starts a create and delete pair after two scans have finished since its first + // write. The second of those scans started after that write and finished before the last + // pair, so one whole scan runs while the listeners are written, however the threads are + // scheduled. + CountDownLatch scannedOnce = new CountDownLatch(1); + AtomicInteger scans = new AtomicInteger(); AtomicBoolean writing = new AtomicBoolean(true); try { - Future writer = pool.submit(() -> { - start.await(); + Future scanner = pool.submit(() -> { try { - for (int i = 0; i < 400; i++) { + service.serverCertificateReferences(); + scans.incrementAndGet(); + } finally { + // Released even when the first scan fails, so the failure comes out of this + // future rather than as a writer left waiting. + scannedOnce.countDown(); + } + while (writing.get()) { + service.serverCertificateReferences(); + scans.incrementAndGet(); + } + return null; + }); + Future writer = pool.submit(() -> { + try { + assertTrue(scannedOnce.await(30, TimeUnit.SECONDS), "the scan never started"); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(20); + int scansAtFirstWrite = -1; + int scansBeforePair; + int pairs = 0; + // A scanner that stopped on a failure scans no more, so the writes end after + // the usual 400 and the failure comes out of the scanner's future. + do { + assertTrue(System.nanoTime() < deadline, "the scanner made no progress for 20 s"); + scansBeforePair = scans.get(); service.createLoadBalancerListeners(REGION, "scanned-elb", List.of(https)); + if (pairs == 0) { + scansAtFirstWrite = scans.get(); + } service.deleteLoadBalancerListeners(REGION, "scanned-elb", List.of(443)); - } + pairs++; + } while (pairs < 400 || (scansBeforePair < scansAtFirstWrite + 2 && !scanner.isDone())); } finally { writing.set(false); } return null; }); - Future scanner = pool.submit(() -> { - start.await(); - int scans = 0; - while (writing.get()) { - service.serverCertificateReferences(); - scans++; - } - return scans; - }); - start.countDown(); writer.get(30, TimeUnit.SECONDS); - assertTrue(scanner.get(30, TimeUnit.SECONDS) > 0, "the scan never ran"); + scanner.get(30, TimeUnit.SECONDS); } finally { pool.shutdownNow(); }