-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
236 lines (195 loc) · 8.26 KB
/
index.ts
File metadata and controls
236 lines (195 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// This sample demonstrates the in-memory testing framework for unit-testing
// orchestrations WITHOUT any external dependencies (no Docker, no emulator, no network).
//
// Features demonstrated:
// 1. InMemoryOrchestrationBackend — full in-memory orchestration engine
// 2. TestOrchestrationClient — same API as TaskHubGrpcClient
// 3. TestOrchestrationWorker — same API as TaskHubGrpcWorker
// 4. ReplaySafeLogger — suppress duplicate logs during orchestration replay
// 5. Testing patterns: sequence, fan-out/fan-in, timers, events, terminate
import {
InMemoryOrchestrationBackend,
TestOrchestrationClient,
TestOrchestrationWorker,
OrchestrationContext,
ActivityContext,
TOrchestrator,
OrchestrationStatus,
whenAll,
ConsoleLogger,
NoOpLogger,
} from "@microsoft/durabletask-js";
// ---------------------------------------------------------------------------
// Application code (same code you'd use with the real TaskHubGrpcWorker)
// ---------------------------------------------------------------------------
const addNumbers = async (_ctx: ActivityContext, nums: number[]): Promise<number> => {
return nums.reduce((a, b) => a + b, 0);
};
const greet = async (_ctx: ActivityContext, name: string): Promise<string> => {
return `Hello, ${name}!`;
};
/** Sequence: calls greet for each city. */
const sequenceOrchestrator: TOrchestrator = async function* (ctx: OrchestrationContext): any {
// Demonstrate ReplaySafeLogger — only logs when NOT replaying
const safeLogger = ctx.createReplaySafeLogger(new ConsoleLogger());
safeLogger.info("Orchestration started (only printed once, not during replay)");
const cities = ["Tokyo", "London", "Paris"];
const results: string[] = [];
for (const city of cities) {
const greeting: string = yield ctx.callActivity(greet, city);
results.push(greeting);
}
safeLogger.info("Orchestration finishing");
return results;
};
/** Fan-out/fan-in: parallel sum. */
const parallelSumOrchestrator: TOrchestrator = async function* (ctx: OrchestrationContext): any {
const batches = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const tasks = batches.map((batch) => ctx.callActivity(addNumbers, batch));
const partialSums: number[] = yield whenAll(tasks);
return partialSums.reduce((a, b) => a + b, 0); // total = 45
};
/** Timer: waits for a timer then returns. */
const timerOrchestrator: TOrchestrator = async function* (ctx: OrchestrationContext): any {
yield ctx.createTimer(1); // 1 second timer
return "Timer fired";
};
/** External event: waits for an event, returns its data. */
const eventOrchestrator: TOrchestrator = async function* (ctx: OrchestrationContext): any {
const data: string = yield ctx.waitForExternalEvent<string>("myEvent");
return `Received: ${data}`;
};
// ---------------------------------------------------------------------------
// Test runner (lightweight, no framework needed)
// ---------------------------------------------------------------------------
interface TestResult {
name: string;
passed: boolean;
error?: string;
}
async function runTest(
name: string,
fn: (backend: InMemoryOrchestrationBackend, client: TestOrchestrationClient, worker: TestOrchestrationWorker) => Promise<void>,
): Promise<TestResult> {
const backend = new InMemoryOrchestrationBackend();
const client = new TestOrchestrationClient(backend);
const worker = new TestOrchestrationWorker(backend);
try {
await fn(backend, client, worker);
await worker.stop();
await client.stop();
backend.reset();
return { name, passed: true };
} catch (error: any) {
await worker.stop();
await client.stop();
backend.reset();
return { name, passed: false, error: error.message };
}
}
function assert(condition: boolean, message: string): void {
if (!condition) {
throw new Error(`Assertion failed: ${message}`);
}
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
(async () => {
console.log("=== Unit Testing with In-Memory Backend ===\n");
const results: TestResult[] = [];
// Test 1: Activity Sequence
results.push(
await runTest("Activity Sequence", async (_backend, client, worker) => {
worker.addOrchestrator(sequenceOrchestrator);
worker.addActivity(greet);
await worker.start();
const id = await client.scheduleNewOrchestration(sequenceOrchestrator);
const state = await client.waitForOrchestrationCompletion(id, true, 10);
assert(state !== undefined, "State should be defined");
assert(state!.runtimeStatus === OrchestrationStatus.COMPLETED, "Should be completed");
const output = JSON.parse(state!.serializedOutput!);
assert(output.length === 3, "Should have 3 greetings");
assert(output[0] === "Hello, Tokyo!", "First greeting should be Tokyo");
}),
);
// Test 2: Fan-out/Fan-in
results.push(
await runTest("Fan-out/Fan-in", async (_backend, client, worker) => {
worker.addOrchestrator(parallelSumOrchestrator);
worker.addActivity(addNumbers);
await worker.start();
const id = await client.scheduleNewOrchestration(parallelSumOrchestrator);
const state = await client.waitForOrchestrationCompletion(id, true, 10);
assert(state!.runtimeStatus === OrchestrationStatus.COMPLETED, "Should be completed");
assert(state!.serializedOutput === "45", `Sum should be 45, got ${state!.serializedOutput}`);
}),
);
// Test 3: Timer
results.push(
await runTest("Timer", async (_backend, client, worker) => {
worker.addOrchestrator(timerOrchestrator);
await worker.start();
const id = await client.scheduleNewOrchestration(timerOrchestrator);
const state = await client.waitForOrchestrationCompletion(id, true, 10);
assert(state!.runtimeStatus === OrchestrationStatus.COMPLETED, "Should be completed");
assert(state!.serializedOutput === '"Timer fired"', "Should return timer result");
}),
);
// Test 4: External Event
results.push(
await runTest("External Event", async (_backend, client, worker) => {
worker.addOrchestrator(eventOrchestrator);
await worker.start();
const id = await client.scheduleNewOrchestration(eventOrchestrator);
await client.waitForOrchestrationStart(id);
// Raise the event
await client.raiseOrchestrationEvent(id, "myEvent", "test-data");
const state = await client.waitForOrchestrationCompletion(id, true, 10);
assert(state!.runtimeStatus === OrchestrationStatus.COMPLETED, "Should be completed");
assert(state!.serializedOutput === '"Received: test-data"', `Got: ${state!.serializedOutput}`);
}),
);
// Test 5: Terminate
results.push(
await runTest("Terminate", async (_backend, client, worker) => {
worker.addOrchestrator(eventOrchestrator);
await worker.start();
const id = await client.scheduleNewOrchestration(eventOrchestrator);
await client.waitForOrchestrationStart(id);
await client.terminateOrchestration(id, "Cancelled");
const state = await client.waitForOrchestrationCompletion(id, true, 10);
assert(state!.runtimeStatus === OrchestrationStatus.TERMINATED, "Should be terminated");
}),
);
// Test 6: NoOpLogger (verify it doesn't throw)
results.push(
await runTest("NoOpLogger", async () => {
const logger = new NoOpLogger();
logger.info("This should do nothing");
logger.error("This too");
logger.warn("And this");
logger.debug("Silent");
// If we get here without throwing, the test passes
}),
);
// --- Report ---
console.log("\n--- Test Results ---");
let allPassed = true;
for (const result of results) {
const status = result.passed ? "PASS" : "FAIL";
console.log(` [${status}] ${result.name}${result.error ? ` — ${result.error}` : ""}`);
if (!result.passed) allPassed = false;
}
const passCount = results.filter((r) => r.passed).length;
console.log(`\n${passCount}/${results.length} tests passed.`);
if (allPassed) {
console.log("\n=== All unit-testing demos completed successfully! ===");
process.exit(0);
} else {
console.error("\nSome tests failed!");
process.exit(1);
}
})();