-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathConcurrencyOperationTest.java
More file actions
310 lines (273 loc) · 12.7 KB
/
ConcurrencyOperationTest.java
File metadata and controls
310 lines (273 loc) · 12.7 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable.operation;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.lambda.model.ContextDetails;
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.lambda.durable.DurableConfig;
import software.amazon.lambda.durable.DurableContext;
import software.amazon.lambda.durable.TypeToken;
import software.amazon.lambda.durable.context.DurableContextImpl;
import software.amazon.lambda.durable.execution.ExecutionManager;
import software.amazon.lambda.durable.execution.OperationIdGenerator;
import software.amazon.lambda.durable.execution.ThreadContext;
import software.amazon.lambda.durable.execution.ThreadType;
import software.amazon.lambda.durable.model.ConcurrencyCompletionStatus;
import software.amazon.lambda.durable.model.OperationIdentifier;
import software.amazon.lambda.durable.model.OperationSubType;
import software.amazon.lambda.durable.serde.JacksonSerDes;
import software.amazon.lambda.durable.serde.SerDes;
class ConcurrencyOperationTest {
private static final SerDes SER_DES = new JacksonSerDes();
private static final String OPERATION_ID = "op-1";
private static final TypeToken<Void> RESULT_TYPE = TypeToken.get(Void.class);
private DurableContextImpl durableContext;
private DurableContextImpl childContext;
private ExecutionManager executionManager;
private AtomicInteger operationIdCounter;
private OperationIdGenerator mockIdGenerator;
@BeforeEach
void setUp() {
durableContext = mock(DurableContextImpl.class);
executionManager = mock(ExecutionManager.class);
operationIdCounter = new AtomicInteger(0);
var childContext = mock(DurableContextImpl.class);
this.childContext = childContext;
when(childContext.getExecutionManager()).thenReturn(executionManager);
when(childContext.getDurableConfig())
.thenReturn(DurableConfig.builder()
.withExecutorService(Executors.newCachedThreadPool())
.build());
when(durableContext.getExecutionManager()).thenReturn(executionManager);
when(durableContext.getDurableConfig())
.thenReturn(DurableConfig.builder()
.withExecutorService(Executors.newCachedThreadPool())
.build());
when(durableContext.createChildContext(anyString(), anyString())).thenReturn(childContext);
when(durableContext.createChildContextWithoutSettingThreadContext(anyString(), anyString()))
.thenReturn(childContext);
when(executionManager.getCurrentThreadContext()).thenReturn(new ThreadContext("Root", ThreadType.CONTEXT));
mockIdGenerator = mock(OperationIdGenerator.class);
when(mockIdGenerator.nextOperationId()).thenAnswer(inv -> "child-" + operationIdCounter.incrementAndGet());
// All child operations are NOT in replay
when(executionManager.getOperationAndUpdateReplayState(anyString())).thenReturn(null);
// Simulate the real backend: the parent concurrency operation is available in storage after completion
// so that waitForOperationCompletion() can find it. TestConcurrencyOperation.handleSuccess/Failure are no-ops
// (no checkpoint sent), so we stub this unconditionally for OPERATION_ID.
when(executionManager.getOperationAndUpdateReplayState(OPERATION_ID))
.thenReturn(Operation.builder()
.id(OPERATION_ID)
.name("test-concurrency")
.type(OperationType.CONTEXT)
.subType(OperationSubType.PARALLEL.getValue())
.status(OperationStatus.SUCCEEDED)
.build());
when(executionManager.sendOperationUpdate(any())).thenReturn(CompletableFuture.completedFuture(null));
}
private TestConcurrencyOperation createOperation(int maxConcurrency, int minSuccessful, int toleratedFailureCount)
throws Exception {
TestConcurrencyOperation testConcurrencyOperation = new TestConcurrencyOperation(
OperationIdentifier.of(
OPERATION_ID, "test-concurrency", OperationType.CONTEXT, OperationSubType.PARALLEL),
RESULT_TYPE,
SER_DES,
durableContext,
maxConcurrency,
minSuccessful,
toleratedFailureCount);
setOperationIdGenerator(testConcurrencyOperation, mockIdGenerator);
return testConcurrencyOperation;
}
private void setOperationIdGenerator(ConcurrencyOperation<?> op, OperationIdGenerator mockGenerator)
throws Exception {
Field field = ConcurrencyOperation.class.getDeclaredField("operationIdGenerator");
field.setAccessible(true);
field.set(op, mockGenerator);
}
// ===== Callback cycle tests =====
@Test
void allChildrenAlreadySucceed_callsHandleSuccess() throws Exception {
when(executionManager.getOperationAndUpdateReplayState("child-1"))
.thenReturn(Operation.builder()
.id("child-1")
.name("branch-1")
.type(OperationType.CONTEXT)
.subType(OperationSubType.PARALLEL_BRANCH.getValue())
.status(OperationStatus.SUCCEEDED)
.contextDetails(
ContextDetails.builder().result("\"result-1\"").build())
.build());
when(executionManager.getOperationAndUpdateReplayState("child-2"))
.thenReturn(Operation.builder()
.id("child-2")
.name("branch-2")
.type(OperationType.CONTEXT)
.subType(OperationSubType.PARALLEL_BRANCH.getValue())
.status(OperationStatus.SUCCEEDED)
.contextDetails(
ContextDetails.builder().result("\"result-2\"").build())
.build());
var functionCalled = new AtomicBoolean(false);
var op = createOperation(-1, -1, 0);
op.addItem(
"branch-1",
ctx -> {
functionCalled.set(true);
return "result-1";
},
TypeToken.get(String.class),
SER_DES);
op.addItem(
"branch-2",
ctx -> {
functionCalled.set(true);
return "result-2";
},
TypeToken.get(String.class),
SER_DES);
op.exposedJoin();
assertTrue(op.isSuccessHandled());
assertFalse(op.isFailureHandled());
assertEquals(2, op.getSucceededCount());
assertEquals(0, op.getFailedCount());
assertFalse(functionCalled.get(), "Functions should not be called during SUCCEEDED replay");
}
@Test
void singleChildAlreadySucceeds_fullCycle() throws Exception {
when(executionManager.getOperationAndUpdateReplayState("child-1"))
.thenReturn(Operation.builder()
.id("child-1")
.name("only-branch")
.type(OperationType.CONTEXT)
.subType(OperationSubType.PARALLEL_BRANCH.getValue())
.status(OperationStatus.SUCCEEDED)
.contextDetails(
ContextDetails.builder().result("\"done\"").build())
.build());
var functionCalled = new AtomicBoolean(false);
var op = createOperation(-1, 1, 0);
op.addItem(
"only-branch",
ctx -> {
functionCalled.set(true);
return "done";
},
TypeToken.get(String.class),
SER_DES);
op.exposedJoin();
assertTrue(op.isSuccessHandled());
assertEquals(1, op.getSucceededCount());
assertEquals(0, op.getFailedCount());
assertFalse(functionCalled.get(), "Function should not be called during SUCCEEDED replay");
}
@Test
void addItem_usesRootChildContextAsParent() throws Exception {
var op = createOperation(-1, -1, 0);
op.addItem("branch-1", ctx -> "result", TypeToken.get(String.class), SER_DES);
// rootContext is created via durableContext.createChildContext(...) in the constructor,
// so the parentContext passed to createItem must be that child context, not durableContext itself
assertNotSame(durableContext, op.getLastParentContext());
assertSame(childContext, op.getLastParentContext());
}
// ===== Test subclass =====
static class TestConcurrencyOperation extends ConcurrencyOperation<Void> {
private boolean successHandled = false;
private boolean failureHandled = false;
private final AtomicInteger executingCount = new AtomicInteger(0);
private DurableContextImpl lastParentContext;
TestConcurrencyOperation(
OperationIdentifier operationIdentifier,
TypeToken<Void> resultTypeToken,
SerDes resultSerDes,
DurableContextImpl durableContext,
int maxConcurrency,
int minSuccessful,
int toleratedFailureCount) {
super(
operationIdentifier,
resultTypeToken,
resultSerDes,
durableContext,
maxConcurrency,
minSuccessful,
toleratedFailureCount);
}
@Override
protected <R> ChildContextOperation<R> createItem(
String operationId,
String name,
Function<DurableContext, R> function,
TypeToken<R> resultType,
SerDes serDes,
DurableContextImpl parentContext) {
lastParentContext = parentContext;
return new ChildContextOperation<R>(
OperationIdentifier.of(operationId, name, OperationType.CONTEXT, OperationSubType.PARALLEL_BRANCH),
function,
resultType,
serDes,
parentContext,
this) {
@Override
public void execute() {
executingCount.incrementAndGet();
super.execute();
}
};
}
@Override
protected void handleSuccess() {
successHandled = true;
// Simulate the checkpoint ACK that a real subclass would receive after sendOperationUpdate.
// This drives completionFuture to completion so waitForOperationCompletion() unblocks.
onCheckpointComplete(Operation.builder()
.id(getOperationId())
.status(OperationStatus.SUCCEEDED)
.build());
}
@Override
protected void handleFailure(ConcurrencyCompletionStatus completionStatus) {
failureHandled = true;
onCheckpointComplete(Operation.builder()
.id(getOperationId())
.status(OperationStatus.SUCCEEDED) // always success for parallel
.build());
}
@Override
protected void start() {}
@Override
protected void replay(Operation existing) {}
@Override
public Void get() {
return null;
}
void exposedJoin() {
join();
}
int getExecutingCount() {
return executingCount.get();
}
boolean isSuccessHandled() {
return successHandled;
}
boolean isFailureHandled() {
return failureHandled;
}
DurableContextImpl getLastParentContext() {
return lastParentContext;
}
}
}