Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: share an owned-connection Worker helper for Runtime lifecycle t…
…ests

Tests that assert Runtime teardown need each Worker to own its connection, so
the Runtime shuts down once the last Worker stops; a shared test environment
keeps it alive.

Move that helper into helpers.ts, choosing the server like
createTestWorkflowEnvironment() does (envconfig only when
TEMPORAL_TEST_ENV_CONFIG_SERVER is set, otherwise TEMPORAL_SERVICE_ADDRESS or
the default local address) instead of always reading a local envconfig profile.
Also give lifecycle tests unique task queues, since a namespace may be shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
2 people authored and chris-olszewski committed Nov 9, 2026
commit 87717046376f6b52b945764898a5017c235baad8
45 changes: 45 additions & 0 deletions packages/test/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import * as grpc from '@grpc/grpc-js';
import asyncRetry from 'async-retry';
import type { Connection } from '@temporalio/client';
import { Client } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
import type { NativeConnectionOptions, WorkerOptions } from '@temporalio/worker';
import { InternalNativeConnection } from '@temporalio/worker/lib/connection';
import * as iface from '@temporalio/proto';
import {
createBaseBundlerOptions,
isSet,
Worker,
loadHistory as loadHistoryBase,
saveHistory as saveHistoryBase,
RUN_TIME_SKIPPING_TESTS,
Expand Down Expand Up @@ -47,6 +52,46 @@ export const bundlerOptions = createBaseBundlerOptions([
require.resolve('./mock-native-worker'),
]);

/**
* Server connection settings for tests that must own their connections instead of using a shared test environment.
*
* Chosen like `createTestWorkflowEnvironment()`, but never starts a server: envconfig when
* `TEMPORAL_TEST_ENV_CONFIG_SERVER` is set (e.g. on Cloud), otherwise `TEMPORAL_SERVICE_ADDRESS` or the default
* local address, in the `default` namespace.
*/
export function ownedConnectionConfig(): { namespace: string; connectionOptions: NativeConnectionOptions } {
if (isSet(process.env.TEMPORAL_TEST_ENV_CONFIG_SERVER, false)) {
const { namespace, connectionOptions } = loadClientConnectConfig();
return { namespace: namespace ?? 'default', connectionOptions };
}
return { namespace: 'default', connectionOptions: { address: process.env.TEMPORAL_SERVICE_ADDRESS } };
}

/**
* Create a Worker on a connection that the Worker owns and closes when it shuts down.
*
* Once the last such Worker stops, the Runtime shuts down too; a shared test environment would keep it alive.
*/
export async function createWorkerWithOwnConnection(
options: Omit<WorkerOptions, 'connection' | 'namespace'> & Partial<Pick<WorkerOptions, 'namespace'>>
): ReturnType<typeof Worker.create> {
const { namespace, connectionOptions } = ownedConnectionConfig();
const connection = await InternalNativeConnection.connect(connectionOptions);
try {
return await Worker.create({ connection, namespace, ...options });
} catch (err) {
await connection.close();
throw err;
}
}

/**
* A task queue name unique to this test run, so tests sharing a server or namespace never poll each other's tasks.
*/
export function uniqueTaskQueue(title: string): string {
return `${title.replace(/[^a-zA-Z0-9]+/g, '-')}-${randomUUID()}`;
}

// Some of our tests expect "default custom search attributes" to exists, which used to be the case
// in all deployment with support for advanced visibility. However, this might no longer be true in
// some environement (e.g. Temporal CLI). Use the operator service to create them if they're missing.
Expand Down
35 changes: 15 additions & 20 deletions packages/test/src/test-worker-lifecycle-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@ import { randomUUID } from 'crypto';
import { setTimeout } from 'timers/promises';
import test from 'ava';
import { Client, Connection } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
import type { WorkerOptions } from '@temporalio/worker';
import { PromiseCompletionTimeoutError, Runtime, TransportError, UnexpectedError } from '@temporalio/worker';
import { InternalNativeConnection } from '@temporalio/worker/lib/connection';
import { isBun, RUN_INTEGRATION_TESTS, Worker } from './helpers';
import {
createWorkerWithOwnConnection,
isBun,
ownedConnectionConfig,
RUN_INTEGRATION_TESTS,
uniqueTaskQueue,
} from './helpers';
import { defaultOptions } from './mock-native-worker';
import { fillMemory } from './workflows';

const { namespace, connectionOptions } = loadClientConnectConfig();
const { namespace, connectionOptions } = ownedConnectionConfig();

/**
* Create a Worker on a configured connection that the Worker owns and closes when it shuts down.
*/
async function createWorker(options: Partial<WorkerOptions> & Pick<WorkerOptions, 'taskQueue'>) {
const connection = await InternalNativeConnection.connect(connectionOptions);
try {
return await Worker.create({ ...defaultOptions, connection, namespace, ...options });
} catch (err) {
await connection.close();
throw err;
}
function createWorker(options: Partial<WorkerOptions> & Pick<WorkerOptions, 'taskQueue'>) {
return createWorkerWithOwnConnection({ ...defaultOptions, ...options });
}

if (RUN_INTEGRATION_TESTS) {
test.serial('Worker shuts down gracefully', async (t) => {
const worker = await createWorker({ taskQueue: t.title.replace(/ /g, '_') });
const worker = await createWorker({ taskQueue: uniqueTaskQueue(t.title) });
t.is(worker.getState(), 'INITIALIZED');
t.not(Runtime._instance, undefined);
const workerRun = worker.run();
Expand All @@ -48,7 +43,7 @@ if (RUN_INTEGRATION_TESTS) {
});

test.serial("Worker.runUntil doesn't hang if provided promise survives to Worker's shutdown", async (t) => {
const worker = await createWorker({ taskQueue: t.title.replace(/ /g, '_') });
const worker = await createWorker({ taskQueue: uniqueTaskQueue(t.title) });
t.not(Runtime._instance, undefined);
const p = worker.runUntil(
new Promise(() => {
Expand All @@ -64,7 +59,7 @@ if (RUN_INTEGRATION_TESTS) {
});

test.serial('Worker shuts down gracefully if interrupted before running', async (t) => {
const worker = await createWorker({ taskQueue: t.title.replace(/ /g, '_') });
const worker = await createWorker({ taskQueue: uniqueTaskQueue(t.title) });
t.is(worker.getState(), 'INITIALIZED');
process.emit('SIGINT', 'SIGINT');
const workerRun = worker.run();
Expand All @@ -76,7 +71,7 @@ if (RUN_INTEGRATION_TESTS) {
test.serial('Worker fails validation against unknown namespace', async (t) => {
await t.throwsAsync(
createWorker({
taskQueue: t.title.replace(/ /g, '_'),
taskQueue: uniqueTaskQueue(t.title),
namespace: 'oogabooga',
}),
{
Expand All @@ -90,7 +85,7 @@ if (RUN_INTEGRATION_TESTS) {

(isBun ? test.skip : test.serial)('Threaded VM gracely stops and fails on ERR_WORKER_OUT_OF_MEMORY', async (t) => {
t.timeout(30_000);
const taskQueue = t.title.replace(/ /g, '_');
const taskQueue = uniqueTaskQueue(t.title);
const connection = await Connection.connect(connectionOptions);
const client = new Client({ connection, namespace });
const worker = await createWorker({ taskQueue });
Expand Down