@temporalio/testing 1.0.0-rc.0 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/child-process.d.ts +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/package.json +12 -11
- package/scripts/compile-proto.mjs +10 -17
- package/src/assert-to-failure-interceptor.ts +38 -0
- package/src/child-process.ts +60 -0
- package/src/index-for-docs.ts +2 -0
- package/src/index.ts +348 -0
- package/src/test-service-client.ts +47 -0
- package/src/utils.ts +52 -0
package/lib/child-process.d.ts
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import * as activity from '@temporalio/activity';
|
|
3
4
|
import { AsyncCompletionClient, WorkflowClient as BaseWorkflowClient, WorkflowClientOptions as BaseWorkflowClientOptions, WorkflowResultOptions as BaseWorkflowResultOptions, WorkflowStartOptions as BaseWorkflowStartOptions } from '@temporalio/client';
|
|
4
5
|
import { ActivityFunction, Workflow, WorkflowResultType } from '@temporalio/common';
|
package/lib/index.js
CHANGED
|
@@ -202,6 +202,7 @@ exports.TestWorkflowEnvironment = TestWorkflowEnvironment;
|
|
|
202
202
|
*/
|
|
203
203
|
exports.defaultActivityInfo = {
|
|
204
204
|
attempt: 1,
|
|
205
|
+
taskQueue: 'test',
|
|
205
206
|
isLocal: false,
|
|
206
207
|
taskToken: Buffer.from('test'),
|
|
207
208
|
activityId: 'test',
|
|
@@ -235,6 +236,9 @@ class MockActivityEnvironment extends events_1.default.EventEmitter {
|
|
|
235
236
|
});
|
|
236
237
|
const heartbeatCallback = (details) => this.emit('heartbeat', details);
|
|
237
238
|
this.context = new activity.Context({ ...exports.defaultActivityInfo, ...info }, promise, abortController.signal, heartbeatCallback);
|
|
239
|
+
promise.catch(() => {
|
|
240
|
+
/* avoid unhandled rejection */
|
|
241
|
+
});
|
|
238
242
|
}
|
|
239
243
|
/**
|
|
240
244
|
* Run a function in Activity Context
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAAiD;AACjD,+CAM4B;AAC5B,+CAA8G;AAC9G,+CAA6E;AAC7E,gDAAwB;AACxB,4CAAoB;AACpB,uDAAmD;AACnD,iDAAkE;AAClE,oDAA4B;AAC5B,mDAAoD;AAEpD,+DAAgE;AAEhE,MAAM,2BAA2B,GAAG,YAAE,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,aAAa,CAAC;AAErF,QAAA,wBAAwB,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,2BAA2B,EAAE,CAAC,CAAC;AAgClG;;;;GAIG;AACH,MAAa,cAAe,SAAQ,uBAAkB;IAGpD,YAAY,OAA8B;QACxC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,OAAO,CAClB,kBAA8B,EAC9B,OAAgC;QAEhC,OAAO,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACM,KAAK,CAAC,MAAM,CACnB,UAAkB,EAClB,KAA0B,EAC1B,IAAwC;QAExC,IAAI,IAAI,EAAE,eAAe,EAAE;YACzB,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;SACpD;QACD,MAAM,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAC9C,IAAI;YACF,OAAO,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CAAC,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACnE,CAAC;CACF;AA/BD,0DA+BC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporalio/testing",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
4
|
"description": "Temporal.io SDK Testing sub-package",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -17,17 +17,17 @@
|
|
|
17
17
|
"author": "Temporal Technologies Inc. <sdk@temporal.io>",
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@grpc/grpc-js": "^1.
|
|
21
|
-
"@temporalio/activity": "^1.0.0-rc.
|
|
22
|
-
"@temporalio/client": "^1.0.0-rc.
|
|
23
|
-
"@temporalio/common": "^1.0.0-rc.
|
|
24
|
-
"@temporalio/worker": "^1.0.0-rc.
|
|
25
|
-
"@types/long": "^4.0.
|
|
20
|
+
"@grpc/grpc-js": "^1.6.7",
|
|
21
|
+
"@temporalio/activity": "^1.0.0-rc.1",
|
|
22
|
+
"@temporalio/client": "^1.0.0-rc.1",
|
|
23
|
+
"@temporalio/common": "^1.0.0-rc.1",
|
|
24
|
+
"@temporalio/worker": "^1.0.0-rc.1",
|
|
25
|
+
"@types/long": "^4.0.2",
|
|
26
26
|
"abort-controller": "^3.0.0",
|
|
27
27
|
"get-port": "^6.1.2",
|
|
28
|
-
"got": "^12.0
|
|
29
|
-
"long": "^
|
|
30
|
-
"protobufjs": "
|
|
28
|
+
"got": "^12.1.0",
|
|
29
|
+
"long": "^5.2.0",
|
|
30
|
+
"protobufjs": "6.11.2",
|
|
31
31
|
"tar-stream": "^2.2.0",
|
|
32
32
|
"unzipper": "^0.10.11"
|
|
33
33
|
},
|
|
@@ -37,11 +37,12 @@
|
|
|
37
37
|
"homepage": "https://github.com/temporalio/sdk-typescript/tree/main/packages/testing",
|
|
38
38
|
"files": [
|
|
39
39
|
"lib",
|
|
40
|
+
"src",
|
|
40
41
|
"generated-protos",
|
|
41
42
|
"scripts"
|
|
42
43
|
],
|
|
43
44
|
"publishConfig": {
|
|
44
45
|
"access": "public"
|
|
45
46
|
},
|
|
46
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "723de0fbc7a04e68084ec99453578e7027eb3803"
|
|
47
48
|
}
|
|
@@ -58,24 +58,17 @@ async function compileProtos(protoPath, jsOutputFile, dtsOutputFile, ...args) {
|
|
|
58
58
|
);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
mkdirSync(outputDir, { recursive: true });
|
|
61
|
+
mkdirSync(outputDir, { recursive: true });
|
|
63
62
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
const protoFiles = glob.sync(resolve(protoBaseDir, '**/*.proto'));
|
|
64
|
+
const protosMTime = Math.max(...protoFiles.map(mtime));
|
|
65
|
+
const genMTime = mtime(outputFile);
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
await compileProtos(serviceProtoPath, outputFile, resolve(outputDir, 'index.d.ts'), '--path', resolve(protoBaseDir));
|
|
74
|
-
|
|
75
|
-
console.log('Done');
|
|
67
|
+
if (protosMTime < genMTime) {
|
|
68
|
+
console.log('Assuming protos are up to date');
|
|
69
|
+
process.exit(0);
|
|
76
70
|
}
|
|
77
71
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
72
|
+
await compileProtos(serviceProtoPath, outputFile, resolve(outputDir, 'index.d.ts'), '--path', resolve(protoBaseDir));
|
|
73
|
+
|
|
74
|
+
console.log('Done');
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
import { ApplicationFailure, WorkflowInterceptors } from '@temporalio/workflow';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Simple interceptor that transforms {@link assert.AssertionError} into non retryable failures.
|
|
6
|
+
*
|
|
7
|
+
* This allows conveniently using `assert` directly from Workflows.
|
|
8
|
+
*/
|
|
9
|
+
export function interceptors(): WorkflowInterceptors {
|
|
10
|
+
return {
|
|
11
|
+
inbound: [
|
|
12
|
+
{
|
|
13
|
+
async handleSignal(input, next) {
|
|
14
|
+
try {
|
|
15
|
+
return await next(input);
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (err instanceof assert.AssertionError) {
|
|
18
|
+
const appErr = ApplicationFailure.nonRetryable(err.message);
|
|
19
|
+
appErr.stack = err.stack;
|
|
20
|
+
throw appErr;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async execute(input, next) {
|
|
25
|
+
try {
|
|
26
|
+
return await next(input);
|
|
27
|
+
} catch (err) {
|
|
28
|
+
if (err instanceof assert.AssertionError) {
|
|
29
|
+
const appErr = ApplicationFailure.nonRetryable(err.message);
|
|
30
|
+
appErr.stack = err.stack;
|
|
31
|
+
throw appErr;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// TODO: this code is duplicated in the scripts directory, consider moving to external dependency
|
|
2
|
+
import { ChildProcess } from 'child_process';
|
|
3
|
+
|
|
4
|
+
export class ChildProcessError extends Error {
|
|
5
|
+
public readonly name = 'ChildProcessError';
|
|
6
|
+
|
|
7
|
+
constructor(message: string, public readonly code: number | null, public readonly signal: NodeJS.Signals | null) {
|
|
8
|
+
super(message);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface WaitOptions {
|
|
13
|
+
validReturnCodes: number[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function waitOnChild(child: ChildProcess, opts?: WaitOptions): Promise<void> {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
child.on('exit', (code, signal) => {
|
|
19
|
+
if (code !== null && (opts?.validReturnCodes ?? [0]).includes(code)) {
|
|
20
|
+
resolve();
|
|
21
|
+
} else {
|
|
22
|
+
reject(new ChildProcessError('Process failed', code, signal));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
child.on('error', reject);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function kill(child: ChildProcess, signal: NodeJS.Signals = 'SIGINT', opts?: WaitOptions): Promise<void> {
|
|
30
|
+
if (child.pid === undefined) {
|
|
31
|
+
throw new TypeError('Expected child with pid');
|
|
32
|
+
}
|
|
33
|
+
process.kill(child.pid, signal);
|
|
34
|
+
try {
|
|
35
|
+
await waitOnChild(child, opts);
|
|
36
|
+
} catch (err) {
|
|
37
|
+
// Should error if the error is not a child process error or it is a child
|
|
38
|
+
// process and either the platform is Windows or the signal matches.
|
|
39
|
+
const shouldError = !(err instanceof ChildProcessError) || (process.platform !== 'win32' && err.signal !== signal);
|
|
40
|
+
if (shouldError) {
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function killIfExists(
|
|
47
|
+
child: ChildProcess,
|
|
48
|
+
signal: NodeJS.Signals = 'SIGINT',
|
|
49
|
+
opts?: WaitOptions
|
|
50
|
+
): Promise<void> {
|
|
51
|
+
try {
|
|
52
|
+
await kill(child, signal, opts);
|
|
53
|
+
} catch (err: any) {
|
|
54
|
+
if (err.code !== 'ESRCH') {
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const shell = process.platform === 'win32';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import * as activity from '@temporalio/activity';
|
|
2
|
+
import {
|
|
3
|
+
AsyncCompletionClient,
|
|
4
|
+
WorkflowClient as BaseWorkflowClient,
|
|
5
|
+
WorkflowClientOptions as BaseWorkflowClientOptions,
|
|
6
|
+
WorkflowResultOptions as BaseWorkflowResultOptions,
|
|
7
|
+
WorkflowStartOptions as BaseWorkflowStartOptions,
|
|
8
|
+
} from '@temporalio/client';
|
|
9
|
+
import { ActivityFunction, CancelledFailure, msToTs, Workflow, WorkflowResultType } from '@temporalio/common';
|
|
10
|
+
import { NativeConnection, Logger, DefaultLogger } from '@temporalio/worker';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import os from 'os';
|
|
13
|
+
import { AbortController } from 'abort-controller';
|
|
14
|
+
import { ChildProcess, spawn, StdioOptions } from 'child_process';
|
|
15
|
+
import events from 'events';
|
|
16
|
+
import { kill, waitOnChild } from './child-process';
|
|
17
|
+
import type getPortType from 'get-port';
|
|
18
|
+
import { Connection, TestService } from './test-service-client';
|
|
19
|
+
|
|
20
|
+
const TEST_SERVER_EXECUTABLE_NAME = os.platform() === 'win32' ? 'test-server.exe' : 'test-server';
|
|
21
|
+
|
|
22
|
+
export const DEFAULT_TEST_SERVER_PATH = path.join(__dirname, `../${TEST_SERVER_EXECUTABLE_NAME}`);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Options passed to {@link WorkflowClient.result}, these are the same as the
|
|
26
|
+
* {@link BaseWorkflowResultOptions} with an additional option that controls
|
|
27
|
+
* whether to toggle time skipping in the Test server while waiting on a
|
|
28
|
+
* Workflow's result.
|
|
29
|
+
*/
|
|
30
|
+
export interface WorkflowResultOptions extends BaseWorkflowResultOptions {
|
|
31
|
+
/**
|
|
32
|
+
* If set to `true`, waiting for the result does not enable time skipping
|
|
33
|
+
*/
|
|
34
|
+
runInNormalTime?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Options passed to {@link WorkflowClient.execute}, these are the same as the
|
|
39
|
+
* {@link BaseWorkflowStartOptions} with an additional option that controls
|
|
40
|
+
* whether to toggle time skipping in the Test server while waiting on a
|
|
41
|
+
* Workflow's result.
|
|
42
|
+
*/
|
|
43
|
+
export type WorkflowStartOptions<T extends Workflow> = BaseWorkflowStartOptions<T> & {
|
|
44
|
+
/**
|
|
45
|
+
* If set to `true`, waiting for the result does not enable time skipping
|
|
46
|
+
*/
|
|
47
|
+
runInNormalTime?: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export interface WorkflowClientOptions extends BaseWorkflowClientOptions {
|
|
51
|
+
connection: Connection;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A client with the exact same API as the "normal" client with 1 exception,
|
|
56
|
+
* When this client waits on a Workflow's result, it will enable time skipping
|
|
57
|
+
* in the test server.
|
|
58
|
+
*/
|
|
59
|
+
export class WorkflowClient extends BaseWorkflowClient {
|
|
60
|
+
protected readonly testService: TestService;
|
|
61
|
+
|
|
62
|
+
constructor(options: WorkflowClientOptions) {
|
|
63
|
+
super(options);
|
|
64
|
+
this.testService = options.connection.testService;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Execute a Workflow and wait for completion.
|
|
69
|
+
*
|
|
70
|
+
* @see {@link BaseWorkflowClient.execute}
|
|
71
|
+
*/
|
|
72
|
+
public async execute<T extends Workflow>(
|
|
73
|
+
workflowTypeOrFunc: string | T,
|
|
74
|
+
options: WorkflowStartOptions<T>
|
|
75
|
+
): Promise<WorkflowResultType<T>> {
|
|
76
|
+
return super.execute(workflowTypeOrFunc, options);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Gets the result of a Workflow execution.
|
|
81
|
+
*
|
|
82
|
+
* @see {@link BaseWorkflowClient.result}
|
|
83
|
+
*/
|
|
84
|
+
override async result<T>(
|
|
85
|
+
workflowId: string,
|
|
86
|
+
runId?: string | undefined,
|
|
87
|
+
opts?: WorkflowResultOptions | undefined
|
|
88
|
+
): Promise<T> {
|
|
89
|
+
if (opts?.runInNormalTime) {
|
|
90
|
+
return await super.result(workflowId, runId, opts);
|
|
91
|
+
}
|
|
92
|
+
await this.testService.unlockTimeSkipping({});
|
|
93
|
+
try {
|
|
94
|
+
return await super.result(workflowId, runId, opts);
|
|
95
|
+
} finally {
|
|
96
|
+
await this.testService.lockTimeSkipping({});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Convenience workflow interceptors
|
|
103
|
+
*
|
|
104
|
+
* Contains a single interceptor for transforming `AssertionError`s into non
|
|
105
|
+
* retryable `ApplicationFailure`s.
|
|
106
|
+
*/
|
|
107
|
+
export const workflowInterceptorModules = [path.join(__dirname, 'assert-to-failure-interceptor')];
|
|
108
|
+
|
|
109
|
+
export interface TestServerSpawnerOptions {
|
|
110
|
+
/**
|
|
111
|
+
* @default {@link DEFAULT_TEST_SERVER_PATH}
|
|
112
|
+
*/
|
|
113
|
+
path?: string;
|
|
114
|
+
/**
|
|
115
|
+
* @default ignore
|
|
116
|
+
*/
|
|
117
|
+
stdio?: StdioOptions;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A generic callback that returns a child process
|
|
122
|
+
*/
|
|
123
|
+
export type TestServerSpawner = (port: number) => ChildProcess;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Options for {@link TestWorkflowEnvironment.create}
|
|
127
|
+
*/
|
|
128
|
+
export interface TestWorkflowEnvironmentOptions {
|
|
129
|
+
testServer?: TestServerSpawner | TestServerSpawnerOptions;
|
|
130
|
+
logger?: Logger;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
interface TestWorkflowEnvironmentOptionsWithDefaults {
|
|
134
|
+
testServerSpawner: TestServerSpawner;
|
|
135
|
+
logger: Logger;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function addDefaults({
|
|
139
|
+
testServer,
|
|
140
|
+
logger,
|
|
141
|
+
}: TestWorkflowEnvironmentOptions): TestWorkflowEnvironmentOptionsWithDefaults {
|
|
142
|
+
return {
|
|
143
|
+
testServerSpawner:
|
|
144
|
+
typeof testServer === 'function'
|
|
145
|
+
? testServer
|
|
146
|
+
: (port: number) =>
|
|
147
|
+
spawn(testServer?.path || DEFAULT_TEST_SERVER_PATH, [`${port}`], {
|
|
148
|
+
stdio: testServer?.stdio || 'ignore',
|
|
149
|
+
}),
|
|
150
|
+
logger: logger ?? new DefaultLogger('INFO'),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// TS transforms `import` statements into `require`s, this is a workaround until
|
|
155
|
+
// tsconfig module nodenext is stable.
|
|
156
|
+
const _importDynamic = new Function('modulePath', 'return import(modulePath)');
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* An execution environment for running Workflow integration tests.
|
|
160
|
+
*
|
|
161
|
+
* Runs an external server.
|
|
162
|
+
* By default, the Java test server is used which supports time skipping.
|
|
163
|
+
*/
|
|
164
|
+
export class TestWorkflowEnvironment {
|
|
165
|
+
/**
|
|
166
|
+
* Get an extablished {@link Connection} to the test server
|
|
167
|
+
*/
|
|
168
|
+
public readonly connection: Connection;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* An {@link AsyncCompletionClient} for interacting with the test server
|
|
172
|
+
*/
|
|
173
|
+
public readonly asyncCompletionClient: AsyncCompletionClient;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A {@link WorkflowClient} for interacting with the test server
|
|
177
|
+
*/
|
|
178
|
+
public readonly workflowClient: WorkflowClient;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A {@link NativeConnection} for interacting with the test server.
|
|
182
|
+
*
|
|
183
|
+
* Use this connection when creating Workers for testing.
|
|
184
|
+
*/
|
|
185
|
+
public readonly nativeConnection: NativeConnection;
|
|
186
|
+
|
|
187
|
+
protected constructor(
|
|
188
|
+
protected readonly serverProc: ChildProcess,
|
|
189
|
+
connection: Connection,
|
|
190
|
+
nativeConnection: NativeConnection
|
|
191
|
+
) {
|
|
192
|
+
this.connection = connection;
|
|
193
|
+
this.nativeConnection = nativeConnection;
|
|
194
|
+
this.workflowClient = new WorkflowClient({ connection });
|
|
195
|
+
this.asyncCompletionClient = new AsyncCompletionClient({ connection });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Create a new test environment
|
|
200
|
+
*/
|
|
201
|
+
static async create(opts?: TestWorkflowEnvironmentOptions): Promise<TestWorkflowEnvironment> {
|
|
202
|
+
// No, we're not going to compile this to ESM for one dependency
|
|
203
|
+
const getPort = (await _importDynamic('get-port')).default as typeof getPortType;
|
|
204
|
+
const port = await getPort();
|
|
205
|
+
|
|
206
|
+
const { testServerSpawner, logger } = addDefaults(opts ?? {});
|
|
207
|
+
|
|
208
|
+
const child = testServerSpawner(port);
|
|
209
|
+
|
|
210
|
+
const address = `127.0.0.1:${port}`;
|
|
211
|
+
const connPromise = Connection.connect({ address });
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
await Promise.race([
|
|
215
|
+
connPromise,
|
|
216
|
+
waitOnChild(child).then(() => {
|
|
217
|
+
throw new Error('Test server child process exited prematurely');
|
|
218
|
+
}),
|
|
219
|
+
]);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
try {
|
|
222
|
+
await kill(child);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
logger.error('Failed to kill test server child process', { error });
|
|
225
|
+
}
|
|
226
|
+
throw err;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const conn = await connPromise;
|
|
230
|
+
const nativeConnection = await NativeConnection.connect({ address });
|
|
231
|
+
|
|
232
|
+
return new this(child, conn, nativeConnection);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Kill the test server process and close the connection to it
|
|
237
|
+
*/
|
|
238
|
+
async teardown(): Promise<void> {
|
|
239
|
+
await this.connection.close();
|
|
240
|
+
await this.nativeConnection.close();
|
|
241
|
+
// TODO: the server should return exit code 0
|
|
242
|
+
await kill(this.serverProc, 'SIGINT', { validReturnCodes: [0, 130] });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Wait for `durationMs` in "test server time".
|
|
247
|
+
*
|
|
248
|
+
* The test server toggles between skipped time and normal time depending on what it needs to execute.
|
|
249
|
+
*
|
|
250
|
+
* This method is likely to resolve in less than `durationMs` of "real time".
|
|
251
|
+
*
|
|
252
|
+
* Useful for simulating events far into the future like completion of long running activities.
|
|
253
|
+
*
|
|
254
|
+
* @param durationMs {@link https://www.npmjs.com/package/ms | ms} formatted string or number of milliseconds
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
*
|
|
258
|
+
* `workflow.ts`
|
|
259
|
+
*
|
|
260
|
+
* ```ts
|
|
261
|
+
* const activities = proxyActivities({ startToCloseTimeout: 2_000_000 });
|
|
262
|
+
*
|
|
263
|
+
* export async function raceActivityAndTimer(): Promise<string> {
|
|
264
|
+
* return await Promise.race([
|
|
265
|
+
* wf.sleep(500_000).then(() => 'timer'),
|
|
266
|
+
* activities.longRunning().then(() => 'activity'),
|
|
267
|
+
* ]);
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* `test.ts`
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* const worker = await Worker.create({
|
|
275
|
+
* connection: testEnv.nativeConnection,
|
|
276
|
+
* activities: {
|
|
277
|
+
* async longRunning() {
|
|
278
|
+
* await testEnv.sleep(1_000_000); // <-- sleep called here
|
|
279
|
+
* },
|
|
280
|
+
* },
|
|
281
|
+
* // ...
|
|
282
|
+
* });
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
sleep = async (durationMs: number | string): Promise<void> => {
|
|
286
|
+
await this.connection.testService.unlockTimeSkippingWithSleep({ duration: msToTs(durationMs) });
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Used as the default activity info for Activities executed in the {@link MockActivityEnvironment}
|
|
292
|
+
*/
|
|
293
|
+
export const defaultActivityInfo: activity.Info = {
|
|
294
|
+
attempt: 1,
|
|
295
|
+
taskQueue: 'test',
|
|
296
|
+
isLocal: false,
|
|
297
|
+
taskToken: Buffer.from('test'),
|
|
298
|
+
activityId: 'test',
|
|
299
|
+
activityType: 'unknown',
|
|
300
|
+
workflowType: 'test',
|
|
301
|
+
base64TaskToken: Buffer.from('test').toString('base64'),
|
|
302
|
+
heartbeatDetails: undefined,
|
|
303
|
+
activityNamespace: 'default',
|
|
304
|
+
workflowNamespace: 'default',
|
|
305
|
+
workflowExecution: { workflowId: 'test', runId: 'dead-beef' },
|
|
306
|
+
scheduledTimestampMs: 1,
|
|
307
|
+
startToCloseTimeoutMs: 1000,
|
|
308
|
+
scheduleToCloseTimeoutMs: 1000,
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* An execution environment for testing Activities.
|
|
313
|
+
*
|
|
314
|
+
* Mocks Activity {@link Context | activity.Context} and exposes hooks for
|
|
315
|
+
* cancellation and heartbeats.
|
|
316
|
+
*/
|
|
317
|
+
export class MockActivityEnvironment extends events.EventEmitter {
|
|
318
|
+
public cancel: (reason?: any) => void = () => undefined;
|
|
319
|
+
public readonly context: activity.Context;
|
|
320
|
+
|
|
321
|
+
constructor(info?: Partial<activity.Info>) {
|
|
322
|
+
super();
|
|
323
|
+
const abortController = new AbortController();
|
|
324
|
+
const promise = new Promise<never>((_, reject) => {
|
|
325
|
+
this.cancel = (reason?: any) => {
|
|
326
|
+
abortController.abort();
|
|
327
|
+
reject(new CancelledFailure(reason));
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
const heartbeatCallback = (details?: unknown) => this.emit('heartbeat', details);
|
|
331
|
+
this.context = new activity.Context(
|
|
332
|
+
{ ...defaultActivityInfo, ...info },
|
|
333
|
+
promise,
|
|
334
|
+
abortController.signal,
|
|
335
|
+
heartbeatCallback
|
|
336
|
+
);
|
|
337
|
+
promise.catch(() => {
|
|
338
|
+
/* avoid unhandled rejection */
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Run a function in Activity Context
|
|
344
|
+
*/
|
|
345
|
+
public run<P extends any[], R, F extends ActivityFunction<P, R>>(fn: F, ...args: P): Promise<R> {
|
|
346
|
+
return activity.asyncLocalStorage.run(this.context, fn, ...args);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as grpc from '@grpc/grpc-js';
|
|
2
|
+
import { Connection as BaseConnection, ConnectionOptions } from '@temporalio/client';
|
|
3
|
+
import { ConnectionCtorOptions as BaseConnectionCtorOptions } from '@temporalio/client/lib/connection';
|
|
4
|
+
import { temporal } from '../generated-protos';
|
|
5
|
+
|
|
6
|
+
export type TestService = temporal.api.testservice.v1.TestService;
|
|
7
|
+
export const { TestService } = temporal.api.testservice.v1;
|
|
8
|
+
|
|
9
|
+
interface ConnectionCtorOptions extends BaseConnectionCtorOptions {
|
|
10
|
+
testService: TestService;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A Connection class that can be used to interact with both the test server's TestService and WorkflowService
|
|
15
|
+
*/
|
|
16
|
+
export class Connection extends BaseConnection {
|
|
17
|
+
public static readonly TestServiceClient = grpc.makeGenericClientConstructor({}, 'TestService', {});
|
|
18
|
+
public readonly testService: TestService;
|
|
19
|
+
|
|
20
|
+
protected static createCtorOptions(options?: ConnectionOptions): ConnectionCtorOptions {
|
|
21
|
+
const ctorOptions = BaseConnection.createCtorOptions(options);
|
|
22
|
+
const rpcImpl = this.generateRPCImplementation({
|
|
23
|
+
serviceName: 'temporal.api.testservice.v1.TestService',
|
|
24
|
+
client: ctorOptions.client,
|
|
25
|
+
callContextStorage: ctorOptions.callContextStorage,
|
|
26
|
+
interceptors: ctorOptions.options.interceptors,
|
|
27
|
+
});
|
|
28
|
+
const testService = TestService.create(rpcImpl, false, false);
|
|
29
|
+
return { ...ctorOptions, testService };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static lazy(options?: ConnectionOptions): Connection {
|
|
33
|
+
const ctorOptions = this.createCtorOptions(options);
|
|
34
|
+
return new this(ctorOptions);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static async connect(options?: ConnectionOptions): Promise<Connection> {
|
|
38
|
+
const ret = this.lazy(options);
|
|
39
|
+
await ret.ensureConnected();
|
|
40
|
+
return ret;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected constructor(options: ConnectionCtorOptions) {
|
|
44
|
+
super(options);
|
|
45
|
+
this.testService = options.testService;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Connection } from '@temporalio/client';
|
|
2
|
+
import { msToTs } from '@temporalio/internal-workflow-common';
|
|
3
|
+
|
|
4
|
+
export async function waitOnNamespace(
|
|
5
|
+
connection: Connection,
|
|
6
|
+
namespace: string,
|
|
7
|
+
maxAttempts = 100,
|
|
8
|
+
retryIntervalSecs = 1
|
|
9
|
+
): Promise<void> {
|
|
10
|
+
const runId = '12345678-dead-beef-1234-1234567890ab';
|
|
11
|
+
for (let attempt = 1; attempt <= maxAttempts; ++attempt) {
|
|
12
|
+
try {
|
|
13
|
+
await connection.workflowService.getWorkflowExecutionHistory({
|
|
14
|
+
namespace,
|
|
15
|
+
execution: { workflowId: 'fake', runId },
|
|
16
|
+
});
|
|
17
|
+
} catch (err: any) {
|
|
18
|
+
if (err.details.includes('workflow history not found') || err.details.includes(runId)) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
if (attempt === maxAttempts) {
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
await new Promise((resolve) => setTimeout(resolve, retryIntervalSecs * 1000));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function createNamespace(
|
|
30
|
+
connection: Connection,
|
|
31
|
+
namespace: string,
|
|
32
|
+
maxAttempts = 100,
|
|
33
|
+
retryIntervalSecs = 1
|
|
34
|
+
): Promise<void> {
|
|
35
|
+
for (let attempt = 1; attempt <= maxAttempts; ++attempt) {
|
|
36
|
+
try {
|
|
37
|
+
await connection.workflowService.registerNamespace({
|
|
38
|
+
namespace,
|
|
39
|
+
workflowExecutionRetentionPeriod: msToTs('1 day'),
|
|
40
|
+
});
|
|
41
|
+
break;
|
|
42
|
+
} catch (err: any) {
|
|
43
|
+
if (err.details === 'Namespace already exists.') {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (attempt === maxAttempts) {
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, retryIntervalSecs * 1000));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|