Skip to content

Commit 24bc5ab

Browse files
authored
add a negative case for WaitForCallback (#277)
1 parent 03fddad commit 24bc5ab

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package software.amazon.lambda.durable.examples.callback;
4+
5+
import software.amazon.lambda.durable.DurableContext;
6+
import software.amazon.lambda.durable.DurableHandler;
7+
import software.amazon.lambda.durable.TypeToken;
8+
import software.amazon.lambda.durable.config.StepConfig;
9+
import software.amazon.lambda.durable.config.WaitForCallbackConfig;
10+
import software.amazon.lambda.durable.examples.types.ApprovalRequest;
11+
import software.amazon.lambda.durable.exception.SerDesException;
12+
import software.amazon.lambda.durable.serde.JacksonSerDes;
13+
14+
public class WaitForCallbackFailedExample extends DurableHandler<ApprovalRequest, String> {
15+
16+
@Override
17+
public String handleRequest(ApprovalRequest input, DurableContext context) {
18+
19+
String approvalResult;
20+
21+
try {
22+
approvalResult = context.waitForCallback(
23+
"preapproval",
24+
String.class,
25+
(callbackId, ctx) -> {
26+
ctx.getLogger().info("Sending callback {} to preapproval system", callbackId);
27+
throw new RuntimeException("Submitter failed with an exception");
28+
},
29+
WaitForCallbackConfig.builder()
30+
.stepConfig(StepConfig.builder()
31+
.serDes(new FailedSerDes())
32+
.build())
33+
.build());
34+
} catch (Exception ex) {
35+
return ex.getClass().getSimpleName() + ":" + ex.getMessage();
36+
}
37+
38+
return approvalResult;
39+
}
40+
41+
private static class FailedSerDes extends JacksonSerDes {
42+
@Override
43+
public <T> T deserialize(String json, TypeToken<T> typeToken) {
44+
T result = super.deserialize(json, typeToken);
45+
if (result instanceof RuntimeException ex) {
46+
throw new SerDesException("Deserialization failed", ex);
47+
}
48+
return result;
49+
}
50+
}
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package software.amazon.lambda.durable.examples.callback;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import org.junit.jupiter.api.Test;
8+
import software.amazon.lambda.durable.examples.types.ApprovalRequest;
9+
import software.amazon.lambda.durable.model.ExecutionStatus;
10+
import software.amazon.lambda.durable.testing.LocalDurableTestRunner;
11+
12+
class WaitForCallbackFailedExampleTest {
13+
14+
@Test
15+
void testWaitForCallbackFailedExample() {
16+
var handler = new WaitForCallbackFailedExample();
17+
var runner = LocalDurableTestRunner.create(ApprovalRequest.class, handler);
18+
19+
var input = new ApprovalRequest("New laptop", 1500.00);
20+
21+
// First run - prepares request and creates callback, then suspends
22+
var result = runner.runUntilComplete(input);
23+
24+
assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus());
25+
26+
// Verify the callback was created
27+
assertEquals(
28+
"CallbackSubmitterException:Step failed with error of type java.lang.RuntimeException. Message: Submitter failed with an exception",
29+
result.getResult(String.class));
30+
}
31+
}

0 commit comments

Comments
 (0)