-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDurableContextTest.java
More file actions
407 lines (346 loc) · 15.5 KB
/
DurableContextTest.java
File metadata and controls
407 lines (346 loc) · 15.5 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// 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.*;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.lambda.model.*;
import software.amazon.lambda.durable.context.DurableContextImpl;
import software.amazon.lambda.durable.execution.ExecutionManager;
import software.amazon.lambda.durable.execution.SuspendExecutionException;
import software.amazon.lambda.durable.execution.ThreadContext;
import software.amazon.lambda.durable.execution.ThreadType;
import software.amazon.lambda.durable.model.DurableExecutionInput;
import software.amazon.lambda.durable.model.OperationSubType;
import software.amazon.lambda.durable.retry.RetryStrategies;
class DurableContextTest {
private static final String EXECUTION_NAME = "349beff4-a89d-4bc8-a56f-af7a8af67a5f";
private static final String EXECUTION_OP_ID = "20dae574-53da-37a1-bfd5-b0e2e6ec715d";
private static final String EXECUTION_ARN = "arn:aws:lambda:us-east-1:123456789012:function:test/durable-execution/"
+ EXECUTION_NAME + "/" + EXECUTION_OP_ID;
private static final Operation EXECUTION_OP = Operation.builder()
.id(EXECUTION_OP_ID)
.type(OperationType.EXECUTION)
.status(OperationStatus.STARTED)
.build();
private static final String OPERATION_ID1 = TestUtils.hashOperationId("1");
private static final String OPERATION_ID2 = TestUtils.hashOperationId("2");
private static final String OPERATION_ID3 = TestUtils.hashOperationId("3");
private DurableContext createTestContext() {
return createTestContext(List.of());
}
private DurableContext createTestContext(List<Operation> initialOperations) {
var client = TestUtils.createMockClient();
var operations = new ArrayList<>(List.of(EXECUTION_OP));
operations.addAll(initialOperations);
var initialExecutionState =
CheckpointUpdatedExecutionState.builder().operations(operations).build();
var executionManager = new ExecutionManager(
new DurableExecutionInput(EXECUTION_ARN, "test-token", initialExecutionState),
DurableConfig.builder().withDurableExecutionClient(client).build());
var root = DurableContextImpl.createRootContext(
executionManager, DurableConfig.builder().build(), null);
executionManager.registerActiveThread(null);
executionManager.setCurrentThreadContext(new ThreadContext(null, ThreadType.CONTEXT));
return root;
}
@Test
void testContextCreation() {
var context = createTestContext();
assertNotNull(context);
assertNull(context.getLambdaContext());
}
@Test
void testGetExecutionContext() {
var context = createTestContext();
assertNotNull(context.getExecutionArn());
assertEquals(EXECUTION_ARN, context.getExecutionArn());
}
@Test
void testStepExecution() {
var context = createTestContext();
var result = context.step("test", String.class, stepCtx -> "Hello World");
assertEquals("Hello World", result);
}
@Test
void testStepReplay() {
// Create context with existing operation
var existingOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().result("\"Cached Result\"").build())
.build();
var context = createTestContext(List.of(existingOp));
// This should return cached result, not execute the function
var result = context.step("test", String.class, stepCtx -> "New Result");
assertEquals("Cached Result", result);
}
@Test
void testStepAsync() throws Exception {
var context = createTestContext();
var future = context.stepAsync("async-test", String.class, stepCtx -> "Async Result");
assertNotNull(future);
assertEquals("Async Result", future.get());
}
@Test
void testStepAsyncReplay() throws Exception {
// Create context with existing operation
var existingOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.stepDetails(
StepDetails.builder().result("\"Cached Async Result\"").build())
.build();
var context = createTestContext(List.of(existingOp));
// This should return cached result immediately
var future = context.stepAsync("async-test", String.class, stepCtx -> "New Async Result");
assertEquals("Cached Async Result", future.get());
}
@Test
void testWait() {
var context = createTestContext();
// Wait should throw SuspendExecutionException
assertThrows(SuspendExecutionException.class, () -> {
context.wait(null, Duration.ofMinutes(5));
});
}
@Test
void testWaitReplay() {
// Create context with completed wait operation
var existingOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.build();
var context = createTestContext(List.of(existingOp));
// Wait should complete immediately (no exception)
assertDoesNotThrow(() -> {
context.wait(null, Duration.ofMinutes(5));
});
}
@Test
void testCombinedSyncAsyncWait() throws Exception {
var context = createTestContext();
// Execute sync step
var syncResult = context.step("sync-step", String.class, stepCtx -> "Sync Done");
assertEquals("Sync Done", syncResult);
// Execute async step
var asyncFuture = context.stepAsync("async-step", Integer.class, stepCtx -> 42);
assertEquals(42, asyncFuture.get());
// Receiving results from `get` calls doesn't mean the step threads have been deregistered.So we wait for 500ms
// to make sure the above step threads have been deregistered. Otherwise, the wait call will be stuck forever
// and SuspendExecutionException will be thrown from the step thread
Thread.sleep(500);
// Wait should suspend (throw exception)
assertThrows(SuspendExecutionException.class, () -> {
context.wait(null, Duration.ofSeconds(30));
});
}
@Test
void testCombinedReplay() throws Exception {
// Create context with all operations completed
var syncOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().result("\"Replayed Sync\"").build())
.build();
var asyncOp = Operation.builder()
.id(OPERATION_ID2)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().result("100").build())
.build();
var waitOp = Operation.builder()
.id(OPERATION_ID3)
.status(OperationStatus.SUCCEEDED)
.build();
var context = createTestContext(List.of(syncOp, asyncOp, waitOp));
// All operations should replay from cache
var syncResult = context.step("sync-step", String.class, stepCtx -> "New Sync");
assertEquals("Replayed Sync", syncResult);
var asyncFuture = context.stepAsync("async-step", Integer.class, stepCtx -> 999);
assertEquals(100, asyncFuture.get());
// Wait should complete immediately (no exception)
assertDoesNotThrow(() -> {
context.wait(null, Duration.ofSeconds(30));
});
}
@Test
void testNamedWait() {
var ctx = createTestContext();
// Named wait should throw SuspendExecutionException
assertThrows(SuspendExecutionException.class, () -> {
ctx.wait("my-wait", Duration.ofSeconds(5));
});
// Verify it works without error (basic functionality test)
assertDoesNotThrow(() -> {
var ctx2 = createTestContext();
try {
ctx2.wait("another-wait", Duration.ofMinutes(1));
} catch (SuspendExecutionException e) {
// Expected - this means the method worked
}
});
}
@Test
void testStepWithTypeToken() {
var context = createTestContext();
List<String> result =
context.step("test-list", new TypeToken<List<String>>() {}, stepCtx -> List.of("a", "b", "c"));
assertNotNull(result);
assertEquals(3, result.size());
assertEquals("a", result.get(0));
}
@Test
void testStepWithTypeTokenReplay() {
// Create context with existing operation
var existingOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder()
.result("[\"cached1\",\"cached2\"]")
.build())
.build();
var context = createTestContext(List.of(existingOp));
// This should return cached result, not execute the function
List<String> result =
context.step("test-list", new TypeToken<List<String>>() {}, stepCtx -> List.of("new1", "new2"));
assertEquals(2, result.size());
assertEquals("cached1", result.get(0));
assertEquals("cached2", result.get(1));
}
@Test
void testStepWithTypeTokenAndConfig() {
var context = createTestContext();
List<Integer> result = context.step(
"test-numbers",
new TypeToken<List<Integer>>() {},
stepCtx -> List.of(1, 2, 3),
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.DEFAULT)
.build());
assertNotNull(result);
assertEquals(3, result.size());
assertEquals(1, result.get(0));
}
@Test
void testStepAsyncWithTypeToken() throws Exception {
var context = createTestContext();
DurableFuture<List<String>> future =
context.stepAsync("async-list", new TypeToken<List<String>>() {}, stepCtx -> List.of("x", "y", "z"));
assertNotNull(future);
List<String> result = future.get();
assertEquals(3, result.size());
assertEquals("x", result.get(0));
}
@Test
void testStepAsyncWithTypeTokenReplay() throws Exception {
// Create context with existing operation
var existingOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder()
.result("[\"async-cached1\",\"async-cached2\"]")
.build())
.build();
var context = createTestContext(List.of(existingOp));
// This should return cached result immediately
DurableFuture<List<String>> future = context.stepAsync(
"async-list", new TypeToken<List<String>>() {}, stepCtx -> List.of("async-new1", "async-new2"));
List<String> result = future.get();
assertEquals(2, result.size());
assertEquals("async-cached1", result.get(0));
assertEquals("async-cached2", result.get(1));
}
@Test
void testStepAsyncWithTypeTokenAndConfig() throws Exception {
var context = createTestContext();
DurableFuture<List<Integer>> future = context.stepAsync(
"async-numbers",
new TypeToken<List<Integer>>() {},
stepCtx -> List.of(10, 20, 30),
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.DEFAULT)
.build());
assertNotNull(future);
List<Integer> result = future.get();
assertEquals(3, result.size());
assertEquals(10, result.get(0));
}
@Test
void testWaitForCallback_allCompleted() {
var contextOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.contextDetails(ContextDetails.builder().replayChildren(true).build())
.build();
var callbackOp = Operation.builder()
.id(TestUtils.hashOperationId(OPERATION_ID1 + "-1"))
.status(OperationStatus.SUCCEEDED)
.callbackDetails(CallbackDetails.builder()
.result("\"result\"")
.callbackId("callback-id")
.build())
.build();
var stepOp = Operation.builder()
.id(TestUtils.hashOperationId(OPERATION_ID1 + "-2"))
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().build())
.build();
var context = createTestContext(List.of(contextOp, callbackOp, stepOp));
// waitForCallback should throw SuspendExecutionException on first execution
var result = context.waitForCallback("approval", String.class, (callbackId, stepCtx) -> {
// step already completed so this should not be called
fail();
});
assertEquals("result", result);
}
@Test
void testWaitForCallback_contextCompleted() {
var contextOp = Operation.builder()
.id(OPERATION_ID1)
.status(OperationStatus.SUCCEEDED)
.contextDetails(ContextDetails.builder()
.result("\"result\"")
.replayChildren(false)
.build())
.build();
var context = createTestContext(List.of(contextOp));
// waitForCallback should throw SuspendExecutionException on first execution
var result = context.waitForCallback("approval", String.class, (callbackId, stepCtx) -> {
// step already completed so this should not be called
fail();
});
assertEquals("result", result);
}
@Test
void testWaitForCallback_callbackAndStepCompleted() {
var contextOp = Operation.builder()
.id(OPERATION_ID1)
.name("approval")
.status(OperationStatus.STARTED)
.type(OperationType.CONTEXT)
.subType(OperationSubType.WAIT_FOR_CALLBACK.getValue())
.build();
var callbackOp = Operation.builder()
.id(TestUtils.hashOperationId(OPERATION_ID1 + "-1"))
.status(OperationStatus.SUCCEEDED)
.callbackDetails(CallbackDetails.builder()
.result("\"result\"")
.callbackId("callback-id")
.build())
.build();
var stepOp = Operation.builder()
.id(TestUtils.hashOperationId(OPERATION_ID1 + "-2"))
.status(OperationStatus.SUCCEEDED)
.stepDetails(StepDetails.builder().build())
.build();
var context = createTestContext(List.of(contextOp, callbackOp, stepOp));
// waitForCallback should throw SuspendExecutionException on first execution
var result = context.waitForCallback("approval", String.class, (callbackId, stepCtx) -> {
// step already completed so this should not be called
fail();
});
assertEquals("result", result);
}
}