-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExceptionIntegrationTest.java
More file actions
259 lines (217 loc) · 10.1 KB
/
Copy pathExceptionIntegrationTest.java
File metadata and controls
259 lines (217 loc) · 10.1 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
// 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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import software.amazon.lambda.durable.config.StepConfig;
import software.amazon.lambda.durable.config.StepSemantics;
import software.amazon.lambda.durable.exception.StepInterruptedException;
import software.amazon.lambda.durable.model.ExecutionStatus;
import software.amazon.lambda.durable.retry.RetryStrategies;
import software.amazon.lambda.durable.testing.LocalDurableTestRunner;
/** Integration tests for exception handling scenarios documented in the README. */
class ExceptionIntegrationTest {
@Test
void testStepFailedExceptionThrownAfterRetryExhaustion() {
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
return ctx.step(
"always-fails",
String.class,
stepCtx -> {
throw new RuntimeException("Service unavailable");
},
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
});
var result = runner.run("test");
assertEquals(ExecutionStatus.FAILED, result.getStatus());
}
@Test
void testStepFailedExceptionCanBeCaughtWithFallback() {
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
try {
return ctx.step(
"primary",
String.class,
stepCtx -> {
throw new RuntimeException("Primary failed");
},
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
} catch (RuntimeException e) {
return ctx.step("fallback", String.class, stepCtx -> "fallback-result");
}
});
var result = runner.run("test");
assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus());
assertEquals("fallback-result", result.getResult(String.class));
}
@Test
void testOriginalExceptionTypeIsPreserved() {
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
ctx.step(
"throws-illegal-arg",
String.class,
stepCtx -> {
throw new IllegalArgumentException("Invalid parameter");
},
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
return "should-not-reach";
});
// First run - exception is thrown and checkpointed
var result = runner.run("test");
assertEquals(ExecutionStatus.FAILED, result.getStatus());
// Verify the operation failed with the correct exception type
var failedOp = result.getOperation("throws-illegal-arg");
assertNotNull(failedOp);
var error = failedOp.getError();
assertNotNull(error);
assertEquals("java.lang.IllegalArgumentException", error.errorType());
assertEquals("Invalid parameter", error.errorMessage());
// Verify stackTrace is preserved
assertNotNull(error.stackTrace());
assertTrue(error.stackTrace().size() > 0, "Stack trace should not be empty");
// Verify errorData contains serialized exception
assertNotNull(error.errorData());
assertTrue(error.errorData().contains("Invalid parameter"), "errorData should contain the exception message");
}
@Test
void testOriginalExceptionTypeCanBeCaughtSpecifically() {
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
try {
return ctx.step(
"throws-illegal-state",
String.class,
stepCtx -> {
throw new IllegalStateException("Invalid state");
},
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
} catch (IllegalStateException e) {
// Catch specific exception type
return ctx.step("handle-illegal-state", String.class, stepCtx -> "recovered-from-illegal-state");
} catch (Exception e) {
// This should NOT be caught
return ctx.step("handle-illegal-arg", String.class, stepCtx -> "recovered-from-exception");
}
});
var result = runner.runUntilComplete("test");
assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus());
assertEquals("recovered-from-illegal-state", result.getResult(String.class));
}
@Test
void testCustomExceptionTypeIsPreserved() {
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
ctx.step(
"throws-custom",
String.class,
stepCtx -> {
throw new CustomBusinessException("Business rule violated", 42);
},
StepConfig.builder()
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
return "should-not-reach";
});
var result = runner.runUntilComplete("test");
assertEquals(ExecutionStatus.FAILED, result.getStatus());
// Verify the operation failed with the correct exception type
var failedOp = result.getOperation("throws-custom");
assertNotNull(failedOp);
var error = failedOp.getError();
assertNotNull(error);
assertTrue(error.errorType().contains("CustomBusinessException"));
assertEquals("Business rule violated", error.errorMessage());
}
@Test
void testStepInterruptedExceptionForAtMostOnceAfterCheckpointLoss() {
var executionCount = new AtomicInteger(0);
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
return ctx.step(
"at-most-once-step",
String.class,
stepCtx -> {
executionCount.incrementAndGet();
return "result";
},
StepConfig.builder()
.semanticsPerRetry(StepSemantics.AT_MOST_ONCE_PER_RETRY)
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
});
// First run succeeds
runner.run("test");
assertEquals(1, executionCount.get());
// Simulate checkpoint loss (step started but result not saved)
runner.resetCheckpointToStarted("at-most-once-step");
// Second run should fail with StepInterruptedException (not re-execute)
var result = runner.run("test");
assertEquals(ExecutionStatus.FAILED, result.getStatus());
assertEquals(1, executionCount.get()); // Should NOT have re-executed
assertEquals(result.getError().get().errorType(), StepInterruptedException.class.getName());
}
@Test
void testStepInterruptedExceptionCanBeCaughtForRecovery() {
var executionCount = new AtomicInteger(0);
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
try {
return ctx.step(
"payment",
String.class,
stepCtx -> {
executionCount.incrementAndGet();
return "payment-success";
},
StepConfig.builder()
.semanticsPerRetry(StepSemantics.AT_MOST_ONCE_PER_RETRY)
.retryStrategy(RetryStrategies.Presets.NO_RETRY)
.build());
} catch (StepInterruptedException e) {
// Recovery: check external status and return verified result
return ctx.step("verify-payment", String.class, stepCtx -> "verified-payment");
}
});
// First run succeeds
runner.run("test");
// Simulate interruption (step started but result not checkpointed)
runner.resetCheckpointToStarted("payment");
// Second run catches exception and recovers
var result = runner.run("test");
assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus());
assertEquals("verified-payment", result.getResult(String.class));
}
@Test
void testNonDeterministicExceptionOnStepNameChange() {
var useNewName = new AtomicInteger(0);
var runner = LocalDurableTestRunner.create(String.class, (input, ctx) -> {
var stepName = useNewName.get() == 0 ? "original-name" : "changed-name";
return ctx.step(stepName, String.class, stepCtx -> "result");
});
// First run with original name
runner.run("test");
// Change step name for replay
useNewName.set(1);
// Replay should detect non-determinism
var result = runner.run("test");
assertEquals(ExecutionStatus.FAILED, result.getStatus());
}
// Custom exception for testing exception preservation
public static class CustomBusinessException extends RuntimeException {
private final int errorCode;
public CustomBusinessException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}
}