|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.amazonaws.lambda.durable.testing; |
| 5 | + |
| 6 | +import com.amazonaws.lambda.durable.model.ExecutionStatus; |
| 7 | +import java.time.Duration; |
| 8 | +import java.time.Instant; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.TimeoutException; |
| 11 | +import java.util.function.Predicate; |
| 12 | +import software.amazon.awssdk.services.lambda.LambdaClient; |
| 13 | +import software.amazon.awssdk.services.lambda.model.Event; |
| 14 | +import software.amazon.awssdk.services.lambda.model.GetDurableExecutionHistoryRequest; |
| 15 | +import software.amazon.awssdk.services.lambda.model.ResourceNotFoundException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Handle for an asynchronously executing durable function. |
| 19 | + * Allows incremental polling and inspection of execution state. |
| 20 | + */ |
| 21 | +public class AsyncExecution<O> { |
| 22 | + private final String executionArn; |
| 23 | + private final LambdaClient lambdaClient; |
| 24 | + private final Class<O> outputType; |
| 25 | + private final Duration pollInterval; |
| 26 | + private final Duration timeout; |
| 27 | + private final HistoryEventProcessor processor; |
| 28 | + private List<Event> currentHistory; |
| 29 | + private TestResult<O> currentResult; |
| 30 | + |
| 31 | + public AsyncExecution( |
| 32 | + String executionArn, |
| 33 | + LambdaClient lambdaClient, |
| 34 | + Class<O> outputType, |
| 35 | + Duration pollInterval, |
| 36 | + Duration timeout) { |
| 37 | + this.executionArn = executionArn; |
| 38 | + this.lambdaClient = lambdaClient; |
| 39 | + this.outputType = outputType; |
| 40 | + this.pollInterval = pollInterval; |
| 41 | + this.timeout = timeout; |
| 42 | + this.processor = new HistoryEventProcessor(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Poll execution history until the given condition is met. |
| 47 | + * |
| 48 | + * @param condition predicate to test on each poll |
| 49 | + * @return this execution for chaining |
| 50 | + * @throws TimeoutException if condition not met within timeout |
| 51 | + */ |
| 52 | + public AsyncExecution<O> pollUntil(Predicate<AsyncExecution<O>> condition) throws TimeoutException { |
| 53 | + var startTime = Instant.now(); |
| 54 | + |
| 55 | + while (Duration.between(startTime, Instant.now()).compareTo(timeout) < 0) { |
| 56 | + refreshHistory(); |
| 57 | + |
| 58 | + if (condition.test(this)) { |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + Thread.sleep(pollInterval.toMillis()); |
| 64 | + } catch (InterruptedException e) { |
| 65 | + Thread.currentThread().interrupt(); |
| 66 | + throw new RuntimeException("Polling interrupted", e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + throw new TimeoutException("Condition not met within timeout of " + timeout); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Poll until execution completes and return the final result. |
| 75 | + * |
| 76 | + * @return test result with execution status and output |
| 77 | + * @throws TimeoutException if execution doesn't complete within timeout |
| 78 | + */ |
| 79 | + public TestResult<O> pollUntilComplete() throws TimeoutException { |
| 80 | + pollUntil(AsyncExecution::isComplete); |
| 81 | + return currentResult; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if execution has completed (succeeded or failed). |
| 86 | + */ |
| 87 | + public boolean isComplete() { |
| 88 | + if (currentHistory == null) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + return currentHistory.stream() |
| 92 | + .anyMatch(e -> { |
| 93 | + var eventType = e.eventTypeAsString(); |
| 94 | + return "ExecutionSucceeded".equals(eventType) || "ExecutionFailed".equals(eventType); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Check if an operation with the given name exists. |
| 100 | + */ |
| 101 | + public boolean hasOperation(String name) { |
| 102 | + if (currentResult == null) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + return currentResult.getOperations().stream() |
| 106 | + .anyMatch(op -> name.equals(op.getName())); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Check if a callback operation with the given name exists and is started. |
| 111 | + */ |
| 112 | + public boolean hasCallback(String name) { |
| 113 | + if (currentHistory == null) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + // Look for CallbackStarted event with this name |
| 117 | + return currentHistory.stream() |
| 118 | + .anyMatch(e -> name.equals(e.name()) && "CallbackStarted".equals(e.eventTypeAsString())); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get the callback ID for a callback operation. |
| 123 | + * |
| 124 | + * @param operationName name of the callback operation |
| 125 | + * @return callback ID |
| 126 | + * @throws IllegalStateException if no callback found for operation |
| 127 | + */ |
| 128 | + public String getCallbackId(String operationName) { |
| 129 | + if (currentResult == null) { |
| 130 | + throw new IllegalStateException("No history available - call pollUntil first"); |
| 131 | + } |
| 132 | + |
| 133 | + var operation = currentResult.getOperations().stream() |
| 134 | + .filter(op -> operationName.equals(op.getName())) |
| 135 | + .findFirst() |
| 136 | + .orElseThrow(() -> new IllegalStateException( |
| 137 | + "No operation found with name: " + operationName)); |
| 138 | + |
| 139 | + var callbackDetails = operation.getCallbackDetails(); |
| 140 | + if (callbackDetails == null || callbackDetails.callbackId() == null) { |
| 141 | + throw new IllegalStateException( |
| 142 | + "Operation '" + operationName + "' is not a callback or has no callback ID"); |
| 143 | + } |
| 144 | + |
| 145 | + return callbackDetails.callbackId(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get details for a specific operation. |
| 150 | + */ |
| 151 | + public TestOperation getOperation(String name) { |
| 152 | + if (currentResult == null) { |
| 153 | + throw new IllegalStateException("No history available - call pollUntil first"); |
| 154 | + } |
| 155 | + return currentResult.getOperation(name); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get all operations in the execution. |
| 160 | + */ |
| 161 | + public List<TestOperation> getOperations() { |
| 162 | + if (currentResult == null) { |
| 163 | + throw new IllegalStateException("No history available - call pollUntil first"); |
| 164 | + } |
| 165 | + return currentResult.getOperations(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get current execution status. |
| 170 | + */ |
| 171 | + public ExecutionStatus getStatus() { |
| 172 | + if (currentResult == null) { |
| 173 | + return ExecutionStatus.PENDING; |
| 174 | + } |
| 175 | + return currentResult.getStatus(); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Get the execution ARN. |
| 180 | + */ |
| 181 | + public String getExecutionArn() { |
| 182 | + return executionArn; |
| 183 | + } |
| 184 | + |
| 185 | + private void refreshHistory() { |
| 186 | + try { |
| 187 | + var request = GetDurableExecutionHistoryRequest.builder() |
| 188 | + .durableExecutionArn(executionArn) |
| 189 | + .includeExecutionData(true) |
| 190 | + .build(); |
| 191 | + var response = lambdaClient.getDurableExecutionHistory(request); |
| 192 | + this.currentHistory = response.events(); |
| 193 | + this.currentResult = processor.processEvents(currentHistory, outputType); |
| 194 | + } catch (ResourceNotFoundException e) { |
| 195 | + // Execution doesn't exist yet - this can happen immediately after async invoke |
| 196 | + // Leave currentHistory as null, pollUntil will retry |
| 197 | + this.currentHistory = null; |
| 198 | + this.currentResult = null; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments