generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDurableExecutionTest.java
More file actions
303 lines (259 loc) · 11.8 KB
/
DurableExecutionTest.java
File metadata and controls
303 lines (259 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable;
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 software.amazon.lambda.durable.TypeToken.get;
import java.util.List;
import java.util.concurrent.ExecutorService;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.lambda.model.CheckpointUpdatedExecutionState;
import software.amazon.awssdk.services.lambda.model.ExecutionDetails;
import software.amazon.awssdk.services.lambda.model.Operation;
import software.amazon.awssdk.services.lambda.model.OperationStatus;
import software.amazon.awssdk.services.lambda.model.OperationType;
import software.amazon.awssdk.services.lambda.model.StepDetails;
import software.amazon.lambda.durable.execution.DurableExecutor;
import software.amazon.lambda.durable.model.DurableExecutionInput;
import software.amazon.lambda.durable.model.ExecutionStatus;
class DurableExecutionTest {
private static final String EXECUTION_OP_ID = "20dae574-53da-37a1-bfd5-b0e2e6ec715d";
private static final String OPERATION_ID1 = TestUtils.hashOperationId("1");
private static final String EXECUTION_NAME = "exec-name";
private static final String EXECUTION_ARN = "arn:aws:lambda:us-east-1:123456789012:function:test/durable-execution/"
+ EXECUTION_NAME + "/" + EXECUTION_OP_ID;
private DurableConfig configWithMockClient() {
return DurableConfig.builder()
.withDurableExecutionClient(TestUtils.createMockClient())
.build();
}
@Test
void testExecuteSuccess() {
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input\"")
.build())
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp))
.build());
var output = DurableExecutor.execute(
input,
null,
get(String.class),
(userInput, ctx) -> ctx.step("test", String.class, stepCtx -> "Hello " + userInput),
configWithMockClient());
assertEquals(ExecutionStatus.SUCCEEDED, output.status());
assertNotNull(output.result());
assertTrue(output.result().contains("Hello test-input"));
}
@Test
void testExecutePending() {
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input\"")
.build())
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp))
.build());
var output = DurableExecutor.execute(
input,
null,
get(String.class),
(userInput, ctx) -> {
ctx.step("step1", String.class, stepCtx -> "Done");
ctx.wait(null, java.time.Duration.ofSeconds(60));
return "Should not reach here";
},
configWithMockClient());
assertEquals(ExecutionStatus.PENDING, output.status());
assertNull(output.result());
}
@Test
void testExecuteFailure() {
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input\"")
.build())
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp))
.build());
var output = DurableExecutor.execute(
input,
null,
get(String.class),
(userInput, ctx) -> {
throw new RuntimeException("Test error");
},
configWithMockClient());
assertEquals(ExecutionStatus.FAILED, output.status());
assertNotNull(output.error());
assertEquals("java.lang.RuntimeException", output.error().errorType());
assertEquals("Test error", output.error().errorMessage());
}
@Test
void testExecuteReplay() {
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input\"")
.build())
.build();
var completedStep = Operation.builder()
.id(OPERATION_ID1)
.name("step1")
.type(OperationType.STEP)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().result("\"First\"").build())
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token2",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp, completedStep))
.build());
var output = DurableExecutor.execute(
input,
null,
get(String.class),
(userInput, ctx) -> ctx.step("step1", String.class, stepCtx -> "Second"),
configWithMockClient());
assertEquals(ExecutionStatus.SUCCEEDED, output.status());
assertTrue(output.result().contains("First"));
}
@Test
void testValidationNoOperations() {
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder().operations(List.of()).build());
var exception = assertThrows(
IllegalStateException.class,
() -> DurableExecutor.execute(
input, null, get(String.class), (userInput, ctx) -> "result", configWithMockClient()));
assertEquals("EXECUTION operation not found", exception.getMessage());
}
@Test
void testValidationWrongFirstOperation() {
var stepOp = Operation.builder()
.id(OPERATION_ID1)
.type(OperationType.STEP)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().result("\"result\"").build())
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(stepOp))
.build());
var exception = assertThrows(
IllegalStateException.class,
() -> DurableExecutor.execute(
input, null, get(String.class), (userInput, ctx) -> "result", configWithMockClient()));
assertEquals("EXECUTION operation not found", exception.getMessage());
}
@Test
void testValidationMissingExecutionDetails() {
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.build();
var input = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp))
.build());
var result = DurableExecutor.execute(
input, null, get(String.class), (userInput, ctx) -> "result", configWithMockClient());
assertEquals(ExecutionStatus.FAILED, result.status());
assertEquals(
"EXECUTION operation missing executionDetails", result.error().errorMessage());
}
@Test
void testExecutorNotShutdownAfterMultipleHandlerInvocations() {
// Create a config with a shared executor
var config = configWithMockClient();
ExecutorService sharedExecutor = config.getExecutorService();
// Verify executor is not shutdown initially
assertFalse(sharedExecutor.isShutdown(), "Executor should not be shutdown initially");
var executionOp = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input-1\"")
.build())
.build();
var input1 = new DurableExecutionInput(
EXECUTION_ARN,
"token1",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp))
.build());
// Execute first handler
var output1 = DurableExecutor.execute(
input1,
null,
get(String.class),
(userInput, ctx) -> ctx.step("test1", String.class, stepCtx -> "Result 1: " + userInput),
config);
assertEquals(ExecutionStatus.SUCCEEDED, output1.status());
assertFalse(sharedExecutor.isShutdown(), "Executor should not be shutdown after first execution");
// Create second input with different execution operation
var executionOp2 = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.executionDetails(ExecutionDetails.builder()
.inputPayload("\"test-input-2\"")
.build())
.build();
var input2 = new DurableExecutionInput(
EXECUTION_ARN,
"token2",
CheckpointUpdatedExecutionState.builder()
.operations(List.of(executionOp2))
.build());
// Execute second handler using the same config (and thus same executor)
var output2 = DurableExecutor.execute(
input2,
null,
get(String.class),
(userInput, ctx) -> ctx.step("test2", String.class, stepCtx -> "Result 2: " + userInput),
config);
assertEquals(ExecutionStatus.SUCCEEDED, output2.status());
assertFalse(sharedExecutor.isShutdown(), "Executor should not be shutdown after second execution");
// Verify both executions completed successfully and used the same executor
assertTrue(output1.result().contains("Result 1: test-input-1"));
assertTrue(output2.result().contains("Result 2: test-input-2"));
}
}