Skip to content

Commit 4a03c93

Browse files
committed
fix cloud test cases for waitForCallback
1 parent 5e210a3 commit 4a03c93

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

examples/src/test/java/software/amazon/lambda/durable/examples/CloudBasedIntegrationTest.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,15 @@ void testErrorHandlingExample() {
273273
}
274274

275275
@Test
276-
void testCallbackExample() throws Exception {
276+
void testCallbackExample() {
277+
// happy case covering both createCallback (approval) and waitForCallback (preapproval-callback)
277278
var runner = CloudDurableTestRunner.create(arn("callback-example"), ApprovalRequest.class, String.class);
278279
var lambda = LambdaClient.create();
279280

280281
// Start async execution
281282
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0));
282283

284+
// Complete the preapproval callback
283285
execution.pollUntil(exec -> exec.hasCallback("preapproval-callback"));
284286
var preapprovalCallbackId = execution.getCallbackId("preapproval-callback");
285287
lambda.sendDurableExecutionCallbackSuccess(
@@ -316,10 +318,16 @@ void testCallbackExample() throws Exception {
316318
@Test
317319
void testCallbackExampleWithFailure() {
318320
var runner = CloudDurableTestRunner.create(arn("callback-example"), ApprovalRequest.class, String.class);
321+
var lambda = LambdaClient.create();
319322

320323
// Start async execution
321324
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0));
322325

326+
execution.pollUntil(exec -> exec.hasCallback("preapproval-callback"));
327+
var preapprovalCallbackId = execution.getCallbackId("preapproval-callback");
328+
lambda.sendDurableExecutionCallbackSuccess(
329+
req -> req.callbackId(preapprovalCallbackId).result(SdkBytes.fromUtf8String("\"preapproved\"")));
330+
323331
// Wait for callback to appear
324332
execution.pollUntil(exec -> exec.hasCallback("approval"));
325333

@@ -328,7 +336,6 @@ void testCallbackExampleWithFailure() {
328336
assertNotNull(callbackId);
329337

330338
// Fail the callback using AWS SDK
331-
var lambda = LambdaClient.create();
332339
lambda.sendDurableExecutionCallbackFailure(req -> req.callbackId(callbackId)
333340
.error(err -> err.errorType("ApprovalRejected").errorMessage("Approval rejected by manager")));
334341

@@ -349,10 +356,16 @@ void testCallbackExampleWithFailure() {
349356
@Test
350357
void testCallbackExampleWithTimeout() {
351358
var runner = CloudDurableTestRunner.create(arn("callback-example"), ApprovalRequest.class, String.class);
359+
var lambda = LambdaClient.create();
352360

353361
// Start async execution with 10 second timeout
354362
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0, 10));
355363

364+
execution.pollUntil(exec -> exec.hasCallback("preapproval-callback"));
365+
var preapprovalCallbackId = execution.getCallbackId("preapproval-callback");
366+
lambda.sendDurableExecutionCallbackSuccess(
367+
req -> req.callbackId(preapprovalCallbackId).result(SdkBytes.fromUtf8String("\"preapproved\"")));
368+
356369
// Wait for callback to appear
357370
execution.pollUntil(exec -> exec.hasCallback("approval"));
358371

@@ -370,6 +383,37 @@ void testCallbackExampleWithTimeout() {
370383
assertEquals(OperationStatus.TIMED_OUT, approvalOp.getStatus());
371384
}
372385

386+
@Test
387+
void testCallbackExampleWithWaitForCallbackFailure() {
388+
// fail the waitForCallback (preapproval-callback) callback
389+
var runner = CloudDurableTestRunner.create(arn("callback-example"), ApprovalRequest.class, String.class);
390+
var lambda = LambdaClient.create();
391+
392+
// Start async execution with 10 second timeout
393+
var execution = runner.startAsync(new ApprovalRequest("Purchase order", 5000.0, 10));
394+
395+
execution.pollUntil(exec -> exec.hasCallback("preapproval-callback"));
396+
var preapprovalCallbackId = execution.getCallbackId("preapproval-callback");
397+
lambda.sendDurableExecutionCallbackFailure(
398+
req -> req.callbackId(preapprovalCallbackId).error(err -> err.errorMessage("preapproval denied")));
399+
400+
// Wait for callback to appear
401+
execution.pollUntil(exec -> exec.hasCallback("approval"));
402+
403+
// Get callback ID but don't complete it - let it timeout
404+
var callbackId = execution.getCallbackId("approval");
405+
assertNotNull(callbackId);
406+
407+
// Wait for execution to complete (should timeout after 10 seconds)
408+
var result = execution.pollUntilComplete();
409+
assertEquals(ExecutionStatus.FAILED, result.getStatus());
410+
411+
// Verify the callback operation shows timeout status
412+
var approvalOp = execution.getOperation("preapproval-callback");
413+
assertNotNull(approvalOp);
414+
assertEquals(OperationStatus.FAILED, approvalOp.getStatus());
415+
}
416+
373417
@Test
374418
void testChildContextExample() {
375419
var runner = CloudDurableTestRunner.create(arn("child-context-example"), GreetingRequest.class, String.class);

0 commit comments

Comments
 (0)