Skip to content

Commit 42f4c3e

Browse files
committed
Finalize tests
1 parent 8dedfd4 commit 42f4c3e

3 files changed

Lines changed: 88 additions & 5 deletions

File tree

examples/src/main/java/com/amazonaws/lambda/durable/examples/CallbackExample.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,23 @@ public String handleRequest(ApprovalRequest input, DurableContext context) {
3636
});
3737

3838
// Step 2: Create callback for external approval
39-
// Configure with 5min timeout
39+
// Use timeout from input if provided, otherwise default to 5 minutes
40+
var timeout = input.timeoutSeconds() != null
41+
? Duration.ofSeconds(input.timeoutSeconds())
42+
: Duration.ofMinutes(5);
43+
4044
var config = CallbackConfig.builder()
41-
.timeout(Duration.ofMinutes(5))
45+
.timeout(timeout)
4246
.build();
4347

4448
var callback = context.createCallback("approval", String.class, config);
4549

4650
// Step 2.5: Log AWS CLI command to complete the callback
4751
context.step("log-callback-command", Void.class, () -> {
4852
var callbackId = callback.callbackId();
53+
// The result must be base64-encoded JSON
4954
var command = String.format(
50-
"aws lambda send-durable-execution-callback-success --callback-id %s --payload '{\"result\":\"approved\"}'",
55+
"aws lambda send-durable-execution-callback-success --callback-id %s --result $(echo -n '\"approved\"' | base64)",
5156
callbackId
5257
);
5358
context.getLogger().info("To complete this callback, run: {}", command);
@@ -67,4 +72,9 @@ public String handleRequest(ApprovalRequest input, DurableContext context) {
6772
}
6873

6974
/** Input for the approval workflow. */
70-
record ApprovalRequest(String description, double amount) {}
75+
record ApprovalRequest(String description, double amount, Integer timeoutSeconds) {
76+
// Convenience constructor for default timeout
77+
public ApprovalRequest(String description, double amount) {
78+
this(description, amount, null);
79+
}
80+
}

examples/src/test/java/com/amazonaws/lambda/durable/examples/CloudBasedIntegrationTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.jupiter.api.Test;
1212
import org.junit.jupiter.api.condition.EnabledIf;
1313
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
14+
import software.amazon.awssdk.services.lambda.model.OperationStatus;
1415
import software.amazon.awssdk.services.sts.StsClient;
1516

1617
@EnabledIf("isEnabled")
@@ -260,4 +261,66 @@ void testCallbackExample() throws Exception {
260261
assertNotNull(execution.getOperation("log-callback-command"));
261262
assertNotNull(execution.getOperation("process-approval"));
262263
}
264+
265+
@Test
266+
void testCallbackExampleWithFailure() throws Exception {
267+
var runner = CloudDurableTestRunner.create(
268+
arn("callback-example"), ApprovalRequest.class, String.class);
269+
270+
// Start async execution
271+
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0));
272+
273+
// Wait for callback to appear
274+
execution.pollUntil(exec -> exec.hasCallback("approval"));
275+
276+
// Get callback ID
277+
var callbackId = execution.getCallbackId("approval");
278+
assertNotNull(callbackId);
279+
280+
// Fail the callback using AWS SDK
281+
var lambda = software.amazon.awssdk.services.lambda.LambdaClient.create();
282+
lambda.sendDurableExecutionCallbackFailure(req -> req
283+
.callbackId(callbackId)
284+
.error(err -> err
285+
.errorType("ApprovalRejected")
286+
.errorMessage("Approval rejected by manager")));
287+
288+
// Wait for execution to complete
289+
var result = execution.pollUntilComplete();
290+
assertEquals(ExecutionStatus.FAILED, result.getStatus());
291+
292+
// Verify the callback operation shows failure
293+
var approvalOp = execution.getOperation("approval");
294+
assertNotNull(approvalOp);
295+
var callbackDetails = approvalOp.getCallbackDetails();
296+
assertNotNull(callbackDetails);
297+
assertNotNull(callbackDetails.error());
298+
// Error message is redacted in the response, just verify error exists
299+
assertTrue(callbackDetails.error().toString().contains("ErrorObject"));
300+
}
301+
302+
@Test
303+
void testCallbackExampleWithTimeout() throws Exception {
304+
var runner = CloudDurableTestRunner.create(
305+
arn("callback-example"), ApprovalRequest.class, String.class);
306+
307+
// Start async execution with 10 second timeout
308+
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0, 10));
309+
310+
// Wait for callback to appear
311+
execution.pollUntil(exec -> exec.hasCallback("approval"));
312+
313+
// Get callback ID but don't complete it - let it timeout
314+
var callbackId = execution.getCallbackId("approval");
315+
assertNotNull(callbackId);
316+
317+
// Wait for execution to complete (should timeout after 10 seconds)
318+
var result = execution.pollUntilComplete();
319+
assertEquals(ExecutionStatus.FAILED, result.getStatus());
320+
321+
// Verify the callback operation shows timeout status
322+
var approvalOp = execution.getOperation("approval");
323+
assertNotNull(approvalOp);
324+
assertEquals(OperationStatus.TIMED_OUT, approvalOp.getStatus());
325+
}
263326
}

sdk/src/main/java/com/amazonaws/lambda/durable/exception/CallbackFailedException.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
/** Exception thrown when a callback fails due to an error from the external system. */
99
public class CallbackFailedException extends DurableExecutionException {
1010
public CallbackFailedException(ErrorObject error) {
11-
super(error.errorType() + ": " + error.errorMessage());
11+
super(buildMessage(error));
1212
}
1313

1414
public CallbackFailedException(String message) {
1515
super(message);
1616
}
17+
18+
private static String buildMessage(ErrorObject error) {
19+
var errorType = error.errorType();
20+
var errorMessage = error.errorMessage();
21+
22+
if (errorType != null && !errorType.isEmpty()) {
23+
return errorType + ": " + errorMessage;
24+
}
25+
return errorMessage;
26+
}
1727
}

0 commit comments

Comments
 (0)