mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sfn): preserve mocked response indexes (#3391)
Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
1 parent
ad9a88cf7e
commit
78cf107fcb
2 files changed
+110
-19
No files matched your search
@@ -120,6 +120,22 @@ public class AslExecutor {
|
||||
private record ResolvedMapItems(JsonNode items, MapItemsSource source) {
|
||||
}
|
||||
|
||||
private record ActiveMockExecution(
|
||||
MockedTestCase testCase,
|
||||
ConcurrentHashMap<String, AtomicInteger> responseIndexes) {
|
||||
|
||||
private ActiveMockExecution(MockedTestCase testCase) {
|
||||
this(testCase, new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
private int nextResponseIndex(String stateName) {
|
||||
return responseIndexes.computeIfAbsent(stateName, ignored -> new AtomicInteger()).getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
private record MockedTaskInvocation(List<MockedResponseStep> steps, int responseIndex) {
|
||||
}
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AslExecutor.class);
|
||||
private static final int MAX_WAIT_SECONDS = 30;
|
||||
// How long a Task waits for its token when the state declares no TimeoutSeconds. AWS lets it
|
||||
@@ -225,7 +241,7 @@ public class AslExecutor {
|
||||
private final WebClient webClient;
|
||||
private final EmulatorConfig config;
|
||||
private final CustomResourceLiveness customResourceLiveness;
|
||||
private final Map<String, MockedTestCase> activeMocks = new ConcurrentHashMap<>();
|
||||
private final Map<String, ActiveMockExecution> activeMocks = new ConcurrentHashMap<>();
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool(r -> {
|
||||
Thread t = new Thread(r, "sfn-executor");
|
||||
t.setDaemon(true);
|
||||
@@ -539,7 +555,7 @@ public class AslExecutor {
|
||||
|
||||
private void registerMocks(Execution exec, MockedTestCase mockedTestCase) {
|
||||
if (mockedTestCase != null) {
|
||||
activeMocks.put(exec.getExecutionArn(), mockedTestCase);
|
||||
activeMocks.put(exec.getExecutionArn(), new ActiveMockExecution(mockedTestCase));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -560,7 +576,7 @@ public class AslExecutor {
|
||||
while (true) {
|
||||
try {
|
||||
return executeState(name, type, stateDef, input, chain, sm, jsonata,
|
||||
topLevelQueryLanguage, context, variables, attempt, executionDeadlineNanos);
|
||||
topLevelQueryLanguage, context, variables, executionDeadlineNanos);
|
||||
} catch (FailStateException raised) {
|
||||
var e = raised.attributedTo(name, enteredEventId);
|
||||
if (!raised.hasFinalCause()) {
|
||||
@@ -634,11 +650,11 @@ public class AslExecutor {
|
||||
private StateResult executeState(String name, String type, JsonNode stateDef, JsonNode input,
|
||||
HistoryChain chain, StateMachine sm, boolean jsonata,
|
||||
String topLevelQueryLanguage, JsonNode context, ObjectNode variables,
|
||||
int attempt, long executionDeadlineNanos) throws Exception {
|
||||
long executionDeadlineNanos) throws Exception {
|
||||
return switch (type) {
|
||||
case "Pass" -> executePassState(stateDef, input, jsonata, context, variables);
|
||||
case "Task" -> executeTaskState(name, stateDef, input, chain, sm,
|
||||
jsonata, context, variables, attempt, executionDeadlineNanos);
|
||||
jsonata, context, variables, executionDeadlineNanos);
|
||||
case "Choice" -> executeChoiceState(stateDef, input, jsonata, context, variables);
|
||||
case "Wait" -> executeWaitState(stateDef, input, jsonata, context, variables, executionDeadlineNanos);
|
||||
case "Succeed" -> executeSucceedState(stateDef, input, jsonata, context, variables);
|
||||
@@ -678,7 +694,7 @@ public class AslExecutor {
|
||||
|
||||
private StateResult executeTaskState(String stateName, JsonNode stateDef, JsonNode input,
|
||||
HistoryChain chain, StateMachine sm, boolean jsonata,
|
||||
JsonNode context, ObjectNode variables, int attempt,
|
||||
JsonNode context, ObjectNode variables,
|
||||
long executionDeadlineNanos) throws Exception {
|
||||
var resource = stateDef.path("Resource").asText();
|
||||
var isWaitForToken = resource.endsWith(".waitForTaskToken");
|
||||
@@ -686,10 +702,10 @@ public class AslExecutor {
|
||||
? resource.substring(0, resource.length() - ".waitForTaskToken".length())
|
||||
: resource;
|
||||
var isActivity = isActivityArn(effectiveResource);
|
||||
var mockedSteps = findMockedResponses(context, stateName);
|
||||
var mockedInvocation = findMockedInvocation(context, stateName);
|
||||
// A mocked task never calls the integrated service, so it neither registers a task token
|
||||
// nor waits for one; the mocked response stands in for the whole interaction.
|
||||
var needsToken = mockedSteps == null && (isWaitForToken || isActivity);
|
||||
var needsToken = mockedInvocation == null && (isWaitForToken || isActivity);
|
||||
|
||||
String taskToken = null;
|
||||
if (needsToken) {
|
||||
@@ -720,8 +736,8 @@ public class AslExecutor {
|
||||
addTaskScheduledEvent(chain, profile, stateDef, effectiveInput, sm);
|
||||
addTaskStartedEvent(chain, profile);
|
||||
try {
|
||||
taskResult = mockedSteps != null
|
||||
? mockedTaskResult(mockedSteps, stateName, attempt)
|
||||
taskResult = mockedInvocation != null
|
||||
? mockedTaskResult(mockedInvocation.steps(), stateName, mockedInvocation.responseIndex())
|
||||
: invokeResource(effectiveResource, effectiveInput, sm, taskToken,
|
||||
executionDeadlineNanos, jsonata ? null : stateDef.path("Parameters"));
|
||||
if (tokenFuture != null) {
|
||||
@@ -773,7 +789,7 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private List<MockedResponseStep> findMockedResponses(JsonNode context, String stateName) {
|
||||
private MockedTaskInvocation findMockedInvocation(JsonNode context, String stateName) {
|
||||
if (activeMocks.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@@ -781,13 +797,19 @@ public class AslExecutor {
|
||||
if (executionArn == null) {
|
||||
return null;
|
||||
}
|
||||
var testCase = activeMocks.get(executionArn);
|
||||
return testCase != null ? testCase.stateResponses().get(stateName) : null;
|
||||
var activeMock = activeMocks.get(executionArn);
|
||||
if (activeMock == null) {
|
||||
return null;
|
||||
}
|
||||
var steps = activeMock.testCase().stateResponses().get(stateName);
|
||||
return steps != null
|
||||
? new MockedTaskInvocation(steps, activeMock.nextResponseIndex(stateName))
|
||||
: null;
|
||||
}
|
||||
|
||||
private JsonNode mockedTaskResult(List<MockedResponseStep> steps, String stateName, int attempt) {
|
||||
private JsonNode mockedTaskResult(List<MockedResponseStep> steps, String stateName, int responseIndex) {
|
||||
for (var step : steps) {
|
||||
if (step.covers(attempt)) {
|
||||
if (step.covers(responseIndex)) {
|
||||
if (step.isThrow()) {
|
||||
// The mocked Error and Cause must reach Retry/Catch unchanged; routing them
|
||||
// through integration error translation would rewrite the error name that
|
||||
@@ -798,7 +820,7 @@ public class AslExecutor {
|
||||
}
|
||||
}
|
||||
throw new FailStateException("States.Runtime",
|
||||
"No mocked response defined for attempt " + attempt + " of state '" + stateName + "'");
|
||||
"No mocked response defined for attempt " + responseIndex + " of state '" + stateName + "'");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+72
-3
@@ -154,6 +154,67 @@ class AslExecutorMockedResponsesTest {
|
||||
assertTrue(objectMapper.readTree(execution.getOutput()).path("retried").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockedResponseIndexContinuesAcrossMapIterations() throws Exception {
|
||||
var mocks = testCase(Map.of("Call API", List.of(
|
||||
returnStep(0, 0, "{\"Payload\": \"first\"}"),
|
||||
returnStep(1, 1, "{\"Payload\": \"second\"}"))));
|
||||
|
||||
var execution = run("""
|
||||
{
|
||||
"StartAt": "Each",
|
||||
"States": {
|
||||
"Each": {
|
||||
"Type": "Map",
|
||||
"ItemsPath": "$.ids",
|
||||
"MaxConcurrency": 1,
|
||||
"ItemProcessor": {
|
||||
"StartAt": "Call API",
|
||||
"States": {
|
||||
"Call API": {
|
||||
"Type": "Task",
|
||||
"Resource": "%s",
|
||||
"OutputPath": "$.Payload",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(UNSUPPORTED_RESOURCE), mocks, "{\"ids\": [1, 2]}");
|
||||
|
||||
assertEquals("SUCCEEDED", execution.getStatus());
|
||||
assertEquals(List.of("first", "second"),
|
||||
objectMapper.readValue(execution.getOutput(), List.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockedResponseIndexRestartsForEachExecution() throws Exception {
|
||||
var mocks = testCase(Map.of("Call API", List.of(
|
||||
returnStep(0, 0, "{\"value\": \"first\"}"),
|
||||
returnStep(1, 1, "{\"value\": \"second\"}"))));
|
||||
var definition = """
|
||||
{
|
||||
"StartAt": "Call API",
|
||||
"States": {
|
||||
"Call API": {
|
||||
"Type": "Task",
|
||||
"Resource": "%s",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(UNSUPPORTED_RESOURCE);
|
||||
|
||||
var firstExecution = run(definition, mocks, "{}", "first-execution");
|
||||
var secondExecution = run(definition, mocks, "{}", "second-execution");
|
||||
|
||||
assertEquals("first", objectMapper.readTree(firstExecution.getOutput()).path("value").asText());
|
||||
assertEquals("first", objectMapper.readTree(secondExecution.getOutput()).path("value").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingAttemptEntryFailsExecution() throws Exception {
|
||||
var mocks = testCase(Map.of("Call API", List.of(
|
||||
@@ -236,6 +297,14 @@ class AslExecutorMockedResponsesTest {
|
||||
}
|
||||
|
||||
private Execution run(String definition, MockedTestCase mocks) {
|
||||
return run(definition, mocks, "{}");
|
||||
}
|
||||
|
||||
private Execution run(String definition, MockedTestCase mocks, String input) {
|
||||
return run(definition, mocks, input, "mock-test-execution");
|
||||
}
|
||||
|
||||
private Execution run(String definition, MockedTestCase mocks, String input, String executionName) {
|
||||
var stateMachine = new StateMachine();
|
||||
stateMachine.setName("mock-test");
|
||||
stateMachine.setStateMachineArn("arn:aws:states:%s:%s:stateMachine:mock-test".formatted(REGION, ACCOUNT));
|
||||
@@ -243,11 +312,11 @@ class AslExecutorMockedResponsesTest {
|
||||
stateMachine.setDefinition(definition);
|
||||
|
||||
var execution = new Execution();
|
||||
execution.setName("mock-test-execution");
|
||||
execution.setName(executionName);
|
||||
execution.setExecutionArn(
|
||||
"arn:aws:states:%s:%s:execution:mock-test:mock-test-execution".formatted(REGION, ACCOUNT));
|
||||
"arn:aws:states:%s:%s:execution:mock-test:%s".formatted(REGION, ACCOUNT, executionName));
|
||||
execution.setStateMachineArn(stateMachine.getStateMachineArn());
|
||||
execution.setInput("{}");
|
||||
execution.setInput(input);
|
||||
|
||||
var history = new ArrayList<HistoryEvent>();
|
||||
executor.executeSync(stateMachine, execution, history, mocks, (updated, events) -> {
|
||||
|
||||
Reference in new issue
Block a user