@temporalio/testing 1.0.0-rc.0 → 1.0.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.
@@ -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
+ }