Skip to content

Commit 650fd31

Browse files
anthonytingParidelPooya
authored andcommitted
refactor(testing-sdk): change operation storage to register operations on base class and use API interfaces (#89)
*Issue #, if available:* DAR-SDK-305 *Description of changes:* Refactoring operation storage to use API interfaces, and to register operations on the base class. This will allow for the cloud runner to call APIs such as SendCallbackSuccess, and to register operations before the cloud runner starts. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 5e8ae84 commit 650fd31

24 files changed

+1221
-599
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ describe("CloudDurableTestRunner", () => {
149149
expect(IndexedOperations).toHaveBeenCalledWith([]);
150150
expect(OperationStorage).toHaveBeenCalledWith(
151151
mockWaitManager,
152-
mockIndexedOperations
152+
mockIndexedOperations,
153+
expect.any(Object)
153154
);
154155
});
155156
});
@@ -396,6 +397,7 @@ describe("CloudDurableTestRunner", () => {
396397
expect(OperationWithData).toHaveBeenCalledWith(
397398
mockWaitManager,
398399
mockIndexedOperations,
400+
expect.any(Object),
399401
mockOperation
400402
);
401403
expect(result).toBe(mockOperationWithData);
@@ -416,6 +418,7 @@ describe("CloudDurableTestRunner", () => {
416418
expect(OperationWithData).toHaveBeenCalledWith(
417419
mockWaitManager,
418420
mockIndexedOperations,
421+
expect.any(Object),
419422
mockOperation
420423
);
421424
expect(result).toBe(mockOperationWithData);
@@ -439,6 +442,7 @@ describe("CloudDurableTestRunner", () => {
439442
expect(OperationWithData).toHaveBeenCalledWith(
440443
mockWaitManager,
441444
mockIndexedOperations,
445+
expect.any(Object),
442446
mockOperation
443447
);
444448
expect(result).toBe(mockOperationWithData);
@@ -459,6 +463,7 @@ describe("CloudDurableTestRunner", () => {
459463
expect(OperationWithData).toHaveBeenCalledWith(
460464
mockWaitManager,
461465
mockIndexedOperations,
466+
expect.any(Object),
462467
mockOperation
463468
);
464469
expect(result).toBe(mockOperationWithData);

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
import { OperationWaitManager } from "../local/operations/operation-wait-manager";
1818
import { ResultFormatter } from "../local/result-formatter";
1919
import { historyEventsToOperationEvents } from "./utils/process-history-events/process-history-events";
20+
import {
21+
createDurableApiClient,
22+
DurableApiClient,
23+
} from "../common/create-durable-api-client";
2024

2125
export interface CloudDurableTestRunnerParameters {
2226
functionName: string;
@@ -31,17 +35,21 @@ export class CloudDurableTestRunner<ResultType>
3135
private readonly formatter = new ResultFormatter<ResultType>();
3236
private readonly waitManager = new OperationWaitManager();
3337
private readonly indexedOperations = new IndexedOperations([]);
34-
private readonly operationStorage = new OperationStorage(
35-
this.waitManager,
36-
this.indexedOperations
37-
);
38+
private readonly operationStorage: OperationStorage;
39+
private readonly durableApiClient: DurableApiClient;
3840
private history: GetDurableExecutionHistoryResponse | undefined;
3941

4042
constructor({
4143
functionName: functionArn,
4244
config,
4345
}: CloudDurableTestRunnerParameters) {
4446
this.client = new LambdaClient(config ?? {});
47+
this.durableApiClient = createDurableApiClient(() => this.client);
48+
this.operationStorage = new OperationStorage(
49+
this.waitManager,
50+
this.indexedOperations,
51+
this.durableApiClient
52+
);
4553
this.functionArn = functionArn;
4654
}
4755

@@ -99,6 +107,7 @@ export class CloudDurableTestRunner<ResultType>
99107
return new OperationWithData(
100108
this.waitManager,
101109
this.indexedOperations,
110+
this.durableApiClient,
102111
this.indexedOperations.getByIndex(index)
103112
);
104113
}
@@ -109,13 +118,15 @@ export class CloudDurableTestRunner<ResultType>
109118
return new OperationWithData(
110119
this.waitManager,
111120
this.indexedOperations,
121+
this.durableApiClient,
112122
this.indexedOperations.getByNameAndIndex(name, index)
113123
);
114124
}
115125
getOperationById<T>(id: string): OperationWithData<T> {
116126
return new OperationWithData(
117127
this.waitManager,
118128
this.indexedOperations,
129+
this.durableApiClient,
119130
this.indexedOperations.getById(id)
120131
);
121132
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
LambdaClient,
3+
SendDurableExecutionCallbackFailureCommand,
4+
SendDurableExecutionCallbackHeartbeatCommand,
5+
SendDurableExecutionCallbackSuccessCommand,
6+
} from "@aws-sdk/client-lambda";
7+
import { createDurableApiClient } from "../create-durable-api-client";
8+
9+
describe("create-durable-api-client", () => {
10+
const client = {
11+
send: jest.fn(),
12+
} as unknown as LambdaClient;
13+
14+
beforeEach(() => {
15+
jest.clearAllMocks();
16+
});
17+
18+
it("should create client with expected properties", () => {
19+
expect(createDurableApiClient(() => client)).toEqual({
20+
sendCallbackSuccess: expect.any(Function),
21+
sendCallbackFailure: expect.any(Function),
22+
sendCallbackHeartbeat: expect.any(Function),
23+
});
24+
});
25+
26+
it("should call correct API for sendCallbackSuccess", async () => {
27+
const apiClient = createDurableApiClient(() => client);
28+
await apiClient.sendCallbackSuccess({
29+
CallbackId: "CallbackId",
30+
});
31+
32+
expect(client.send).toHaveBeenCalledWith(
33+
expect.any(SendDurableExecutionCallbackSuccessCommand)
34+
);
35+
expect(client.send).toHaveBeenCalledWith(
36+
expect.objectContaining({
37+
input: {
38+
CallbackId: "CallbackId",
39+
},
40+
})
41+
);
42+
});
43+
44+
it("should call correct API for sendCallbackFailure", async () => {
45+
const apiClient = createDurableApiClient(() => client);
46+
47+
await apiClient.sendCallbackFailure({
48+
CallbackId: "CallbackId",
49+
});
50+
51+
expect(client.send).toHaveBeenCalledWith(
52+
expect.any(SendDurableExecutionCallbackFailureCommand)
53+
);
54+
expect(client.send).toHaveBeenCalledWith(
55+
expect.objectContaining({
56+
input: {
57+
CallbackId: "CallbackId",
58+
},
59+
})
60+
);
61+
});
62+
63+
it("should call correct API for sendCallbackHeartbeat", async () => {
64+
const apiClient = createDurableApiClient(() => client);
65+
66+
await apiClient.sendCallbackHeartbeat({
67+
CallbackId: "CallbackId",
68+
});
69+
70+
expect(client.send).toHaveBeenCalledWith(
71+
expect.any(SendDurableExecutionCallbackHeartbeatCommand)
72+
);
73+
expect(client.send).toHaveBeenCalledWith(
74+
expect.objectContaining({
75+
input: {
76+
CallbackId: "CallbackId",
77+
},
78+
})
79+
);
80+
});
81+
});

0 commit comments

Comments
 (0)