Skip to content

Commit 7c773d8

Browse files
author
cod1k
committed
Add signal handling to test runners and utilities
This change integrates `signal` handling for all test runners, utilities, and server setups, allowing better management of abortable operations. It improves reliability and ensures proper cleanup during execution interruptions.
1 parent e4b5bd1 commit 7c773d8

File tree

190 files changed

+1031
-873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+1031
-873
lines changed

dev-packages/cloudflare-integration-tests/runner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type StartResult = {
5454

5555
/** Creates a test runner */
5656
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
57-
export function createRunner(...paths: string[]) {
57+
export function createRunner({ signal }: { readonly signal?: AbortSignal }, ...paths: string[]) {
5858
const testPath = join(...paths);
5959

6060
if (!existsSync(testPath)) {
@@ -130,7 +130,7 @@ export function createRunner(...paths: string[]) {
130130
}
131131
}
132132

133-
createBasicSentryServer(newEnvelope)
133+
createBasicSentryServer(newEnvelope, { signal })
134134
.then(([mockServerPort, mockServerClose]) => {
135135
if (mockServerClose) {
136136
CLEANUP_STEPS.add(() => {
@@ -155,7 +155,7 @@ export function createRunner(...paths: string[]) {
155155
'--var',
156156
`SENTRY_DSN:http://public@localhost:${mockServerPort}/1337`,
157157
],
158-
{ stdio },
158+
{ stdio, signal },
159159
);
160160

161161
CLEANUP_STEPS.add(() => {

dev-packages/cloudflare-integration-tests/suites/basic/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { expect, it } from 'vitest';
22
import { eventEnvelope } from '../../expect';
33
import { createRunner } from '../../runner';
44

5-
it('Basic error in fetch handler', async () => {
6-
const runner = createRunner(__dirname)
5+
it('Basic error in fetch handler', async ({ signal }) => {
6+
const runner = createRunner({ signal }, __dirname)
77
.expect(
88
eventEnvelope({
99
level: 'error',

dev-packages/cloudflare-integration-tests/suites/tracing/anthropic-ai/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { createRunner } from '../../../runner';
66
// want to test that the instrumentation does not break in our
77
// cloudflare SDK.
88

9-
it('traces a basic message creation request', async () => {
10-
const runner = createRunner(__dirname)
9+
it('traces a basic message creation request', async ({ signal }) => {
10+
const runner = createRunner({ signal }, __dirname)
1111
.ignore('event')
1212
.expect(envelope => {
1313
const transactionEvent = envelope[1]?.[0]?.[1] as any;

dev-packages/cloudflare-integration-tests/suites/tracing/durableobject/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect, it } from 'vitest';
22
import { createRunner } from '../../../runner';
33

4-
it('traces a durable object method', async () => {
5-
const runner = createRunner(__dirname)
4+
it('traces a durable object method', async ({ signal }) => {
5+
const runner = createRunner({ signal }, __dirname)
66
.expect(envelope => {
77
const transactionEvent = envelope[1]?.[0]?.[1];
88
expect(transactionEvent).toEqual(

dev-packages/cloudflare-integration-tests/suites/tracing/google-genai/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { createRunner } from '../../../runner';
66
// want to test that the instrumentation does not break in our
77
// cloudflare SDK.
88

9-
it('traces Google GenAI chat creation and message sending', async () => {
10-
const runner = createRunner(__dirname)
9+
it('traces Google GenAI chat creation and message sending', async ({ signal }) => {
10+
const runner = createRunner({ signal }, __dirname)
1111
.ignore('event')
1212
.expect(envelope => {
1313
const transactionEvent = envelope[1]?.[0]?.[1] as any;

dev-packages/cloudflare-integration-tests/suites/tracing/openai/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { createRunner } from '../../../runner';
66
// want to test that the instrumentation does not break in our
77
// cloudflare SDK.
88

9-
it('traces a basic chat completion request', async () => {
10-
const runner = createRunner(__dirname)
9+
it('traces a basic chat completion request', async ({ signal }) => {
10+
const runner = createRunner({ signal }, __dirname)
1111
.ignore('event')
1212
.expect(envelope => {
13-
const transactionEvent = envelope[1]?.[0]?.[1];
13+
const transactionEvent = envelope[1]?.[0]?.[1] as any;
1414

1515
expect(transactionEvent.transaction).toBe('GET /');
1616
expect(transactionEvent.spans).toEqual(

dev-packages/e2e-tests/registrySetup.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ function groupCIOutput(groupTitle: string, fn: () => void): void {
1717
}
1818
}
1919

20-
export function registrySetup(): void {
20+
export function registrySetup(signal?: AbortSignal): void {
2121
groupCIOutput('Test Registry Setup', () => {
2222
// Stop test registry container (Verdaccio) if it was already running
23-
childProcess.spawnSync('docker', ['stop', TEST_REGISTRY_CONTAINER_NAME], { stdio: 'ignore' });
24-
console.log('Stopped previously running test registry');
23+
const stop = (): void => {
24+
childProcess.spawnSync('docker', ['stop', TEST_REGISTRY_CONTAINER_NAME], { stdio: 'ignore' });
25+
console.log('Stopped previously running test registry');
26+
};
27+
stop();
28+
signal?.addEventListener('abort', () => stop());
2529

2630
// Start test registry (Verdaccio)
2731
const startRegistryProcessResult = childProcess.spawnSync(
@@ -38,7 +42,7 @@ export function registrySetup(): void {
3842
`${__dirname}/verdaccio-config:/verdaccio/conf`,
3943
`verdaccio/verdaccio:${VERDACCIO_VERSION}`,
4044
],
41-
{ encoding: 'utf8', stdio: 'inherit' },
45+
{ encoding: 'utf8', stdio: 'inherit', signal },
4246
);
4347

4448
if (startRegistryProcessResult.status !== 0) {
@@ -60,6 +64,7 @@ export function registrySetup(): void {
6064
{
6165
encoding: 'utf8',
6266
stdio: 'inherit',
67+
signal,
6368
},
6469
);
6570

@@ -82,6 +87,7 @@ export function registrySetup(): void {
8287
{
8388
encoding: 'utf8',
8489
stdio: 'inherit',
90+
signal,
8591
},
8692
);
8793

dev-packages/e2e-tests/run.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const DEFAULT_DSN = 'https://username@domain/123';
1212
const DEFAULT_SENTRY_ORG_SLUG = 'sentry-javascript-sdks';
1313
const DEFAULT_SENTRY_PROJECT = 'sentry-javascript-e2e-tests';
1414

15-
function asyncExec(command: string, options: { env: Record<string, string | undefined>; cwd: string }): Promise<void> {
15+
function asyncExec(
16+
command: string,
17+
options: { env: Record<string, string | undefined>; cwd: string; signal: AbortSignal },
18+
): Promise<void> {
1619
return new Promise((resolve, reject) => {
1720
const process = spawn(command, { ...options, shell: true });
1821

@@ -41,6 +44,8 @@ async function run(): Promise<void> {
4144
// Load environment variables from .env file locally
4245
dotenv.config();
4346

47+
const abortController = new AbortController();
48+
4449
// Allow to run a single app only via `yarn test:run <app-name>`
4550
const appName = process.argv[2] || '';
4651

@@ -65,11 +70,11 @@ async function run(): Promise<void> {
6570
console.log('');
6671

6772
if (!process.env.SKIP_REGISTRY) {
68-
registrySetup();
73+
registrySetup(abortController.signal);
6974
}
7075

71-
await asyncExec('pnpm clean:test-applications', { env, cwd: __dirname });
72-
await asyncExec('pnpm cache delete "@sentry/*"', { env, cwd: __dirname });
76+
await asyncExec('pnpm clean:test-applications', { env, cwd: __dirname, signal: abortController.signal });
77+
await asyncExec('pnpm cache delete "@sentry/*"', { env, cwd: __dirname, signal: abortController.signal });
7378

7479
const testAppPaths = appName ? [appName.trim()] : globSync('*', { cwd: `${__dirname}/test-applications/` });
7580

@@ -84,15 +89,17 @@ async function run(): Promise<void> {
8489
const cwd = tmpDirPath;
8590

8691
console.log(`Building ${testAppPath} in ${tmpDirPath}...`);
87-
await asyncExec('volta run pnpm test:build', { env, cwd });
92+
await asyncExec('volta run pnpm test:build', { env, cwd, signal: abortController.signal });
8893

8994
console.log(`Testing ${testAppPath}...`);
90-
await asyncExec('volta run pnpm test:assert', { env, cwd });
95+
await asyncExec('volta run pnpm test:assert', { env, cwd, signal: abortController.signal });
9196

9297
// clean up (although this is tmp, still nice to do)
9398
await rm(tmpDirPath, { recursive: true });
9499
}
100+
abortController.abort();
95101
} catch (error) {
102+
abortController.abort(error);
96103
console.error(error);
97104
process.exit(1);
98105
}

dev-packages/e2e-tests/test-applications/aws-serverless/src/stack.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { Stack, CfnResource, StackProps } from 'aws-cdk-lib';
1+
import { CfnResource, Stack, StackProps } from 'aws-cdk-lib';
22
import { Construct } from 'constructs';
33
import * as path from 'node:path';
44
import * as fs from 'node:fs';
55
import * as os from 'node:os';
66
import * as dns from 'node:dns/promises';
7-
import { platform } from 'node:process';
87
import { globSync } from 'glob';
98
import { execFileSync } from 'node:child_process';
109

@@ -123,10 +122,5 @@ export async function getHostIp() {
123122
return host.address;
124123
}
125124

126-
if (platform === 'darwin' || platform === 'win32') {
127-
return 'host.docker.internal';
128-
}
129-
130-
const host = await dns.lookup(os.hostname());
131-
return host.address;
125+
return 'host.docker.internal';
132126
}

dev-packages/node-core-integration-tests/suites/tracing/dsc-txn-name-update/test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { createTestServer } from '../../../utils/server';
66
// This test requires Node.js 22+ because it depends on the 'http.client.request.created'
77
// diagnostic channel for baggage header propagation, which only exists since Node 22.12.0+ and 23.2.0+
88
conditionalTest({ min: 22 })('node >=22', () => {
9-
test('adds current transaction name to baggage when the txn name is high-quality', async () => {
9+
test('adds current transaction name to baggage when the txn name is high-quality', async ({ signal }) => {
1010
expect.assertions(5);
1111

1212
let traceId: string | undefined;
1313

14-
const [SERVER_URL, closeTestServer] = await createTestServer()
14+
const [SERVER_URL, closeTestServer] = await createTestServer({ signal })
1515
.get('/api/v0', (headers: Record<string, string | string[] | undefined>) => {
1616
const baggageItems = getBaggageHeaderItems(headers);
1717
traceId = baggageItems.find(item => item.startsWith('sentry-trace_id='))?.split('=')[1] as string;
@@ -54,7 +54,7 @@ conditionalTest({ min: 22 })('node >=22', () => {
5454
})
5555
.start();
5656

57-
await createRunner(__dirname, 'scenario-headers.ts')
57+
await createRunner({ signal }, __dirname, 'scenario-headers.ts')
5858
.withEnv({ SERVER_URL })
5959
.expect({
6060
transaction: {},
@@ -65,10 +65,10 @@ conditionalTest({ min: 22 })('node >=22', () => {
6565
});
6666
});
6767

68-
test('adds current transaction name to trace envelope header when the txn name is high-quality', async () => {
68+
test('adds current transaction name to trace envelope header when the txn name is high-quality', async ({ signal }) => {
6969
expect.assertions(4);
7070

71-
await createRunner(__dirname, 'scenario-events.ts')
71+
await createRunner({ signal }, __dirname, 'scenario-events.ts')
7272
.expectHeader({
7373
event: {
7474
trace: {

0 commit comments

Comments
 (0)