fix(eventbridge): emit whole-second scheduled event times (#9455)

Closes #9450

A scheduled rule's Scheduled Event now carries its time to the second, as PutEvents events have since #8020, read from the scheduler's clock so a test can pin it. RuleSchedulerTest covers a cron and a rate rule.
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-25 16:10:02 -04:00
1 parent 215e37eb48
commit be526ff56e
2 files changed
+35 -7

No files matched your search

@@ -14,8 +14,9 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Clock;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -43,8 +44,8 @@ public class RuleScheduler implements Resettable {
}
/**
* Test seam: lets cron next-fire computation be exercised deterministically instead
* of depending on wall-clock time. Production code always goes through the
* Test seam: lets cron next-fire computation and event time be exercised deterministically
* instead of depending on wall-clock time. Production code always goes through the
* public/{@code @Inject} constructor, which pins the system clock.
*/
RuleScheduler(Vertx vertx,
@@ -196,14 +197,13 @@ public class RuleScheduler implements Resettable {
private String buildScheduledEvent(Rule rule, String region) {
try {
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
ObjectNode node = objectMapper.createObjectNode();
node.put("version", "0");
node.put("id", UUID.randomUUID().toString());
node.put("source", "aws.events");
node.put("detail-type", "Scheduled Event");
node.put("account", rule.getAccountId() != null ? rule.getAccountId() : defaultAccountId);
node.put("time", now.toInstant().toString());
node.put("time", Instant.now(clock).truncatedTo(ChronoUnit.SECONDS).toString());
node.put("region", region);
node.putArray("resources").add(rule.getArn());
node.putObject("detail");
@@ -222,6 +222,25 @@ class RuleSchedulerTest {
assertEquals(before + 1, invoker.invocationCount());
}
@Test
void scheduledEventTimeHasWholeSecondPrecision() throws Exception {
Clock clock = Clock.fixed(Instant.parse("2026-06-15T12:00:59.123456789Z"), ZoneOffset.UTC);
for (String expression : List.of(EVERY_MINUTE_CRON, "rate(1 minute)")) {
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = timerHandlerCaptor();
when(vertx.setTimer(anyLong(), fire.capture())).thenReturn(1L, 2L);
when(vertx.setPeriodic(anyLong(), fire.capture())).thenReturn(1L);
StoppingInvoker invoker = new StoppingInvoker(() -> { });
RuleScheduler scheduler = newScheduler(vertx, invoker, clock);
scheduler.startScheduler(RULE_ARN, expression, () -> toScheduleData(enabledRule()));
fire.getValue().handle(1L);
assertEquals("2026-06-15T12:00:59Z",
new ObjectMapper().readTree(invoker.eventJson()).path("time").asText(), expression);
}
}
@SuppressWarnings("unchecked")
private static ArgumentCaptor<Handler<Long>> timerHandlerCaptor() {
return ArgumentCaptor.forClass(Handler.class);
@@ -250,9 +269,12 @@ class RuleSchedulerTest {
}
private static RuleScheduler newScheduler(Vertx vertx, EventBridgeInvoker invoker) {
return newScheduler(vertx, invoker, Clock.fixed(JUST_BEFORE_MINUTE_BOUNDARY, ZoneOffset.UTC));
}
private static RuleScheduler newScheduler(Vertx vertx, EventBridgeInvoker invoker, Clock clock) {
TargetDispatcher dispatcher = new TargetDispatcher(invoker, null, "http://localhost:4566", Clock.systemUTC(), null);
return new RuleScheduler(vertx, testConfig(), new ObjectMapper(), dispatcher,
Clock.fixed(JUST_BEFORE_MINUTE_BOUNDARY, ZoneOffset.UTC));
return new RuleScheduler(vertx, testConfig(), new ObjectMapper(), dispatcher, clock);
}
/**
@@ -263,6 +285,7 @@ class RuleSchedulerTest {
private final Runnable onDelivery;
private final AtomicInteger invocationCount = new AtomicInteger();
private final AtomicReference<String> eventJson = new AtomicReference<>();
StoppingInvoker(Runnable onDelivery) {
super(null, null, null, new ObjectMapper(), testConfig());
@@ -272,12 +295,17 @@ class RuleSchedulerTest {
@Override
public void invokeTarget(Target target, String eventJson, String region, String ruleArn) {
invocationCount.incrementAndGet();
this.eventJson.set(eventJson);
onDelivery.run();
}
int invocationCount() {
return invocationCount.get();
}
String eventJson() {
return eventJson.get();
}
}
private static EmulatorConfig testConfig() {