@temporalio/testing 1.11.8 → 1.12.0-rc.0
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/client.d.ts +39 -0
- package/lib/client.js +58 -0
- package/lib/client.js.map +1 -0
- package/lib/connection.js +1 -0
- package/lib/connection.js.map +1 -1
- package/lib/ephemeral-server.d.ts +109 -0
- package/lib/ephemeral-server.js +62 -0
- package/lib/ephemeral-server.js.map +1 -0
- package/lib/index.d.ts +4 -242
- package/lib/index.js +9 -300
- package/lib/index.js.map +1 -1
- package/lib/mocking-activity-environment.d.ts +35 -0
- package/lib/mocking-activity-environment.js +71 -0
- package/lib/mocking-activity-environment.js.map +1 -0
- package/lib/pkg.d.ts +5 -0
- package/lib/pkg.js +12 -0
- package/lib/pkg.js.map +1 -0
- package/lib/testing-workflow-environment.d.ts +205 -0
- package/lib/testing-workflow-environment.js +278 -0
- package/lib/testing-workflow-environment.js.map +1 -0
- package/package.json +12 -9
- package/src/client.ts +81 -0
- package/src/ephemeral-server.ts +185 -0
- package/src/index.ts +27 -439
- package/src/mocking-activity-environment.ts +90 -0
- package/src/pkg.ts +7 -0
- package/src/testing-workflow-environment.ts +337 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import 'abort-controller/polyfill'; // eslint-disable-line import/no-unassigned-import
|
|
2
|
+
import { Duration, SearchAttributeType } from '@temporalio/common';
|
|
3
|
+
import { msToNumber } from '@temporalio/common/lib/time';
|
|
4
|
+
import { native } from '@temporalio/core-bridge';
|
|
5
|
+
import { SearchAttributeKey } from '@temporalio/common/lib/search-attributes';
|
|
6
|
+
import pkg from './pkg';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for the Temporal CLI Dev Server.
|
|
10
|
+
*/
|
|
11
|
+
export interface DevServerConfig {
|
|
12
|
+
type: 'dev-server';
|
|
13
|
+
|
|
14
|
+
executable?: EphemeralServerExecutable;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sqlite DB filename if persisting or non-persistent if none (default).
|
|
18
|
+
*/
|
|
19
|
+
dbFilename?: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Namespace to use - created at startup.
|
|
23
|
+
*
|
|
24
|
+
* @default "default"
|
|
25
|
+
*/
|
|
26
|
+
namespace?: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* IP to bind to.
|
|
30
|
+
*
|
|
31
|
+
* @default localhost
|
|
32
|
+
*/
|
|
33
|
+
ip?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Port to listen on; defaults to find a random free port.
|
|
37
|
+
*/
|
|
38
|
+
port?: number;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether to enable the UI.
|
|
42
|
+
*
|
|
43
|
+
* @default true if `uiPort` is set; defaults to `false` otherwise.
|
|
44
|
+
*/
|
|
45
|
+
ui?: boolean;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Port to listen on for the UI; if `ui` is true, defaults to `port + 1000`.
|
|
49
|
+
*/
|
|
50
|
+
uiPort?: number;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Log format and level
|
|
54
|
+
* @default { format: "pretty", level" "warn" }
|
|
55
|
+
*/
|
|
56
|
+
log?: { format: string; level: string };
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Extra args to pass to the executable command.
|
|
60
|
+
*
|
|
61
|
+
* Note that the Dev Server implementation may be changed to another one in the future. Therefore, there is no
|
|
62
|
+
* guarantee that Dev Server options, and particularly those provided through the `extraArgs` array, will continue to
|
|
63
|
+
* be supported in the future.
|
|
64
|
+
*/
|
|
65
|
+
extraArgs?: string[];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Search attributes to be registered with the dev server.
|
|
69
|
+
*/
|
|
70
|
+
searchAttributes?: SearchAttributeKey<SearchAttributeType>[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Configuration for the time-skipping test server.
|
|
75
|
+
*/
|
|
76
|
+
export interface TimeSkippingServerConfig {
|
|
77
|
+
type: 'time-skipping';
|
|
78
|
+
|
|
79
|
+
executable?: EphemeralServerExecutable;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Optional port to listen on, defaults to find a random free port.
|
|
83
|
+
*/
|
|
84
|
+
port?: number;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Extra args to pass to the executable command.
|
|
88
|
+
*
|
|
89
|
+
* Note that the Test Server implementation may be changed to another one in the future. Therefore, there is
|
|
90
|
+
* no guarantee that server options, and particularly those provided through the `extraArgs` array, will continue to
|
|
91
|
+
* be supported in the future.
|
|
92
|
+
*/
|
|
93
|
+
extraArgs?: string[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Which version of the executable to run.
|
|
98
|
+
*/
|
|
99
|
+
export type EphemeralServerExecutable =
|
|
100
|
+
| {
|
|
101
|
+
type: 'cached-download';
|
|
102
|
+
/**
|
|
103
|
+
* Download destination directory or the system's temp directory if none set.
|
|
104
|
+
*/
|
|
105
|
+
downloadDir?: string;
|
|
106
|
+
/**
|
|
107
|
+
* Optional version, can be set to a specific server release or "default" or "latest".
|
|
108
|
+
*
|
|
109
|
+
* At the time of writing the the server is released as part of the
|
|
110
|
+
* Java SDK - (https://github.com/temporalio/sdk-java/releases).
|
|
111
|
+
*
|
|
112
|
+
* @default "default" - get the best version for the current SDK version.
|
|
113
|
+
*/
|
|
114
|
+
version?: string;
|
|
115
|
+
|
|
116
|
+
/** How long to cache the download for. Default to 1 day. */
|
|
117
|
+
ttl?: Duration;
|
|
118
|
+
}
|
|
119
|
+
| {
|
|
120
|
+
type: 'existing-path';
|
|
121
|
+
/** Path to executable */
|
|
122
|
+
path: string;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
export function toNativeEphemeralServerConfig(
|
|
129
|
+
server: DevServerConfig | TimeSkippingServerConfig
|
|
130
|
+
): native.EphemeralServerConfig {
|
|
131
|
+
switch (server.type) {
|
|
132
|
+
case 'dev-server':
|
|
133
|
+
return {
|
|
134
|
+
type: 'dev-server',
|
|
135
|
+
exe: toNativeEphemeralServerExecutableConfig(server.executable),
|
|
136
|
+
ip: server.ip ?? '127.0.0.1',
|
|
137
|
+
port: server.port ?? null,
|
|
138
|
+
ui: server.ui ?? false,
|
|
139
|
+
uiPort: server.uiPort ?? null,
|
|
140
|
+
namespace: server.namespace ?? 'default',
|
|
141
|
+
dbFilename: server.dbFilename ?? null,
|
|
142
|
+
log: server.log ?? { format: 'pretty', level: 'warn' },
|
|
143
|
+
extraArgs: server.extraArgs ?? [],
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
case 'time-skipping':
|
|
147
|
+
return {
|
|
148
|
+
type: 'time-skipping',
|
|
149
|
+
exe: toNativeEphemeralServerExecutableConfig(server.executable),
|
|
150
|
+
port: server.port ?? null,
|
|
151
|
+
extraArgs: server.extraArgs ?? [],
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
default:
|
|
155
|
+
throw new TypeError(`Unsupported server type: ${String((server as any).type)}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @internal
|
|
161
|
+
*/
|
|
162
|
+
function toNativeEphemeralServerExecutableConfig(
|
|
163
|
+
executable: EphemeralServerExecutable = { type: 'cached-download' }
|
|
164
|
+
): native.EphemeralServerExecutableConfig {
|
|
165
|
+
switch (executable.type) {
|
|
166
|
+
case 'cached-download':
|
|
167
|
+
return {
|
|
168
|
+
type: 'cached-download',
|
|
169
|
+
downloadDir: executable.downloadDir ?? null,
|
|
170
|
+
version: executable.version ?? 'default',
|
|
171
|
+
ttl: msToNumber(executable.ttl ?? '1d'),
|
|
172
|
+
sdkName: 'sdk-typescript',
|
|
173
|
+
sdkVersion: pkg.version,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
case 'existing-path':
|
|
177
|
+
return {
|
|
178
|
+
type: 'existing-path',
|
|
179
|
+
path: executable.path,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
default:
|
|
183
|
+
throw new TypeError(`Unsupported server executable type: ${String((executable as any).type)}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,109 +8,35 @@
|
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import 'abort-controller/polyfill'; // eslint-disable-line import/no-unassigned-import
|
|
12
11
|
import path from 'node:path';
|
|
13
|
-
import events from 'node:events';
|
|
14
|
-
import * as activity from '@temporalio/activity';
|
|
15
|
-
import {
|
|
16
|
-
AsyncCompletionClient,
|
|
17
|
-
Client,
|
|
18
|
-
ClientOptions,
|
|
19
|
-
WorkflowClient,
|
|
20
|
-
WorkflowClientOptions,
|
|
21
|
-
WorkflowResultOptions,
|
|
22
|
-
} from '@temporalio/client';
|
|
23
|
-
import {
|
|
24
|
-
ActivityFunction,
|
|
25
|
-
Duration,
|
|
26
|
-
SdkComponent,
|
|
27
|
-
Logger,
|
|
28
|
-
defaultFailureConverter,
|
|
29
|
-
defaultPayloadConverter,
|
|
30
|
-
} from '@temporalio/common';
|
|
31
|
-
import { msToNumber, msToTs, tsToMs } from '@temporalio/common/lib/time';
|
|
32
|
-
import { ActivityInterceptorsFactory, DefaultLogger, NativeConnection, Runtime } from '@temporalio/worker';
|
|
33
|
-
import { withMetadata } from '@temporalio/worker/lib/logger';
|
|
34
|
-
import { Activity } from '@temporalio/worker/lib/activity';
|
|
35
|
-
import {
|
|
36
|
-
EphemeralServer,
|
|
37
|
-
EphemeralServerConfig,
|
|
38
|
-
getEphemeralServerTarget,
|
|
39
|
-
DevServerConfig,
|
|
40
|
-
TimeSkippingServerConfig,
|
|
41
|
-
} from '@temporalio/core-bridge';
|
|
42
|
-
import { filterNullAndUndefined } from '@temporalio/common/lib/internal-non-workflow';
|
|
43
|
-
import { Connection, TestService } from './connection';
|
|
44
12
|
|
|
45
|
-
export {
|
|
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
|
-
* Gets the result of a Workflow execution.
|
|
75
|
-
*
|
|
76
|
-
* @see {@link WorkflowClient.result}
|
|
77
|
-
*/
|
|
78
|
-
override async result<T>(
|
|
79
|
-
workflowId: string,
|
|
80
|
-
runId?: string | undefined,
|
|
81
|
-
opts?: WorkflowResultOptions | undefined
|
|
82
|
-
): Promise<T> {
|
|
83
|
-
if (this.enableTimeSkipping) {
|
|
84
|
-
await this.testService.unlockTimeSkipping({});
|
|
85
|
-
try {
|
|
86
|
-
return await super.result(workflowId, runId, opts);
|
|
87
|
-
} finally {
|
|
88
|
-
await this.testService.lockTimeSkipping({});
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
return await super.result(workflowId, runId, opts);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* A client with the exact same API as the "normal" client with one exception:
|
|
98
|
-
* when `TestEnvClient.workflow` (an instance of {@link TimeSkippingWorkflowClient}) waits on a Workflow's result, it will enable time skipping
|
|
99
|
-
* in the Test Server.
|
|
100
|
-
*/
|
|
101
|
-
class TestEnvClient extends Client {
|
|
102
|
-
constructor(options: TestEnvClientOptions) {
|
|
103
|
-
super(options);
|
|
104
|
-
|
|
105
|
-
// Recreate the client (this isn't optimal but it's better than adding public methods just for testing).
|
|
106
|
-
// NOTE: we cast to "any" to work around `workflow` being a readonly attribute.
|
|
107
|
-
(this as any).workflow = new TimeSkippingWorkflowClient({
|
|
108
|
-
...this.workflow.options,
|
|
109
|
-
connection: options.connection,
|
|
110
|
-
enableTimeSkipping: options.enableTimeSkipping,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
|
13
|
+
export {
|
|
14
|
+
TestWorkflowEnvironment,
|
|
15
|
+
type LocalTestWorkflowEnvironmentOptions,
|
|
16
|
+
type TimeSkippingTestWorkflowEnvironmentOptions,
|
|
17
|
+
type ExistingServerTestWorkflowEnvironmentOptions,
|
|
18
|
+
} from './testing-workflow-environment';
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
type DevServerConfig,
|
|
22
|
+
type TimeSkippingServerConfig,
|
|
23
|
+
type EphemeralServerExecutable,
|
|
24
|
+
} from './ephemeral-server';
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
// FIXME: Revise the pertinence of these types
|
|
28
|
+
type ClientOptionsForTestEnv,
|
|
29
|
+
type TestEnvClientOptions,
|
|
30
|
+
type TimeSkippingWorkflowClientOptions,
|
|
31
|
+
TestEnvClient,
|
|
32
|
+
TimeSkippingWorkflowClient,
|
|
33
|
+
} from './client';
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
type MockActivityEnvironmentOptions,
|
|
37
|
+
MockActivityEnvironment,
|
|
38
|
+
defaultActivityInfo,
|
|
39
|
+
} from './mocking-activity-environment';
|
|
114
40
|
|
|
115
41
|
/**
|
|
116
42
|
* Convenience workflow interceptors
|
|
@@ -119,341 +45,3 @@ class TestEnvClient extends Client {
|
|
|
119
45
|
* retryable `ApplicationFailure`s.
|
|
120
46
|
*/
|
|
121
47
|
export const workflowInterceptorModules = [path.join(__dirname, 'assert-to-failure-interceptor')];
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Subset of the "normal" client options that are used to create a client for the test environment.
|
|
125
|
-
*/
|
|
126
|
-
export type ClientOptionsForTestEnv = Omit<ClientOptions, 'namespace' | 'connection'>;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Options for {@link TestWorkflowEnvironment.create}
|
|
130
|
-
*/
|
|
131
|
-
export type TestWorkflowEnvironmentOptions = {
|
|
132
|
-
server: EphemeralServerConfig;
|
|
133
|
-
client?: ClientOptionsForTestEnv;
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Options for {@link TestWorkflowEnvironment.createTimeSkipping}
|
|
138
|
-
*/
|
|
139
|
-
export type TimeSkippingTestWorkflowEnvironmentOptions = {
|
|
140
|
-
server?: Omit<TimeSkippingServerConfig, 'type'>;
|
|
141
|
-
client?: ClientOptionsForTestEnv;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Options for {@link TestWorkflowEnvironment.createLocal}
|
|
146
|
-
*/
|
|
147
|
-
export type LocalTestWorkflowEnvironmentOptions = {
|
|
148
|
-
server?: Omit<DevServerConfig, 'type'>;
|
|
149
|
-
client?: ClientOptionsForTestEnv;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
export type TestWorkflowEnvironmentOptionsWithDefaults = Required<TestWorkflowEnvironmentOptions>;
|
|
153
|
-
|
|
154
|
-
function addDefaults(opts: TestWorkflowEnvironmentOptions): TestWorkflowEnvironmentOptionsWithDefaults {
|
|
155
|
-
return {
|
|
156
|
-
client: {},
|
|
157
|
-
...opts,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* An execution environment for running Workflow integration tests.
|
|
163
|
-
*
|
|
164
|
-
* Runs an external server.
|
|
165
|
-
* By default, the Java test server is used which supports time skipping.
|
|
166
|
-
*/
|
|
167
|
-
export class TestWorkflowEnvironment {
|
|
168
|
-
/**
|
|
169
|
-
* Namespace used in this environment (taken from {@link TestWorkflowEnvironmentOptions})
|
|
170
|
-
*/
|
|
171
|
-
public readonly namespace?: string;
|
|
172
|
-
/**
|
|
173
|
-
* Get an established {@link Connection} to the ephemeral server
|
|
174
|
-
*/
|
|
175
|
-
public readonly connection: Connection;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* A {@link TestEnvClient} for interacting with the ephemeral server
|
|
179
|
-
*/
|
|
180
|
-
public readonly client: Client;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* An {@link AsyncCompletionClient} for interacting with the test server
|
|
184
|
-
*
|
|
185
|
-
* @deprecated - use `client.activity` instead
|
|
186
|
-
*/
|
|
187
|
-
public readonly asyncCompletionClient: AsyncCompletionClient;
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* A {@link TimeSkippingWorkflowClient} for interacting with the test server
|
|
191
|
-
*
|
|
192
|
-
* @deprecated - use `client.workflow` instead
|
|
193
|
-
*/
|
|
194
|
-
public readonly workflowClient: WorkflowClient;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* A {@link NativeConnection} for interacting with the test server.
|
|
198
|
-
*
|
|
199
|
-
* Use this connection when creating Workers for testing.
|
|
200
|
-
*/
|
|
201
|
-
public readonly nativeConnection: NativeConnection;
|
|
202
|
-
|
|
203
|
-
protected constructor(
|
|
204
|
-
public readonly options: TestWorkflowEnvironmentOptionsWithDefaults,
|
|
205
|
-
public readonly supportsTimeSkipping: boolean,
|
|
206
|
-
protected readonly server: EphemeralServer,
|
|
207
|
-
connection: Connection,
|
|
208
|
-
nativeConnection: NativeConnection,
|
|
209
|
-
namespace: string | undefined
|
|
210
|
-
) {
|
|
211
|
-
this.connection = connection;
|
|
212
|
-
this.nativeConnection = nativeConnection;
|
|
213
|
-
this.namespace = namespace;
|
|
214
|
-
this.client = new TestEnvClient({
|
|
215
|
-
connection,
|
|
216
|
-
namespace: this.namespace,
|
|
217
|
-
enableTimeSkipping: supportsTimeSkipping,
|
|
218
|
-
...options.client,
|
|
219
|
-
});
|
|
220
|
-
// eslint-disable-next-line deprecation/deprecation
|
|
221
|
-
this.asyncCompletionClient = this.client.activity;
|
|
222
|
-
// eslint-disable-next-line deprecation/deprecation
|
|
223
|
-
this.workflowClient = this.client.workflow;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Start a time skipping workflow environment.
|
|
228
|
-
*
|
|
229
|
-
* This environment automatically skips to the next events in time when a workflow handle's `result` is awaited on
|
|
230
|
-
* (which includes {@link WorkflowClient.execute}). Before the result is awaited on, time can be manually skipped
|
|
231
|
-
* forward using {@link sleep}. The currently known time can be obtained via {@link currentTimeMs}.
|
|
232
|
-
*
|
|
233
|
-
* This environment will be powered by the Temporal Time Skipping Test Server (part of the [Java SDK](https://github.com/temporalio/sdk-java)).
|
|
234
|
-
* Note that the Time Skipping Test Server does not support full capabilities of the regular Temporal Server, and may
|
|
235
|
-
* occasionally present different behaviors. For general Workflow testing, it is generally preferable to use {@link createLocal}
|
|
236
|
-
* instead.
|
|
237
|
-
*
|
|
238
|
-
* Users can reuse this environment for testing multiple independent workflows, but not concurrently. Time skipping,
|
|
239
|
-
* which is automatically done when awaiting a workflow result and manually done on sleep, is global to the
|
|
240
|
-
* environment, not to the workflow under test. We highly recommend running tests serially when using a single
|
|
241
|
-
* environment or creating a separate environment per test.
|
|
242
|
-
*
|
|
243
|
-
* By default, the latest release of the Test Serveer will be downloaded and cached to a temporary directory
|
|
244
|
-
* (e.g. `$TMPDIR/temporal-test-server-sdk-typescript-*` or `%TEMP%/temporal-test-server-sdk-typescript-*.exe`). Note
|
|
245
|
-
* that existing cached binairies will be reused without validation that they are still up-to-date, until the SDK
|
|
246
|
-
* itself is updated. Alternatively, a specific version number of the Test Server may be provided, or the path to an
|
|
247
|
-
* existing Test Server binary may be supplied; see {@link LocalTestWorkflowEnvironmentOptions.server.executable}.
|
|
248
|
-
*
|
|
249
|
-
* Note that the Test Server implementation may be changed to another one in the future. Therefore, there is no
|
|
250
|
-
* guarantee that Test Server options, and particularly those provided through the `extraArgs` array, will continue to
|
|
251
|
-
* be supported in the future.
|
|
252
|
-
*
|
|
253
|
-
* IMPORTANT: At this time, the Time Skipping Test Server is not supported on ARM platforms. Execution on Apple
|
|
254
|
-
* silicon Macs will work if Rosetta 2 is installed.
|
|
255
|
-
*/
|
|
256
|
-
static async createTimeSkipping(opts?: TimeSkippingTestWorkflowEnvironmentOptions): Promise<TestWorkflowEnvironment> {
|
|
257
|
-
return await this.create({
|
|
258
|
-
server: { type: 'time-skipping', ...opts?.server },
|
|
259
|
-
client: opts?.client,
|
|
260
|
-
supportsTimeSkipping: true,
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Start a full Temporal server locally.
|
|
266
|
-
*
|
|
267
|
-
* This environment is good for testing full server capabilities, but does not support time skipping like
|
|
268
|
-
* {@link createTimeSkipping} does. {@link supportsTimeSkipping} will always return `false` for this environment.
|
|
269
|
-
* {@link sleep} will sleep the actual amount of time and {@link currentTimeMs} will return the current time.
|
|
270
|
-
*
|
|
271
|
-
* This local environment will be powered by [Temporal CLI](https://github.com/temporalio/cli), which is a
|
|
272
|
-
* self-contained executable for Temporal. By default, Temporal's database will not be persisted to disk, and no UI
|
|
273
|
-
* will be launched.
|
|
274
|
-
*
|
|
275
|
-
* By default, the latest release of the CLI will be downloaded and cached to a temporary directory
|
|
276
|
-
* (e.g. `$TMPDIR/temporal-sdk-typescript-*` or `%TEMP%/temporal-sdk-typescript-*.exe`). Note that existing cached
|
|
277
|
-
* binairies will be reused without validation that they are still up-to-date, until the SDK itself is updated.
|
|
278
|
-
* Alternatively, a specific version number of the CLI may be provided, or the path to an existing CLI binary may be
|
|
279
|
-
* supplied; see {@link LocalTestWorkflowEnvironmentOptions.server.executable}.
|
|
280
|
-
*
|
|
281
|
-
* Note that the Dev Server implementation may be changed to another one in the future. Therefore, there is no
|
|
282
|
-
* guarantee that Dev Server options, and particularly those provided through the `extraArgs` array, will continue to
|
|
283
|
-
* be supported in the future.
|
|
284
|
-
*/
|
|
285
|
-
static async createLocal(opts?: LocalTestWorkflowEnvironmentOptions): Promise<TestWorkflowEnvironment> {
|
|
286
|
-
return await this.create({
|
|
287
|
-
server: { type: 'dev-server', ...opts?.server },
|
|
288
|
-
client: opts?.client,
|
|
289
|
-
namespace: opts?.server?.namespace,
|
|
290
|
-
supportsTimeSkipping: false,
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Create a new test environment
|
|
296
|
-
*/
|
|
297
|
-
private static async create(
|
|
298
|
-
opts: TestWorkflowEnvironmentOptions & {
|
|
299
|
-
supportsTimeSkipping: boolean;
|
|
300
|
-
namespace?: string;
|
|
301
|
-
}
|
|
302
|
-
): Promise<TestWorkflowEnvironment> {
|
|
303
|
-
const { supportsTimeSkipping, namespace, ...rest } = opts;
|
|
304
|
-
const optsWithDefaults = addDefaults(filterNullAndUndefined(rest));
|
|
305
|
-
const server = await Runtime.instance().createEphemeralServer(optsWithDefaults.server);
|
|
306
|
-
const address = getEphemeralServerTarget(server);
|
|
307
|
-
|
|
308
|
-
const nativeConnection = await NativeConnection.connect({ address });
|
|
309
|
-
const connection = await Connection.connect({ address });
|
|
310
|
-
|
|
311
|
-
return new this(optsWithDefaults, supportsTimeSkipping, server, connection, nativeConnection, namespace);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Kill the test server process and close the connection to it
|
|
316
|
-
*/
|
|
317
|
-
async teardown(): Promise<void> {
|
|
318
|
-
await this.connection.close();
|
|
319
|
-
await this.nativeConnection.close();
|
|
320
|
-
await Runtime.instance().shutdownEphemeralServer(this.server);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Wait for `durationMs` in "server time".
|
|
325
|
-
*
|
|
326
|
-
* This awaits using regular setTimeout in regular environments, or manually skips time in time-skipping environments.
|
|
327
|
-
*
|
|
328
|
-
* Useful for simulating events far into the future like completion of long running activities.
|
|
329
|
-
*
|
|
330
|
-
* **Time skippping**:
|
|
331
|
-
*
|
|
332
|
-
* The time skippping server toggles between skipped time and normal time depending on what it needs to execute.
|
|
333
|
-
*
|
|
334
|
-
* This method is _likely_ to resolve in less than `durationMs` of "real time".
|
|
335
|
-
*
|
|
336
|
-
* @param durationMs number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
*
|
|
340
|
-
* `workflow.ts`
|
|
341
|
-
*
|
|
342
|
-
* ```ts
|
|
343
|
-
* const activities = proxyActivities({ startToCloseTimeout: 2_000_000 });
|
|
344
|
-
*
|
|
345
|
-
* export async function raceActivityAndTimer(): Promise<string> {
|
|
346
|
-
* return await Promise.race([
|
|
347
|
-
* wf.sleep(500_000).then(() => 'timer'),
|
|
348
|
-
* activities.longRunning().then(() => 'activity'),
|
|
349
|
-
* ]);
|
|
350
|
-
* }
|
|
351
|
-
* ```
|
|
352
|
-
*
|
|
353
|
-
* `test.ts`
|
|
354
|
-
*
|
|
355
|
-
* ```ts
|
|
356
|
-
* const worker = await Worker.create({
|
|
357
|
-
* connection: testEnv.nativeConnection,
|
|
358
|
-
* activities: {
|
|
359
|
-
* async longRunning() {
|
|
360
|
-
* await testEnv.sleep(1_000_000); // <-- sleep called here
|
|
361
|
-
* },
|
|
362
|
-
* },
|
|
363
|
-
* // ...
|
|
364
|
-
* });
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
367
|
-
sleep = async (durationMs: Duration): Promise<void> => {
|
|
368
|
-
if (this.supportsTimeSkipping) {
|
|
369
|
-
await (this.connection as Connection).testService.unlockTimeSkippingWithSleep({ duration: msToTs(durationMs) });
|
|
370
|
-
} else {
|
|
371
|
-
await new Promise((resolve) => setTimeout(resolve, msToNumber(durationMs)));
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Get the current time known to this environment.
|
|
377
|
-
*
|
|
378
|
-
* For non-time-skipping environments this is simply the system time. For time-skipping environments this is whatever
|
|
379
|
-
* time has been skipped to.
|
|
380
|
-
*/
|
|
381
|
-
async currentTimeMs(): Promise<number> {
|
|
382
|
-
if (this.supportsTimeSkipping) {
|
|
383
|
-
const { time } = await (this.connection as Connection).testService.getCurrentTime({});
|
|
384
|
-
return tsToMs(time);
|
|
385
|
-
} else {
|
|
386
|
-
return Date.now();
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
export interface MockActivityEnvironmentOptions {
|
|
392
|
-
interceptors?: ActivityInterceptorsFactory[];
|
|
393
|
-
logger?: Logger;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Used as the default activity info for Activities executed in the {@link MockActivityEnvironment}
|
|
398
|
-
*/
|
|
399
|
-
export const defaultActivityInfo: activity.Info = {
|
|
400
|
-
attempt: 1,
|
|
401
|
-
taskQueue: 'test',
|
|
402
|
-
isLocal: false,
|
|
403
|
-
taskToken: Buffer.from('test'),
|
|
404
|
-
activityId: 'test',
|
|
405
|
-
activityType: 'unknown',
|
|
406
|
-
workflowType: 'test',
|
|
407
|
-
base64TaskToken: Buffer.from('test').toString('base64'),
|
|
408
|
-
heartbeatTimeoutMs: undefined,
|
|
409
|
-
heartbeatDetails: undefined,
|
|
410
|
-
activityNamespace: 'default',
|
|
411
|
-
workflowNamespace: 'default',
|
|
412
|
-
workflowExecution: { workflowId: 'test', runId: 'dead-beef' },
|
|
413
|
-
scheduledTimestampMs: 1,
|
|
414
|
-
startToCloseTimeoutMs: 1000,
|
|
415
|
-
scheduleToCloseTimeoutMs: 1000,
|
|
416
|
-
currentAttemptScheduledTimestampMs: 1,
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* An execution environment for testing Activities.
|
|
421
|
-
*
|
|
422
|
-
* Mocks Activity {@link Context | activity.Context} and exposes hooks for cancellation and heartbeats.
|
|
423
|
-
*
|
|
424
|
-
* Note that the `Context` object used by this environment will be reused for all activities that are run in this
|
|
425
|
-
* environment. Consequently, once `cancel()` is called, any further activity that gets executed in this environment
|
|
426
|
-
* will immediately be in a cancelled state.
|
|
427
|
-
*/
|
|
428
|
-
export class MockActivityEnvironment extends events.EventEmitter {
|
|
429
|
-
public cancel: (reason?: any) => void = () => undefined;
|
|
430
|
-
public readonly context: activity.Context;
|
|
431
|
-
private readonly activity: Activity;
|
|
432
|
-
|
|
433
|
-
constructor(info?: Partial<activity.Info>, opts?: MockActivityEnvironmentOptions) {
|
|
434
|
-
super();
|
|
435
|
-
const heartbeatCallback = (details?: unknown) => this.emit('heartbeat', details);
|
|
436
|
-
const loadedDataConverter = {
|
|
437
|
-
payloadConverter: defaultPayloadConverter,
|
|
438
|
-
payloadCodecs: [],
|
|
439
|
-
failureConverter: defaultFailureConverter,
|
|
440
|
-
};
|
|
441
|
-
this.activity = new Activity(
|
|
442
|
-
{ ...defaultActivityInfo, ...info },
|
|
443
|
-
undefined,
|
|
444
|
-
loadedDataConverter,
|
|
445
|
-
heartbeatCallback,
|
|
446
|
-
withMetadata(opts?.logger ?? new DefaultLogger(), { sdkComponent: SdkComponent.worker }),
|
|
447
|
-
opts?.interceptors ?? []
|
|
448
|
-
);
|
|
449
|
-
this.context = this.activity.context;
|
|
450
|
-
this.cancel = this.activity.cancel;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* Run a function in Activity Context
|
|
455
|
-
*/
|
|
456
|
-
public async run<P extends any[], R, F extends ActivityFunction<P, R>>(fn: F, ...args: P): Promise<R> {
|
|
457
|
-
return this.activity.runNoEncoding(fn as ActivityFunction<any, any>, { args, headers: {} }) as Promise<R>;
|
|
458
|
-
}
|
|
459
|
-
}
|