mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(scheduler): substitute schedule context attributes in target Input (#5194)
* feat(scheduler): substitute schedule context attributes in target Input Replace <aws.scheduler.schedule-arn>, <aws.scheduler.scheduled-time>, <aws.scheduler.execution-id> and <aws.scheduler.attempt-number> in the target Input for templated and universal targets, using the occurrence's scheduled time and the retry attempt the dispatcher tracks. * test(scheduler): share one harness between the scheduler integration tests SchedulerContextAttributesIntegrationTest copied its schedule creation, scoped dispatcher, unique names and cleanup from SchedulerStepFunctionsIntegrationTest. Both now use SchedulerTestHarness, which also builds the at() expression they each formatted by hand. * fix(scheduler): keep dead-letter context aligned with the last attempt Materialize the target request before every invocation so an exhausted occurrence's dead-letter body carries the same execution id and attempt number as its last attempted delivery. Preserve that request when the occurrence expires before another retry. Share scheduled-time formatting across context attributes, default events and dead-letter attributes. Cover retry-count and event-age exhaustion and remove the documented first-attempt body limitation. * style(scheduler): fix shared harness static import order Place ROLE_ARN before atExpression to satisfy the ImportOrder rule used by the PR's merged Checkstyle configuration.
This commit is contained in:
1 parent
5dfb45d71f
commit
07ebc33d28
8 files changed
+606
-143
No files matched your search
+69
-47
@@ -75,8 +75,8 @@ class ScheduleDispatcherTest {
|
||||
s.getTarget().setDeadLetterConfig(new DeadLetterConfig(DLQ_TARGET_ARN));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker)
|
||||
.invoke(eq(s), any());
|
||||
when(invoker.materializeRequest(eq(s), any())).thenReturn(TARGET_REQUEST);
|
||||
.invoke(eq(s), any(), any(), anyInt());
|
||||
when(invoker.materializeRequest(eq(s), any(), any(), anyInt())).thenReturn(TARGET_REQUEST);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")));
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,7 +118,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:17:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,7 +130,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:20:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,7 +163,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,7 +174,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,7 +185,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,13 +195,13 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:04:00Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:06:00Z"));
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:11:01Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -210,15 +210,15 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(schedule));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-28T08:59:59Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
|
||||
Instant firstFire = Instant.parse("2026-04-28T09:00:00Z");
|
||||
dispatcher.tick(firstFire);
|
||||
verify(invoker).invoke(schedule, firstFire);
|
||||
verify(invoker).invoke(eq(schedule), eq(firstFire), any(), anyInt());
|
||||
|
||||
Instant secondFire = firstFire.plusSeconds(7 * 24 * 60 * 60);
|
||||
dispatcher.tick(secondFire);
|
||||
verify(invoker).invoke(schedule, secondFire);
|
||||
verify(invoker).invoke(eq(schedule), eq(secondFire), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -229,9 +229,9 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-03-08T09:30:00Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
dispatcher.tick(Instant.parse("2026-03-08T10:30:00Z"));
|
||||
verify(invoker).invoke(s, Instant.parse("2026-03-08T10:30:00Z"));
|
||||
verify(invoker).invoke(eq(s), eq(Instant.parse("2026-03-08T10:30:00Z")), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -241,9 +241,9 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T15:29:00Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
dispatcher.tick(Instant.parse("2026-04-21T15:30:00Z"));
|
||||
verify(invoker).invoke(s, Instant.parse("2026-04-21T15:30:00Z"));
|
||||
verify(invoker).invoke(eq(s), eq(Instant.parse("2026-04-21T15:30:00Z")), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -252,9 +252,9 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T10:29:00Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
dispatcher.tick(Instant.parse("2026-04-21T10:30:00Z"));
|
||||
verify(invoker).invoke(s, Instant.parse("2026-04-21T10:30:00Z"));
|
||||
verify(invoker).invoke(eq(s), eq(Instant.parse("2026-04-21T10:30:00Z")), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -265,13 +265,13 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:59:59Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
dispatcher.tick(Instant.parse("2026-04-21T10:00:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-22T10:00:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-23T10:00:00Z"));
|
||||
verify(invoker).invoke(s, Instant.parse("2026-04-21T10:00:00Z"));
|
||||
verify(invoker).invoke(s, Instant.parse("2026-04-22T10:00:00Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker).invoke(eq(s), eq(Instant.parse("2026-04-21T10:00:00Z")), any(), anyInt());
|
||||
verify(invoker).invoke(eq(s), eq(Instant.parse("2026-04-22T10:00:00Z")), any(), anyInt());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -281,7 +281,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T10:00:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-22T10:00:00Z"));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -290,7 +290,7 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
assertDoesNotThrow(() -> dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z")));
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -301,7 +301,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -312,7 +312,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")));
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -323,7 +323,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verify(invoker, never()).invoke(any(), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -333,13 +333,35 @@ class ScheduleDispatcherTest {
|
||||
s.getTarget().setRetryPolicy(new RetryPolicy(3600, 1));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker)
|
||||
.invoke(eq(s), any());
|
||||
.invoke(eq(s), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachRetryGetsItsOwnExecutionIdAndAnIncrementingAttemptNumber() {
|
||||
Schedule s = failingSchedule("attempts", "at(2026-04-21T09:17:54)", new RetryPolicy(3600, 1));
|
||||
Instant scheduledAt = Instant.parse("2026-04-21T09:17:54Z");
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
|
||||
ArgumentCaptor<String> executionIds = ArgumentCaptor.captor();
|
||||
ArgumentCaptor<Integer> attempts = ArgumentCaptor.captor();
|
||||
verify(invoker, times(2)).invoke(eq(s), eq(scheduledAt), executionIds.capture(), attempts.capture());
|
||||
assertEquals(List.of(1, 2), attempts.getAllValues());
|
||||
String firstExecutionId = executionIds.getAllValues().get(0);
|
||||
String retryExecutionId = executionIds.getAllValues().get(1);
|
||||
assertTrue(firstExecutionId.matches("[0-9a-f]{16}"), firstExecutionId);
|
||||
assertTrue(retryExecutionId.matches("[0-9a-f]{16}"), retryExecutionId);
|
||||
assertNotEquals(firstExecutionId, retryExecutionId);
|
||||
verify(invoker).materializeRequest(s, scheduledAt, firstExecutionId, 1);
|
||||
verify(invoker).materializeRequest(s, scheduledAt, retryExecutionId, 2);
|
||||
assertEquals(retryExecutionId, attribute(deadLetters(1).get(0), "EXECUTION_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -348,15 +370,15 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:59Z"));
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:20:59Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
verify(invoker, times(3)).invoke(eq(s), any());
|
||||
verify(invoker, times(3)).invoke(eq(s), any(), any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -366,7 +388,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -377,7 +399,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T11:18:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verify(invoker, times(2)).invoke(eq(s), any(), any(), anyInt());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals("MaximumRetryAttempts", attribute(attributes, "EXHAUSTED_RETRY_CONDITION"));
|
||||
assertEquals("1", attribute(attributes, "RETRY_ATTEMPTS"));
|
||||
@@ -391,7 +413,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals("MaximumEventAgeInSeconds", attribute(attributes, "EXHAUSTED_RETRY_CONDITION"));
|
||||
assertEquals("0", attribute(attributes, "RETRY_ATTEMPTS"));
|
||||
@@ -401,13 +423,13 @@ class ScheduleDispatcherTest {
|
||||
void sendsExhaustedOccurrenceToDeadLetterQueueWithSchedulerAttributes() {
|
||||
Schedule s = failingSchedule("dead-letter", "at(2026-04-21T09:17:54)", new RetryPolicy(3600, 2));
|
||||
doThrow(new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
"The specified queue does not exist.", 400)).when(invoker).invoke(eq(s), any());
|
||||
"The specified queue does not exist.", 400)).when(invoker).invoke(eq(s), any(), any(), anyInt());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(3)).invoke(eq(s), any());
|
||||
verify(invoker, times(3)).invoke(eq(s), any(), any(), anyInt());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals(9, attributes.size());
|
||||
assertEquals("AWS.SimpleQueueService.NonExistentQueue", attribute(attributes, "ERROR_CODE"));
|
||||
@@ -431,11 +453,11 @@ class ScheduleDispatcherTest {
|
||||
topic.getTarget().setRetryPolicy(new RetryPolicy(3600, 0));
|
||||
topic.getTarget().setDeadLetterConfig(new DeadLetterConfig("arn:aws:sns:eu-central-1:000000000000:dead-letter"));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(fifo, topic));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker).invoke(any(), any());
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker).invoke(any(), any(), any(), anyInt());
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 2);
|
||||
|
||||
verify(invoker, times(2)).invoke(any(), any());
|
||||
verify(invoker, times(2)).invoke(any(), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -447,7 +469,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:10:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:12:00Z"));
|
||||
|
||||
verify(invoker, times(4)).invoke(eq(s), any());
|
||||
verify(invoker, times(4)).invoke(eq(s), any(), any(), anyInt());
|
||||
List<Map<String, MessageAttributeValue>> deadLetters = deadLetters(2);
|
||||
assertEquals("2026-04-21T09:05:00Z", attribute(deadLetters.get(0), "SCHEDULED_TIME"));
|
||||
assertEquals("2026-04-21T09:10:00Z", attribute(deadLetters.get(1), "SCHEDULED_TIME"));
|
||||
@@ -459,7 +481,7 @@ class ScheduleDispatcherTest {
|
||||
Schedule s = failingSchedule("at-delete", "at(2026-04-21T09:17:54)", new RetryPolicy(3600, 1));
|
||||
s.setActionAfterCompletion("DELETE");
|
||||
s.setAccountId("000000000000");
|
||||
when(invoker.invoke(eq(s), any()))
|
||||
when(invoker.invoke(eq(s), any(), any(), anyInt()))
|
||||
.thenThrow(new RuntimeException("target unavailable"))
|
||||
.thenReturn(TARGET_REQUEST);
|
||||
|
||||
@@ -495,7 +517,7 @@ class ScheduleDispatcherTest {
|
||||
s.setState("ENABLED");
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:20Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -506,7 +528,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -519,7 +541,7 @@ class ScheduleDispatcherTest {
|
||||
s.setLastModificationDate(Instant.parse("2026-04-21T09:18:05Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:10Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verify(invoker, times(1)).invoke(eq(s), any(), any(), anyInt());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,11 @@ import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
@@ -601,4 +603,81 @@ class ScheduleInvokerTest {
|
||||
|
||||
verifyNoInteractions(sqsService, lambdaService, snsService, eventBridgeService, ecsService);
|
||||
}
|
||||
|
||||
private static final String CONTEXT_INPUT = "{\"arn\":\"<aws.scheduler.schedule-arn>\","
|
||||
+ "\"time\":\"<aws.scheduler.scheduled-time>\",\"id\":\"<aws.scheduler.execution-id>\","
|
||||
+ "\"attempt\":<aws.scheduler.attempt-number>}";
|
||||
private static final String SCHEDULE_ARN =
|
||||
"arn:aws:scheduler:us-east-1:000000000000:schedule/default/test-schedule";
|
||||
|
||||
@Test
|
||||
void templatedTargetInputHasContextAttributesReplaced() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:sqs:us-east-1:000000000000:test-queue");
|
||||
target.setInput(CONTEXT_INPUT);
|
||||
|
||||
String request = invoker.invoke(scheduleIn("us-east-1", target),
|
||||
Instant.parse("2026-04-21T09:17:54.678Z"), "d32c5kddcf5bb8c3", 3);
|
||||
|
||||
String expected = "{\"arn\":\"" + SCHEDULE_ARN + "\",\"time\":\"2026-04-21T09:17:54Z\","
|
||||
+ "\"id\":\"d32c5kddcf5bb8c3\",\"attempt\":3}";
|
||||
verify(sqsService).sendMessage(anyString(), eq(expected), eq(0), isNull(), isNull(), eq("us-east-1"));
|
||||
assertTrue(request.contains("2026-04-21T09:17:54Z"), request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializedRequestUsesTheSameContextAttributes() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:lambda:us-east-1:000000000000:function:fn");
|
||||
target.setInput(CONTEXT_INPUT);
|
||||
|
||||
String request = invoker.materializeRequest(scheduleIn("us-east-1", target), SCHEDULED_AT,
|
||||
"abcdef0123456789", 1);
|
||||
|
||||
assertTrue(request.contains("abcdef0123456789"), request);
|
||||
assertTrue(request.contains("2026-04-21T09:17:54Z"), request);
|
||||
assertTrue(request.contains("\\\"attempt\\\":1"), request);
|
||||
assertFalse(request.contains("<aws.scheduler."), request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalTargetInputHasContextAttributesReplacedBeforeTheCall() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"QueueUrl\":\"http://localhost:4566/000000000000/q\","
|
||||
+ "\"MessageBody\":\"<aws.scheduler.schedule-arn>|<aws.scheduler.scheduled-time>|"
|
||||
+ "<aws.scheduler.execution-id>|<aws.scheduler.attempt-number>\"}");
|
||||
|
||||
invoker.invoke(scheduleIn("us-east-1", target), SCHEDULED_AT, "0123456789abcdef", 2);
|
||||
|
||||
verify(sqsService).sendMessage(eq("http://localhost:4566/000000000000/q"),
|
||||
eq(SCHEDULE_ARN + "|2026-04-21T09:17:54Z|0123456789abcdef|2"), eq(0), isNull(), isNull(),
|
||||
argThat(attrs -> attrs == null || attrs.isEmpty()), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputWithoutContextAttributesIsDeliveredUnchanged() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:sqs:us-east-1:000000000000:test-queue");
|
||||
target.setInput("{\"plain\":\"<b>not a keyword</b>\"}");
|
||||
|
||||
invoker.invoke(scheduleIn("us-east-1", target), SCHEDULED_AT, "0123456789abcdef", 4);
|
||||
|
||||
verify(sqsService).sendMessage(anyString(), eq("{\"plain\":\"<b>not a keyword</b>\"}"), eq(0),
|
||||
isNull(), isNull(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoArgumentInvokeIsTheFirstAttemptUnderAFreshExecutionId() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:sqs:us-east-1:000000000000:test-queue");
|
||||
target.setInput("<aws.scheduler.execution-id>/<aws.scheduler.attempt-number>");
|
||||
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService).sendMessage(anyString(), body.capture(), eq(0), isNull(), isNull(), eq("us-east-1"));
|
||||
assertTrue(body.getValue().matches("[0-9a-f]{16}/1"), body.getValue());
|
||||
}
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.DeadLetterConfig;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.RetryPolicy;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.REGION;
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.atExpression;
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.uniqueName;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The Scheduler context attributes in a target's {@code Input} are replaced on delivery with the
|
||||
* schedule ARN, the scheduled time, the execution id and the attempt number.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SchedulerContextAttributesIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String CONTEXT_INPUT = "{\"arn\":\"<aws.scheduler.schedule-arn>\","
|
||||
+ "\"time\":\"<aws.scheduler.scheduled-time>\","
|
||||
+ "\"executionId\":\"<aws.scheduler.execution-id>\","
|
||||
+ "\"attempt\":<aws.scheduler.attempt-number>}";
|
||||
|
||||
@Inject
|
||||
ScheduleInvoker scheduleInvoker;
|
||||
|
||||
@Inject
|
||||
SchedulerService schedulerService;
|
||||
|
||||
@Inject
|
||||
SqsService sqsService;
|
||||
|
||||
@Inject
|
||||
EmulatorConfig config;
|
||||
|
||||
private final List<String> queueUrls = new ArrayList<>();
|
||||
private SchedulerTestHarness harness;
|
||||
|
||||
@BeforeEach
|
||||
void createHarness() {
|
||||
harness = new SchedulerTestHarness(schedulerService, scheduleInvoker, sqsService, config);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
harness.cleanUp();
|
||||
for (String queueUrl : queueUrls) {
|
||||
sqsService.deleteQueue(queueUrl, REGION);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void atScheduleDeliversInputWithContextAttributesReplaced() throws Exception {
|
||||
String queueName = uniqueName("ctx-at-q");
|
||||
String queueUrl = createQueue(queueName);
|
||||
String scheduleName = uniqueName("ctx-at");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "ENABLED", queueArn(queueName), CONTEXT_INPUT);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
|
||||
harness.dispatcherFor(scheduleName).tick(fireAt.plusSeconds(5));
|
||||
|
||||
JsonNode body = singleBody(queueUrl);
|
||||
assertEquals(schedule.getArn(), body.path("arn").asText());
|
||||
assertEquals(fireAt.toString(), body.path("time").asText());
|
||||
assertTrue(body.path("time").asText().matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"),
|
||||
body.toString());
|
||||
assertTrue(body.path("executionId").asText().matches("[0-9a-f]{16}"), body.toString());
|
||||
assertEquals(1, body.path("attempt").asInt());
|
||||
assertTrue(body.path("attempt").isInt(), body.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rateScheduleDeliversEachOccurrenceWithItsScheduledTime() throws Exception {
|
||||
String queueName = uniqueName("ctx-rate-q");
|
||||
String queueUrl = createQueue(queueName);
|
||||
String scheduleName = uniqueName("ctx-rate");
|
||||
harness.createSchedule(scheduleName, "rate(1 minute)", "ENABLED", queueArn(queueName), CONTEXT_INPUT);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
Instant firstFire = schedule.getCreationDate().plus(1, ChronoUnit.MINUTES);
|
||||
ScheduleDispatcher dispatcher = harness.dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(firstFire.plusSeconds(1));
|
||||
|
||||
JsonNode body = singleBody(queueUrl);
|
||||
assertEquals(schedule.getArn(), body.path("arn").asText());
|
||||
assertEquals(firstFire.truncatedTo(ChronoUnit.SECONDS).toString(), body.path("time").asText());
|
||||
assertTrue(body.path("executionId").asText().matches("[0-9a-f]{16}"), body.toString());
|
||||
assertEquals(1, body.path("attempt").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void retriedDeliveryCarriesTheNextAttemptNumber() throws Exception {
|
||||
String queueName = uniqueName("ctx-retry-q");
|
||||
String scheduleName = uniqueName("ctx-retry");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
// The queue does not exist yet, so the first attempt fails and is retried on a later tick.
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "ENABLED", queueArn(queueName), CONTEXT_INPUT);
|
||||
ScheduleDispatcher dispatcher = harness.dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(fireAt.plusSeconds(1));
|
||||
String queueUrl = createQueue(queueName);
|
||||
dispatcher.tick(fireAt.plusSeconds(120));
|
||||
|
||||
JsonNode body = singleBody(queueUrl);
|
||||
assertEquals(2, body.path("attempt").asInt());
|
||||
assertEquals(fireAt.toString(), body.path("time").asText());
|
||||
assertTrue(body.path("executionId").asText().matches("[0-9a-f]{16}"), body.toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void deadLetterBodyMatchesLastAttemptContext(boolean expiresByAge) throws Exception {
|
||||
String queueName = uniqueName("ctx-missing-q");
|
||||
String dlqName = uniqueName("ctx-dlq");
|
||||
String dlqUrl = createQueue(dlqName);
|
||||
String scheduleName = uniqueName("ctx-dlq-schedule");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "ENABLED", queueArn(queueName), CONTEXT_INPUT);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
schedule.getTarget().setDeadLetterConfig(new DeadLetterConfig(queueArn(dlqName)));
|
||||
schedule.getTarget().setRetryPolicy(expiresByAge ? new RetryPolicy(180, 10) : new RetryPolicy(3600, 1));
|
||||
ScheduleDispatcher dispatcher = harness.dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(fireAt.plusSeconds(1));
|
||||
dispatcher.tick(fireAt.plusSeconds(61));
|
||||
if (expiresByAge) {
|
||||
dispatcher.tick(fireAt.plusSeconds(181));
|
||||
}
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(dlqUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
Message message = messages.get(0);
|
||||
JsonNode request = MAPPER.readTree(message.getBody());
|
||||
JsonNode input = MAPPER.readTree(request.path("MessageBody").asText());
|
||||
assertEquals(2, input.path("attempt").asInt());
|
||||
assertEquals(schedule.getArn(), input.path("arn").asText());
|
||||
assertEquals(fireAt.toString(), input.path("time").asText());
|
||||
assertEquals(input.path("executionId").asText(),
|
||||
message.getMessageAttributes().get("EXECUTION_ID").getStringValue());
|
||||
assertEquals(input.path("time").asText(),
|
||||
message.getMessageAttributes().get("SCHEDULED_TIME").getStringValue());
|
||||
assertEquals("1", message.getMessageAttributes().get("RETRY_ATTEMPTS").getStringValue());
|
||||
assertEquals(expiresByAge ? "MaximumEventAgeInSeconds" : "MaximumRetryAttempts",
|
||||
message.getMessageAttributes().get("EXHAUSTED_RETRY_CONDITION").getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputWithoutContextAttributesIsDeliveredUnchanged() {
|
||||
String queueName = uniqueName("ctx-plain-q");
|
||||
String queueUrl = createQueue(queueName);
|
||||
String scheduleName = uniqueName("ctx-plain");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
String input = "{\"hello\":\"<world>\",\"n\":1}";
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "ENABLED", queueArn(queueName), input);
|
||||
|
||||
harness.dispatcherFor(scheduleName).tick(fireAt.plusSeconds(1));
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
assertEquals(input, messages.get(0).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executionIdsDifferBetweenOccurrences() throws Exception {
|
||||
String queueName = uniqueName("ctx-ids-q");
|
||||
String queueUrl = createQueue(queueName);
|
||||
String scheduleName = uniqueName("ctx-ids");
|
||||
harness.createSchedule(scheduleName, "rate(1 minute)", "ENABLED", queueArn(queueName), CONTEXT_INPUT);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
Instant firstFire = schedule.getCreationDate().plus(1, ChronoUnit.MINUTES);
|
||||
ScheduleDispatcher dispatcher = harness.dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(firstFire.plusSeconds(1));
|
||||
dispatcher.tick(firstFire.plusSeconds(62));
|
||||
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(2, messages.size());
|
||||
String first = MAPPER.readTree(messages.get(0).getBody()).path("executionId").asText();
|
||||
String second = MAPPER.readTree(messages.get(1).getBody()).path("executionId").asText();
|
||||
assertNotEquals(first, second);
|
||||
}
|
||||
|
||||
private JsonNode singleBody(String queueUrl) throws Exception {
|
||||
List<Message> messages = sqsService.receiveMessage(queueUrl, 10, 30, 0, REGION);
|
||||
assertEquals(1, messages.size());
|
||||
String body = messages.get(0).getBody();
|
||||
assertTrue(!body.contains("<aws.scheduler."), body);
|
||||
return MAPPER.readTree(body);
|
||||
}
|
||||
|
||||
private String createQueue(String queueName) {
|
||||
String queueUrl = sqsService.createQueue(queueName, Map.of(), REGION).getQueueUrl();
|
||||
queueUrls.add(queueUrl);
|
||||
return queueUrl;
|
||||
}
|
||||
|
||||
private static String queueArn(String queueName) {
|
||||
return "arn:aws:sqs:" + REGION + ":" + ACCOUNT + ":" + queueName;
|
||||
}
|
||||
}
|
||||
+23
-73
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.scheduler;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
@@ -13,33 +12,29 @@ import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.REGION;
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.ROLE_ARN;
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.atExpression;
|
||||
import static io.github.hectorvent.floci.services.scheduler.SchedulerTestHarness.uniqueName;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class SchedulerStepFunctionsIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final DateTimeFormatter SCHEDULE_TIME =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
|
||||
|
||||
@Inject
|
||||
ScheduleInvoker scheduleInvoker;
|
||||
@@ -56,30 +51,25 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
@Inject
|
||||
EmulatorConfig config;
|
||||
|
||||
private final List<String> scheduleNames = new ArrayList<>();
|
||||
private final List<String> stateMachineArns = new ArrayList<>();
|
||||
private final List<ScheduleDispatcher> testDispatchers = new ArrayList<>();
|
||||
private SchedulerTestHarness harness;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createHarness() {
|
||||
harness = new SchedulerTestHarness(schedulerService, scheduleInvoker, sqsService, config);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
for (String scheduleName : scheduleNames) {
|
||||
try {
|
||||
schedulerService.deleteSchedule(scheduleName, null, REGION);
|
||||
} catch (AwsException expected) {
|
||||
// A schedule with ActionAfterCompletion=DELETE may already be gone.
|
||||
}
|
||||
}
|
||||
harness.cleanUp();
|
||||
for (String stateMachineArn : stateMachineArns) {
|
||||
stepFunctionsService.deleteStateMachine(stateMachineArn);
|
||||
}
|
||||
for (ScheduleDispatcher dispatcher : testDispatchers) {
|
||||
dispatcher.onStop(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,10 +78,9 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
String scheduleName = uniqueName("sfn-at");
|
||||
String input = "{\"order\":{\"id\":42},\"items\":[\"a\",\"b\"]}";
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "ENABLED",
|
||||
stateMachineArn, input);
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "ENABLED", stateMachineArn, input);
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
harness.dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
List<Map<String, Object>> executions = listExecutions(stateMachineArn);
|
||||
assertEquals(1, executions.size());
|
||||
@@ -105,10 +94,10 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
void recurringScheduleStartsOneExecutionPerOccurrence() {
|
||||
String stateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-rate");
|
||||
createSchedule(scheduleName, "rate(1 minute)", "ENABLED", stateMachineArn, "{\"kind\":\"recurring\"}");
|
||||
harness.createSchedule(scheduleName, "rate(1 minute)", "ENABLED", stateMachineArn, "{\"kind\":\"recurring\"}");
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
Instant firstFire = schedule.getCreationDate().plus(61, ChronoUnit.SECONDS);
|
||||
ScheduleDispatcher dispatcher = dispatcherFor(scheduleName);
|
||||
ScheduleDispatcher dispatcher = harness.dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(firstFire);
|
||||
dispatcher.tick(firstFire.plus(61, ChronoUnit.SECONDS));
|
||||
@@ -121,10 +110,9 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
String stateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-disabled");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "DISABLED",
|
||||
stateMachineArn, "{\"disabled\":true}");
|
||||
harness.createSchedule(scheduleName, atExpression(fireAt), "DISABLED", stateMachineArn, "{\"disabled\":true}");
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
harness.dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
assertEquals(0, listExecutions(stateMachineArn).size());
|
||||
}
|
||||
@@ -133,7 +121,7 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
void stepFunctionsDeleteScheduleStopsFutureOccurrence() {
|
||||
String targetStateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-delete");
|
||||
createSchedule(scheduleName, "rate(1 minute)", "ENABLED",
|
||||
harness.createSchedule(scheduleName, "rate(1 minute)", "ENABLED",
|
||||
targetStateMachineArn, "{\"deleted\":false}");
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
Instant fireAt = schedule.getCreationDate().plus(61, ChronoUnit.SECONDS);
|
||||
@@ -155,10 +143,7 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
Response deletion = waitForTerminalExecution(startExecution(deleterStateMachineArn));
|
||||
assertEquals("SUCCEEDED", deletion.jsonPath().getString("status"));
|
||||
|
||||
ScheduleDispatcher dispatcher = new ScheduleDispatcher(
|
||||
schedulerService, scheduleInvoker, sqsService, config);
|
||||
testDispatchers.add(dispatcher);
|
||||
dispatcher.tick(fireAt);
|
||||
harness.unscopedDispatcher().tick(fireAt);
|
||||
|
||||
assertEquals(0, listExecutions(targetStateMachineArn).size());
|
||||
}
|
||||
@@ -196,17 +181,17 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
}
|
||||
"""
|
||||
.replace("SCHEDULE_NAME", scheduleName)
|
||||
.replace("SCHEDULE_EXPRESSION", "at(" + SCHEDULE_TIME.format(fireAt) + ")")
|
||||
.replace("SCHEDULE_EXPRESSION", atExpression(fireAt))
|
||||
.replace("TARGET_ARN", targetStateMachineArn)
|
||||
.replace("ROLE", ROLE_ARN));
|
||||
|
||||
Response creation = waitForTerminalExecution(startExecution(creatorStateMachineArn));
|
||||
assertEquals("SUCCEEDED", creation.jsonPath().getString("status"));
|
||||
scheduleNames.add(scheduleName);
|
||||
harness.trackSchedule(scheduleName);
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
assertEquals(expectedInput, schedule.getTarget().getInput());
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
harness.dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
List<Map<String, Object>> executions = listExecutions(targetStateMachineArn);
|
||||
assertEquals(1, executions.size());
|
||||
@@ -254,37 +239,6 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
return response.jsonPath().getString("executionArn");
|
||||
}
|
||||
|
||||
private void createSchedule(String name, String expression, String state,
|
||||
String targetArn, String input) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ScheduleExpression", expression);
|
||||
request.putObject("FlexibleTimeWindow").put("Mode", "OFF");
|
||||
request.put("State", state);
|
||||
ObjectNode target = request.putObject("Target");
|
||||
target.put("Arn", targetArn);
|
||||
target.put("RoleArn", ROLE_ARN);
|
||||
target.put("Input", input);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/schedules/" + name)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
scheduleNames.add(name);
|
||||
}
|
||||
|
||||
private ScheduleDispatcher dispatcherFor(String scheduleName) {
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
SchedulerService scopedSchedulerService = mock(SchedulerService.class);
|
||||
when(scopedSchedulerService.listAllSchedules()).thenReturn(List.of(schedule));
|
||||
ScheduleDispatcher dispatcher = new ScheduleDispatcher(
|
||||
scopedSchedulerService, scheduleInvoker, sqsService, config);
|
||||
testDispatchers.add(dispatcher);
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> listExecutions(String stateMachineArn) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("stateMachineArn", stateMachineArn);
|
||||
@@ -318,8 +272,4 @@ class SchedulerStepFunctionsIntegrationTest {
|
||||
response.then().statusCode(200);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static String uniqueName(String prefix) {
|
||||
return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Creates schedules through the REST API and drives them with a dispatcher scoped to one schedule,
|
||||
* then deletes and stops what it created. An integration test builds one per test and calls
|
||||
* {@link #cleanUp()} after it.
|
||||
*/
|
||||
final class SchedulerTestHarness {
|
||||
|
||||
static final String REGION = "us-east-1";
|
||||
static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role";
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final DateTimeFormatter SCHEDULE_TIME =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
|
||||
|
||||
private final SchedulerService schedulerService;
|
||||
private final ScheduleInvoker scheduleInvoker;
|
||||
private final SqsService sqsService;
|
||||
private final EmulatorConfig config;
|
||||
private final List<String> scheduleNames = new ArrayList<>();
|
||||
private final List<ScheduleDispatcher> dispatchers = new ArrayList<>();
|
||||
|
||||
SchedulerTestHarness(SchedulerService schedulerService, ScheduleInvoker scheduleInvoker,
|
||||
SqsService sqsService, EmulatorConfig config) {
|
||||
this.schedulerService = schedulerService;
|
||||
this.scheduleInvoker = scheduleInvoker;
|
||||
this.sqsService = sqsService;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
static String uniqueName(String prefix) {
|
||||
return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
/** The one-time expression that fires at {@code time}, to the second. */
|
||||
static String atExpression(Instant time) {
|
||||
return "at(" + SCHEDULE_TIME.format(time) + ")";
|
||||
}
|
||||
|
||||
void createSchedule(String name, String expression, String state, String targetArn, String input) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ScheduleExpression", expression);
|
||||
request.putObject("FlexibleTimeWindow").put("Mode", "OFF");
|
||||
request.put("State", state);
|
||||
ObjectNode target = request.putObject("Target");
|
||||
target.put("Arn", targetArn);
|
||||
target.put("RoleArn", ROLE_ARN);
|
||||
target.put("Input", input);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/schedules/" + name)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
scheduleNames.add(name);
|
||||
}
|
||||
|
||||
/** Registers a schedule created another way, so {@link #cleanUp()} deletes it too. */
|
||||
void trackSchedule(String name) {
|
||||
scheduleNames.add(name);
|
||||
}
|
||||
|
||||
/** A dispatcher that sees only the named schedule, so other tests' schedules never fire. */
|
||||
ScheduleDispatcher dispatcherFor(String scheduleName) {
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
SchedulerService scopedSchedulerService = mock(SchedulerService.class);
|
||||
when(scopedSchedulerService.listAllSchedules()).thenReturn(List.of(schedule));
|
||||
return track(new ScheduleDispatcher(scopedSchedulerService, scheduleInvoker, sqsService, config));
|
||||
}
|
||||
|
||||
/** A dispatcher over every schedule the service holds. */
|
||||
ScheduleDispatcher unscopedDispatcher() {
|
||||
return track(new ScheduleDispatcher(schedulerService, scheduleInvoker, sqsService, config));
|
||||
}
|
||||
|
||||
void cleanUp() {
|
||||
for (String scheduleName : scheduleNames) {
|
||||
try {
|
||||
schedulerService.deleteSchedule(scheduleName, null, REGION);
|
||||
} catch (AwsException expected) {
|
||||
// The test, or ActionAfterCompletion=DELETE, may have deleted it already.
|
||||
}
|
||||
}
|
||||
for (ScheduleDispatcher dispatcher : dispatchers) {
|
||||
dispatcher.onStop(null);
|
||||
}
|
||||
}
|
||||
|
||||
private ScheduleDispatcher track(ScheduleDispatcher dispatcher) {
|
||||
dispatchers.add(dispatcher);
|
||||
return dispatcher;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user