From d098a4729a39a521643022cd46ab33226f64f414 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Tue, 3 Nov 2026 22:29:40 -0600 Subject: [PATCH] test(lambda): trigger scheduled repair callbacks deterministically (#7477) --- .../lambda/ProvisionedConcurrencyPool.java | 13 +++++++++++-- .../ProvisionedConcurrencyPoolTest.java | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java b/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java index 8e9a053308..81005cd8d5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java @@ -69,8 +69,7 @@ public class ProvisionedConcurrencyPool implements Resettable { private final Set pendingTeardown = new HashSet<>(); private final Map stopping = new HashMap<>(); private final ExecutorService workers = Executors.newVirtualThreadPerTaskExecutor(); - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor( - Thread.ofPlatform().daemon().name("lambda-provisioned-concurrency").factory()); + private final ScheduledExecutorService scheduler; private boolean stopped; private int activeLaunches; private long shutdownDeadlineNanos; @@ -121,6 +120,15 @@ public class ProvisionedConcurrencyPool implements Resettable { ProvisionedConcurrencyPool(LambdaRuntimeLauncher launcher, LambdaFunctionStore functions, LambdaAliasStore aliases, StorageBackend storage, Clock clock, int maxPerFunction, int maxTotal, boolean ephemeral, int drainSeconds) { + this(launcher, functions, aliases, storage, clock, maxPerFunction, maxTotal, ephemeral, + drainSeconds, Executors.newSingleThreadScheduledExecutor( + Thread.ofPlatform().daemon().name("lambda-provisioned-concurrency").factory())); + } + + ProvisionedConcurrencyPool(LambdaRuntimeLauncher launcher, LambdaFunctionStore functions, + LambdaAliasStore aliases, StorageBackend storage, + Clock clock, int maxPerFunction, int maxTotal, boolean ephemeral, int drainSeconds, + ScheduledExecutorService scheduler) { this.launcher = launcher; this.functions = functions; this.aliases = aliases; @@ -130,6 +138,7 @@ public class ProvisionedConcurrencyPool implements Resettable { this.maxTotal = maxTotal; this.ephemeral = ephemeral; this.drainTimeoutNanos = TimeUnit.SECONDS.toNanos(drainSeconds); + this.scheduler = scheduler; } @PostConstruct diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java index b8340615ac..9d125c6cbe 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; import java.nio.file.Path; import java.time.Clock; @@ -37,6 +38,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; @@ -773,17 +775,28 @@ class ProvisionedConcurrencyPoolTest { LambdaAliasStore aliasLookup = spy(aliases); doThrow(new IllegalStateException("controlled scheduler lookup outage")) .when(aliasLookup).getForAccount(ACCOUNT, REGION, "example", "live"); + ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); pool = new ProvisionedConcurrencyPool(launcher, functions, aliasLookup, - storage, Clock.systemUTC(), 4, 8, false, 15); + storage, Clock.systemUTC(), 4, 8, false, 15, scheduler); pool.start(); - await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> - verify(aliasLookup, atLeast(2)).getForAccount(ACCOUNT, REGION, "example", "live")); + ArgumentCaptor repair = ArgumentCaptor.forClass(Runnable.class); + verify(scheduler).scheduleWithFixedDelay(repair.capture(), eq(0L), eq(1L), eq(TimeUnit.SECONDS)); + repair.getValue().run(); + await().atMost(Duration.ofSeconds(3)).untilAsserted(() -> + verify(aliasLookup, times(1)).getForAccount(ACCOUNT, REGION, "example", "live")); + repair.getValue().run(); + await().atMost(Duration.ofSeconds(3)).untilAsserted(() -> + verify(aliasLookup, times(2)).getForAccount(ACCOUNT, REGION, "example", "live")); doCallRealMethod().when(aliasLookup).getForAccount(ACCOUNT, REGION, "example", "live"); + repair.getValue().run(); + await().atMost(Duration.ofSeconds(3)).until(() -> !launcher.launched.isEmpty()); Environment replacement = launched(); replacement.initialized.complete(null); ready(); assertEquals("READY", get().status()); assertEquals(1, first.stops.get()); + pool.shutdown(); + verify(scheduler).shutdownNow(); } @Test