fix(eventbridge): wait for the rate timer before delivery (#8617) (#8729)

* fix(eventbridge): wait for the rate timer before delivery
* test(eventbridge): drive scheduled SQL account checks from timer

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
jwoa95andAndre Kurait authored and GitHub committed 2026-11-17 06:55:47 -04:00
1 parent 65c6440b8f
commit 3e7ce122e7
3 files changed
+80 -3

No files matched your search

@@ -1,6 +1,8 @@
package io.floci.aws.services.eventbridge;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.eventbridge.model.RedshiftDataParameters;
import io.floci.aws.services.eventbridge.model.Rule;
@@ -9,22 +11,34 @@ import io.floci.aws.services.eventbridge.model.Target;
import io.floci.aws.services.redshiftdata.RedshiftDataService;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import jakarta.inject.Inject;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@QuarkusTest
class EventBridgeRedshiftDataAccountIntegrationTest {
@Inject
RuleScheduler scheduler;
EmulatorConfig config;
@Inject
ObjectMapper objectMapper;
@Inject
TargetDispatcher dispatcher;
@Inject
RegionResolver regionResolver;
@@ -65,9 +79,15 @@ class EventBridgeRedshiftDataAccountIntegrationTest {
}
target.setRedshiftDataParameters(parameters);
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = ArgumentCaptor.captor();
when(vertx.setPeriodic(eq(60_000L), fire.capture())).thenReturn(1L);
RuleScheduler scheduler = new RuleScheduler(vertx, config, objectMapper, dispatcher);
try {
scheduler.startScheduler(rule.getArn(), "rate(1 minute)",
() -> new RuleScheduler.ScheduleData(rule, List.of(target)));
assertNull(executionAccount.get());
fire.getValue().handle(1L);
assertEquals("111122223333", executionAccount.get());
assertEquals(previousAccount, regionResolver.getAccountId());
} finally {
@@ -170,6 +170,54 @@ class RuleSchedulerTest {
assertTrue(scheduler.isRunning(RULE_ARN), "the recreated rule must keep its timer");
}
@Test
void rateHasNoDeliveryBeforeTimer() {
Vertx vertx = mock(Vertx.class);
when(vertx.setPeriodic(anyLong(), any())).thenReturn(1L);
StoppingInvoker invoker = new StoppingInvoker(() -> { });
RuleScheduler scheduler = newScheduler(vertx, invoker);
scheduler.startScheduler(RULE_ARN, "rate(10 minutes)", () -> toScheduleData(enabledRule()));
assertEquals(0, invoker.invocationCount());
}
@Test
void restartingRateHasNoDeliveryBeforeTimer() {
Vertx vertx = mock(Vertx.class);
when(vertx.setPeriodic(anyLong(), any())).thenReturn(1L, 2L);
StoppingInvoker invoker = new StoppingInvoker(() -> { });
RuleScheduler scheduler = newScheduler(vertx, invoker);
scheduler.startScheduler(RULE_ARN, "rate(10 minutes)", () -> toScheduleData(enabledRule()));
scheduler.stopScheduler(RULE_ARN);
scheduler.startScheduler(RULE_ARN, "rate(10 minutes)", () -> toScheduleData(enabledRule()));
assertEquals(0, invoker.invocationCount());
}
@Test
void cronWaitsForTimerAndThenDelivers() {
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = timerHandlerCaptor();
when(vertx.setTimer(anyLong(), fire.capture())).thenReturn(1L, 2L);
StoppingInvoker invoker = new StoppingInvoker(() -> { });
RuleScheduler scheduler = newScheduler(vertx, invoker);
scheduler.startScheduler(RULE_ARN, EVERY_MINUTE_CRON, () -> toScheduleData(enabledRule()));
assertEquals(0, invoker.invocationCount());
fire.getValue().handle(1L);
assertEquals(1, invoker.invocationCount());
}
@Test
void rateTimerCallbackDeliversExactlyOnce() {
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = timerHandlerCaptor();
when(vertx.setPeriodic(anyLong(), fire.capture())).thenReturn(1L);
StoppingInvoker invoker = new StoppingInvoker(() -> { });
RuleScheduler scheduler = newScheduler(vertx, invoker);
scheduler.startScheduler(RULE_ARN, "rate(10 minutes)", () -> toScheduleData(enabledRule()));
int before = invoker.invocationCount();
fire.getValue().handle(1L);
assertEquals(before + 1, invoker.invocationCount());
}
@SuppressWarnings("unchecked")
private static ArgumentCaptor<Handler<Long>> timerHandlerCaptor() {
return ArgumentCaptor.forClass(Handler.class);