Skip to content

Commit 47dca38

Browse files
committed
fix(testing-sdk): only use execution history in determining execution completion
1 parent bafbe2a commit 47dca38

File tree

7 files changed

+256
-215
lines changed

7 files changed

+256
-215
lines changed

packages/aws-durable-execution-sdk-js-testing/src/test-runner/cloud/history-poller.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import {
44
GetDurableExecutionHistoryCommandOutput,
55
GetDurableExecutionHistoryRequest,
66
GetDurableExecutionRequest,
7+
OperationStatus,
78
} from "@aws-sdk/client-lambda";
89
import { OperationEvents } from "../common/operations/operation-with-data";
910
import { historyEventsToOperationEvents } from "./utils/process-history-events/process-history-events";
1011
import { TestExecutionState } from "../common/test-execution-state";
11-
import { isClosedExecution } from "../common/utils";
12+
import { isClosedExecution } from "./utils/process-terminal-event/process-terminal-event";
13+
import {
14+
executionHistoryEventTypes,
15+
historyEventTypes,
16+
} from "./utils/process-history-events/history-event-types";
17+
import {
18+
getErrorFromEvent,
19+
getPayloadFromEvent,
20+
} from "./utils/process-history-events/event-data-extractors";
1221

1322
export type ReceivedOperationEventsCallback = (
1423
operationEvents: OperationEvents[],
@@ -28,9 +37,6 @@ export interface HistoryApiClient {
2837
getHistory: (
2938
request: GetDurableExecutionHistoryRequest,
3039
) => Promise<GetDurableExecutionHistoryCommandOutput>;
31-
getExecution: (
32-
request: GetDurableExecutionRequest,
33-
) => Promise<GetDurableExecutionCommandOutput>;
3440
}
3541

3642
export class HistoryPoller {
@@ -112,29 +118,33 @@ export class HistoryPoller {
112118

113119
this.lastHistoryMarker = previousHistoryMarker;
114120

115-
const executionResult = await this.callWithRetries(() =>
116-
this.apiClient.getExecution({
117-
DurableExecutionArn: this.durableExecutionArn,
118-
}),
119-
);
120-
121121
this.processEvents(pages.flat());
122122

123+
const lastEvent = pages.at(-1)?.at(-1);
123124
const eventsExceptLastPage = pages.slice(0, -1).flat();
124125
this.events.push(...eventsExceptLastPage);
125126

126-
if (!isClosedExecution(executionResult.Status)) {
127+
if (!isClosedExecution(lastEvent)) {
127128
// If the execution has not completed, do not add the last page
128129
// since we will be reading the same page again.
129130
return;
130131
}
131132

132133
const lastPage = pages.at(-1);
133134
this.events.push(...(lastPage ?? []));
135+
136+
const historyEventType = historyEventTypes[
137+
lastEvent.EventType
138+
] as (typeof executionHistoryEventTypes)[keyof typeof executionHistoryEventTypes];
139+
140+
if (historyEventType.operationStatus === OperationStatus.STARTED) {
141+
throw new Error("Completed execution cannot have STARTED status");
142+
}
143+
134144
this.testExecutionState.resolveWith({
135-
status: executionResult.Status,
136-
result: executionResult.Result,
137-
error: executionResult.Error,
145+
status: historyEventType.operationStatus,
146+
result: getPayloadFromEvent(lastEvent, historyEventType.detailPlace),
147+
error: getErrorFromEvent(lastEvent, historyEventType.detailPlace),
138148
});
139149

140150
this.stopPolling();

0 commit comments

Comments
 (0)