test(swf): restrict timeout sweeper configuration fixture (#4480)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-26 13:20:36 -04:00
1 parent 6c868f32f2
commit 2dafbb45b2
1 file changed
+34 -31
@@ -1,20 +1,22 @@
package io.github.hectorvent.floci.services.swf;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.EmulatorConfig.ServicesConfig;
import io.github.hectorvent.floci.config.EmulatorConfig.SwfServiceConfig;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
/**
* The sweep interval reaches {@code scheduleWithFixedDelay}, which rejects a non-positive
* delay. Because the schedule happens in a {@link StartupEvent} observer, an
* IllegalArgumentException there stops the whole emulator from becoming ready — so a
* IllegalArgumentException there stops the whole emulator from becoming ready, so a
* misconfigured interval must not be passed through unchecked.
*/
class SwfTimeoutSweeperTest {
@@ -25,7 +27,7 @@ class SwfTimeoutSweeperTest {
SwfTimeoutSweeper sweeper = new SwfTimeoutSweeper(null, configWithInterval(interval));
assertDoesNotThrow(() -> sweeper.onStart(new StartupEvent()),
"interval " + interval + " must not abort startup");
sweeper.onStop(new io.quarkus.runtime.ShutdownEvent());
sweeper.onStop(new ShutdownEvent());
}
}
@@ -33,32 +35,33 @@ class SwfTimeoutSweeperTest {
void positiveConfiguredInterval_startsNormally() {
SwfTimeoutSweeper sweeper = new SwfTimeoutSweeper(null, configWithInterval(30L));
assertDoesNotThrow(() -> sweeper.onStart(new StartupEvent()));
sweeper.onStop(new io.quarkus.runtime.ShutdownEvent());
sweeper.onStop(new ShutdownEvent());
}
/**
* Minimal {@link EmulatorConfig} view: the sweeper reads only the three SWF settings, so
* a proxy answering those avoids standing up the Quarkus config container.
*/
private static EmulatorConfig configWithInterval(long intervalSeconds) {
Map<String, Object> answers = Map.of(
"enabled", Boolean.TRUE,
"timeoutSweepEnabled", Boolean.TRUE,
"timeoutSweepIntervalSeconds", intervalSeconds);
@Test
void configFixtureRejectsOtherServicePaths() {
EmulatorConfig config = configWithInterval(30L);
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 swf() 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);
AssertionError otherService = assertThrows(AssertionError.class, () -> config.services().ecs());
assertTrue(otherService.getMessage().contains("ecs"));
assertThrows(AssertionError.class, () -> config.services().rds());
}
private static EmulatorConfig configWithInterval(long intervalSeconds) {
EmulatorConfig config = configView(EmulatorConfig.class);
ServicesConfig services = configView(ServicesConfig.class);
SwfServiceConfig swf = configView(SwfServiceConfig.class);
doReturn(services).when(config).services();
doReturn(swf).when(services).swf();
doReturn(true).when(swf).enabled();
doReturn(true).when(swf).timeoutSweepEnabled();
doReturn(intervalSeconds).when(swf).timeoutSweepIntervalSeconds();
return config;
}
private static <T> T configView(Class<T> configType) {
return mock(configType, invocation -> {
throw new AssertionError("Unexpected configuration access: " + invocation.getMethod());
});
}
}