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
self review
  • Loading branch information
chris-olszewski committed Nov 9, 2026
commit 0beda946719e2df328e491decc94cd41b790a12d
40 changes: 8 additions & 32 deletions packages/test/src/test-worker-lifecycle-integration.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/**
* Worker lifecycle integration tests.
*
* Connections are explicitly closed after their Workers stop.
* Run against local servers or Cloud using explicitly supplied connections. Worker shutdown leaves
* these connections open; closing the last connection then shuts down the Runtime.
* Automatic closure of Worker-created connections is covered by test-worker-owned-connection.local.ts.
*/
import { randomUUID } from 'crypto';
import { setTimeout } from 'timers/promises';
import test from 'ava';
import { Client, Connection } from '@temporalio/client';
import type { WorkerOptions } from '@temporalio/worker';
import {
DefaultLogger,
PromiseCompletionTimeoutError,
Runtime,
TransportError,
UnexpectedError,
} from '@temporalio/worker';
import { PromiseCompletionTimeoutError, Runtime, TransportError, UnexpectedError } from '@temporalio/worker';
import {
createWorkerWithConnection,
isBun,
Expand All @@ -33,56 +29,36 @@ function createWorker(options: Partial<WorkerOptions> & Pick<WorkerOptions, 'tas

if (RUN_INTEGRATION_TESTS) {
test.serial('Worker shuts down gracefully', async (t) => {
let flushCalls = 0;
Runtime.install({
logger: Object.assign(new DefaultLogger('ERROR', () => undefined), {
flush: () => {
flushCalls++;
},
}),
});
const { worker, connection } = await createWorker({ taskQueue: uniqueTaskQueue(t.title) });
t.is(worker.getState(), 'INITIALIZED');
const workerRun = worker.run();
t.is(worker.getState(), 'RUNNING');
const flushesBeforeShutdown = flushCalls;
process.emit('SIGINT', 'SIGINT');
await new Promise((resolve) => process.nextTick(resolve));
t.is(worker.getState(), 'DRAINING');
await workerRun;
t.is(worker.getState(), 'STOPPED');
t.true(flushCalls > flushesBeforeShutdown);
await t.throwsAsync(worker.run(), { message: 'Poller was already started' });
const flushesBeforeClose = flushCalls;
t.not(Runtime._instance, undefined, 'The supplied connection keeps the Runtime alive after Worker shutdown');
await connection.close();
t.true(flushCalls > flushesBeforeClose);
t.is(Runtime._instance, undefined, 'Closing the last supplied connection shuts down the Runtime');
});

test.serial("Worker.runUntil doesn't hang if provided promise survives to Worker's shutdown", async (t) => {
let flushCalls = 0;
Runtime.install({
logger: Object.assign(new DefaultLogger('ERROR', () => undefined), {
flush: () => {
flushCalls++;
},
}),
});
const { worker, connection } = await createWorker({ taskQueue: uniqueTaskQueue(t.title) });
const p = worker.runUntil(
new Promise(() => {
// A promise that will never unblock.
})
);
t.is(worker.getState(), 'RUNNING');
const flushesBeforeShutdown = flushCalls;
worker.shutdown();
t.is(worker.getState(), 'DRAINING');
await t.throwsAsync(p, { instanceOf: PromiseCompletionTimeoutError });
t.is(worker.getState(), 'STOPPED');
t.true(flushCalls > flushesBeforeShutdown);
const flushesBeforeClose = flushCalls;
t.not(Runtime._instance, undefined, 'The supplied connection keeps the Runtime alive after Worker shutdown');
await connection.close();
t.true(flushCalls > flushesBeforeClose);
t.is(Runtime._instance, undefined, 'Closing the last supplied connection shuts down the Runtime');
});

test.serial('Worker shuts down gracefully if interrupted before running', async (t) => {
Expand Down
33 changes: 8 additions & 25 deletions packages/test/src/test-worker-owned-connection.local.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
/**
* Implicit connection ownership requires the default local server: Worker.create() cannot configure
* the connection it creates internally. Configured connections are covered by the integration suites.
* Verify that Workers close connections they create themselves, shutting down the Runtime without
* an explicit connection.close(). test-worker-lifecycle-integration.ts covers supplied connections,
* which callers must close after Worker shutdown, against both local servers and Cloud.
* These tests require the default local server because Worker.create() accepts connection settings
* only through a supplied connection.
*/
import { DefaultLogger, PromiseCompletionTimeoutError, Runtime } from '@temporalio/worker';
import { PromiseCompletionTimeoutError, Runtime } from '@temporalio/worker';
import { RUN_INTEGRATION_TESTS, test, uniqueTaskQueue, Worker } from './helpers';
import { defaultOptions } from './mock-native-worker';

if (RUN_INTEGRATION_TESTS) {
test.serial('Worker closes its implicit connection and shuts down the Runtime', async (t) => {
let flushCalls = 0;
Runtime.install({
logger: Object.assign(new DefaultLogger('ERROR', () => undefined), {
flush: () => {
flushCalls++;
},
}),
});
const worker = await Worker.create({ ...defaultOptions, taskQueue: uniqueTaskQueue(t.title) });
t.is(worker.getState(), 'INITIALIZED');
t.not(Runtime._instance, undefined);
const workerRun = worker.run();
t.is(worker.getState(), 'RUNNING');
const flushesBeforeShutdown = flushCalls;
process.emit('SIGINT', 'SIGINT');
await new Promise((resolve) => process.nextTick(resolve));
t.is(worker.getState(), 'DRAINING');
await workerRun;
t.is(worker.getState(), 'STOPPED');
await t.throwsAsync(worker.run(), { message: 'Poller was already started' });
t.is(Runtime._instance, undefined);
t.true(flushCalls > flushesBeforeShutdown);
t.is(Runtime._instance, undefined, 'Worker shutdown closes its implicit connection and shuts down the Runtime');
});

test.serial(
'Worker.runUntil closes its implicit connection when the provided promise outlives shutdown',
async (t) => {
let flushCalls = 0;
Runtime.install({
logger: Object.assign(new DefaultLogger('ERROR', () => undefined), {
flush: () => {
flushCalls++;
},
}),
});
const worker = await Worker.create({ ...defaultOptions, taskQueue: uniqueTaskQueue(t.title) });
t.not(Runtime._instance, undefined);
const workerRun = worker.runUntil(
Expand All @@ -51,13 +36,11 @@ if (RUN_INTEGRATION_TESTS) {
})
);
t.is(worker.getState(), 'RUNNING');
const flushesBeforeShutdown = flushCalls;
worker.shutdown();
t.is(worker.getState(), 'DRAINING');
await t.throwsAsync(workerRun, { instanceOf: PromiseCompletionTimeoutError });
t.is(worker.getState(), 'STOPPED');
t.is(Runtime._instance, undefined);
t.true(flushCalls > flushesBeforeShutdown);
t.is(Runtime._instance, undefined, 'runUntil closes the implicit connection and shuts down the Runtime');
}
);
}
Loading