Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 41 additions & 0 deletions dev-packages/cloudflare-integration-tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ type StartResult = {
path: string,
options?: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean },
): Promise<T | undefined>;
makeRequestAndWaitForEnvelope<T>(
method: 'get' | 'post',
path: string,
expected: Expected | Expected[],
options?: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean },
): Promise<T | undefined>;
};

/** Creates a test runner */
Expand Down Expand Up @@ -108,6 +114,7 @@ export function createRunner(...paths: string[]) {
const expectedEnvelopeCount = expectedEnvelopes.length;

let envelopeCount = 0;
const envelopeWaiters: { expected: Expected; resolve: () => void; reject: (e: unknown) => void }[] = [];
const { resolve: setWorkerPort, promise: workerPortPromise } = deferredPromise<number>();
let child: ReturnType<typeof spawn> | undefined;
let childSubWorker: ReturnType<typeof spawn> | undefined;
Expand All @@ -120,6 +127,12 @@ export function createRunner(...paths: string[]) {
}
}

function waitForEnvelope(expected: Expected): Promise<void> {
return new Promise((resolveWaiter, rejectWaiter) => {
envelopeWaiters.push({ expected, resolve: resolveWaiter, reject: rejectWaiter });
});
}

function assertEnvelopeMatches(expected: Expected, envelope: Envelope): void {
if (typeof expected === 'function') {
expected(envelope);
Expand All @@ -137,6 +150,18 @@ export function createRunner(...paths: string[]) {
return;
}

// Check per-request waiters first (FIFO order)
if (envelopeWaiters.length > 0) {
const waiter = envelopeWaiters.shift()!;
try {
assertEnvelopeMatches(waiter.expected, envelope);
waiter.resolve();
} catch (e) {
waiter.reject(e);
}
return;
}

try {
if (unordered) {
// find any matching expected envelope
Expand Down Expand Up @@ -242,6 +267,10 @@ export function createRunner(...paths: string[]) {
`SENTRY_DSN:http://public@localhost:${mockServerPort}/1337`,
'--var',
`SERVER_URL:${serverUrl}`,
'--port',
'0',
Comment thread
cursor[bot] marked this conversation as resolved.
'--inspector-port',
'0',
...extraWranglerArgs,
],
{ stdio, signal },
Expand Down Expand Up @@ -304,6 +333,18 @@ export function createRunner(...paths: string[]) {
return;
}
},
makeRequestAndWaitForEnvelope: async function <T>(
method: 'get' | 'post',
path: string,
expected: Expected | Expected[],
options: { headers?: Record<string, string>; data?: BodyInit; expectError?: boolean } = {},
): Promise<T | undefined> {
const expectations = Array.isArray(expected) ? expected : [expected];
const envelopePromises = expectations.map(e => waitForEnvelope(e));
const result = await this.makeRequest<T>(method, path, options);
await Promise.all(envelopePromises);
return result;
},
};
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ it('sends a streamed span envelope with correct spans for a manually started spa
},
'url.port': {
type: 'string',
value: '8787',
value: expect.stringMatching(/^\d{4,5}$/),
},
'url.scheme': {
type: 'string',
Expand All @@ -221,7 +221,7 @@ it('sends a streamed span envelope with correct spans for a manually started spa
},
'http.request.header.cf_connecting_ip': {
type: 'string',
value: '::1',
value: expect.stringMatching(/^(::1|127\.0\.0\.1)$/),
},
'http.request.header.host': {
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ it.skip('sends child spans on repeated Durable Object calls', async ({ signal })

// All 5 child spans should be present
expect(transactionEvent.spans).toHaveLength(5);
expect(transactionEvent.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({ description: 'task-1', op: 'task' }),
expect.objectContaining({ description: 'task-2', op: 'task' }),
expect.objectContaining({ description: 'task-3', op: 'task' }),
expect.objectContaining({ description: 'task-4', op: 'task' }),
expect.objectContaining({ description: 'task-5', op: 'task' }),
]),
);
expect(transactionEvent.spans).toEqual([
expect.objectContaining({ description: 'task-1', op: 'task' }),
expect.objectContaining({ description: 'task-2', op: 'task' }),
expect.objectContaining({ description: 'task-3', op: 'task' }),
expect.objectContaining({ description: 'task-4', op: 'task' }),
expect.objectContaining({ description: 'task-5', op: 'task' }),
]);

// All child spans share the root trace_id
const rootTraceId = transactionEvent.contexts?.trace?.trace_id;
Expand All @@ -43,13 +41,12 @@ it.skip('sends child spans on repeated Durable Object calls', async ({ signal })
}
}

// Expect 5 transaction envelopes — one per call.
const runner = createRunner(__dirname).expectN(5, assertDoWorkEnvelope).start(signal);
const runner = createRunner(__dirname).start(signal);

await runner.makeRequest('get', '/');
await runner.makeRequest('get', '/');
await runner.makeRequest('get', '/');
await runner.makeRequest('get', '/');
await runner.makeRequest('get', '/');
await runner.completed();
// Each request waits for its envelope to be received and validated before proceeding.
await runner.makeRequestAndWaitForEnvelope('get', '/', assertDoWorkEnvelope);
await runner.makeRequestAndWaitForEnvelope('get', '/', assertDoWorkEnvelope);
await runner.makeRequestAndWaitForEnvelope('get', '/', assertDoWorkEnvelope);
await runner.makeRequestAndWaitForEnvelope('get', '/', assertDoWorkEnvelope);
await runner.makeRequestAndWaitForEnvelope('get', '/', assertDoWorkEnvelope);
});
3 changes: 3 additions & 0 deletions dev-packages/cloudflare-integration-tests/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export default defineConfig({
singleThread: true,
},
},
sequence: {
shuffle: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the shuffle flag it was consistently failing, this is why this is added in this PR as well

Maybe I misunderstand but shouldn't shuffle be set to false then?

},
reporters: process.env.DEBUG
? ['default', { summary: false }]
: process.env.GITHUB_ACTIONS
Expand Down
Loading