Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codepipeline): fall back when the source poll interval is not positive (#4361)
Priamo Germosen5913812b53f7
diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.javaindex d84ba1a52a..fcaca77c63 100644--- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java+++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java@@ -103,7 +103,21 @@ public class CodePipelineService {LambdaService lambdaService, S3Service s3Service,EmulatorConfig config) {this(storageFactory, mapper, codeBuildService, codeDeployService, lambdaService, s3Service,- config.services().codepipeline().sourcePollIntervalMs());+ resolveSourcePollInterval(config.services().codepipeline().sourcePollIntervalMs()));+ }++ /**+ * A zero or negative interval would make {@code scheduleWithFixedDelay} throw from the startup+ * observer and stop the whole emulator becoming ready. A misconfigured value is not worth+ * failing every other service for, so it falls back with a warning.+ */+ private static long resolveSourcePollInterval(long configured) {+ if (configured > 0) {+ return configured;+ }+ LOG.warnv("Ignoring CodePipeline source poll interval {0}ms: must be a positive number of "+ + "milliseconds, falling back to {1}ms", configured, DEFAULT_SOURCE_POLL_INTERVAL_MS);+ return DEFAULT_SOURCE_POLL_INTERVAL_MS;}@SuppressWarnings("unchecked")diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.javaindex 50703e1790..f07f797f3f 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java@@ -40,11 +40,16 @@ class CodePipelineIntegrationTest {EmulatorConfig config;/**- * Blocks until the S3 source poller has had {@code cycles} chances to run. The assertions that- * follow these waits are all negative ("no execution was started"), so what matters is that the- * poller really did get its turns and declined, not that a fixed number of milliseconds passed.- * Deriving the wait from the configured interval keeps that guarantee whatever the interval is- * set to.+ * Sleeps for {@code cycles} times the configured source poll interval. The assertions that+ * follow these waits check that no execution, or no additional execution, was started, and they+ * are only meaningful if the poller actually ran and declined, so the wait scales with the+ * interval instead of hard-coding milliseconds that stop covering enough cycles when the+ * interval changes.+ *+ * <p>This is wall clock, not a handshake with the scheduler. The poller runs on a fixed delay,+ * so its period is the interval plus however long a pass takes, and fewer than {@code cycles}+ * passes may complete within the sleep. Several cycles are requested to leave margin for that,+ * not to guarantee a number of completed passes.*/private void awaitSourcePollCycles(int cycles) throws InterruptedException {Thread.sleep(config.services().codepipeline().sourcePollIntervalMs() * cycles);diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.javaindex db4c8aeef4..2b848db10f 100644--- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.codepipeline;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;@@ -16,6 +17,9 @@ import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.services.s3.model.S3Object;import org.junit.jupiter.api.Test;+import java.lang.reflect.InvocationHandler;+import java.lang.reflect.Method;+import java.lang.reflect.Proxy;import java.util.Map;import java.util.concurrent.CompletableFuture;import java.util.concurrent.CountDownLatch;@@ -23,6 +27,7 @@ import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicReference;+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertThrows;@@ -48,6 +53,54 @@ class CodePipelineServiceTest {mock(LambdaService.class),mock(S3Service.class));+ /**+ * The configured interval reaches {@code scheduleWithFixedDelay}, which rejects a non-positive+ * delay. That schedule happens in {@code @PostConstruct}, so an IllegalArgumentException there+ * would leave CodePipeline unavailable rather than merely mis-scheduled.+ */+ @Test+ void nonPositiveConfiguredPollInterval_doesNotPreventStartup() {+ for (long interval : new long[] {0L, -1L, Long.MIN_VALUE}) {+ CodePipelineService configured = new CodePipelineService(+ new InMemoryStorageFactory(),+ mapper,+ mock(CodeBuildService.class),+ mock(CodeDeployService.class),+ mock(LambdaService.class),+ mock(S3Service.class),+ configWithPollInterval(interval));+ try {+ assertDoesNotThrow(configured::resumePersistedExecutions,+ "interval " + interval + " must not abort startup");+ } finally {+ configured.shutdown();+ }+ }+ }++ /**+ * Minimal {@link EmulatorConfig} view: the constructor reads only the source poll interval, so+ * a proxy answering that avoids standing up the Quarkus config container.+ */+ private static EmulatorConfig configWithPollInterval(long intervalMs) {+ Map<String, Object> answers = Map.of("sourcePollIntervalMs", intervalMs);++ InvocationHandler handler = new InvocationHandler() {+ @Override+ public Object invoke(Object proxy, Method method, Object[] args) {+ Object answer = answers.get(method.getName());+ if (answer != null) {+ return answer;+ }+ // services() and codepipeline() return further config views; proxy those too.+ return Proxy.newProxyInstance(method.getReturnType().getClassLoader(),+ new Class<?>[] {method.getReturnType()}, this);+ }+ };+ return (EmulatorConfig) Proxy.newProxyInstance(EmulatorConfig.class.getClassLoader(),+ new Class<?>[] {EmulatorConfig.class}, handler);+ }+@Testvoid getPipelineTreatsMissingStoredVersionAsVersionOne() throws Exception {CapturingStorageFactory storageFactory = new CapturingStorageFactory();