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
63 changes: 33 additions & 30 deletions dev-packages/cloudflare-integration-tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ export function createRunner(...paths: string[]) {

childSubWorker.on('error', onChildError);
childSubWorker.on('exit', code => {
// The teardown stops the sub-worker.
if (disposed) {
return;
}
onChildError(new Error(`Sub-worker exited with code ${code}`));
});

Expand Down Expand Up @@ -575,40 +579,35 @@ export function createRunner(...paths: string[]) {

if (process.env.DEBUG) log('making request', method, url, headers, body);

let res: Response;
try {
const res = await fetchWithRetry(url, { headers, method, body }, expectError ? { maxRetries: 1 } : {});

if (!res.ok) {
if (!expectError) {
const responseBody = await res.text().catch(() => '');
reject(
new Error(
`Expected request to "${path}" to succeed, but got a ${res.status} response: ${responseBody.slice(0, 500)}`,
),
);
}

return;
}

res = await fetchWithRetry(url, { headers, method, body }, expectError ? { maxRetries: 1 } : {});
} catch (e) {
if (expectError) {
reject(new Error(`Expected request to "${path}" to fail, but got a ${res.status} response`));
return;
return undefined;
}
throw e;
}

if (res.headers.get('content-type')?.includes('application/json')) {
return await res.json();
if (expectError) {
if (res.ok) {
throw new Error(`Expected request to "${path}" to fail, but got a ${res.status} response`);
}
return undefined;
}

return (await res.text()) as T;
} catch (e) {
if (expectError) {
return;
}
if (!res.ok) {
const responseBody = await res.text().catch(() => '');
throw new Error(
`Expected request to "${path}" to succeed, but got a ${res.status} response: ${responseBody.slice(0, 500)}`,
);
}

reject(e);
return;
if (res.headers.get('content-type')?.includes('application/json')) {
return res.json();
}

return (await res.text()) as T;
},
makeRequestAndWaitForEnvelope: async function <T>(
method: 'get' | 'post',
Expand All @@ -625,13 +624,13 @@ export function createRunner(...paths: string[]) {
await Promise.all(envelopePromises);
return result;
},
collectStreamedSpans: async function (
collectStreamedSpans: function (
isDone: (spansOfTrace: SerializedStreamedSpan[]) => boolean,
): Promise<SerializedStreamedSpan[]> {
const spansByTrace = new Map<string, SerializedStreamedSpan[]>();
let matched: SerializedStreamedSpan[] = [];

await waitForSpans(spans => {
const collected = waitForSpans(spans => {
for (const span of spans) {
const spansOfTrace = spansByTrace.get(span.trace_id);
if (spansOfTrace) {
Expand All @@ -651,9 +650,13 @@ export function createRunner(...paths: string[]) {
}

return false;
});
}).then(() => matched);

// A test creates this promise before its request and awaits it after. If the test fails in
// between, nothing observes a rejection here, and Vitest reports a second "Unhandled error".
collected.catch(() => {});

return matched;
return collected;
},
collectStreamedSpansUntilSegment: function (segment: SegmentMatcher): Promise<SerializedStreamedSpan[]> {
const matchesSegment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default {
return env.TAIL.fetch(request);
}

const stub = env.TEST_DURABLE_OBJECT.get(env.TEST_DURABLE_OBJECT.idFromName(url.pathname));
// The query selects the instance, so a test can invoke a new instance with each request.
const stub = env.TEST_DURABLE_OBJECT.get(env.TEST_DURABLE_OBJECT.idFromName(url.pathname + url.search));

if (url.pathname === '/rpc') {
return new Response(await stub.ping());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface Invocation {
durationMs: number;
}

const INVOCATIONS_PER_TRIGGER = 5;

// Regression test for https://github.com/getsentry/sentry-javascript/issues/25489
// workerd keeps a Durable Object invocation open until its pending timers fire, so an SDK timer
// that is still pending when the method returns makes the invocation longer, and billed.
Expand All @@ -16,31 +18,37 @@ it('does not keep Durable Object invocations open after the method returns', asy
.withWranglerArgs('--config', join(__dirname, 'wrangler-tail.jsonc'))
.start(signal);

await runner.makeRequest('get', '/fetch');
await runner.makeRequest('get', '/rpc');
await runner.makeRequest('get', '/alarm');

const { fetchInvocation, rpcInvocation, alarmInvocation } = await vi.waitFor(
async () => {
const invocations = (await runner.makeRequest<Invocation[]>('get', '/invocations')) ?? [];
const result = {
fetchInvocation: invocations.find(invocation => invocation.trigger === 'fetch'),
rpcInvocation: invocations.find(invocation => invocation.trigger === 'ping'),
alarmInvocation: invocations.find(invocation => invocation.trigger === 'alarm'),
};

expect(result.fetchInvocation).toBeDefined();
expect(result.rpcInvocation).toBeDefined();
expect(result.alarmInvocation).toBeDefined();

return result;
},
{ timeout: 10_000, interval: 100 },
);

// An invocation takes a few ms locally. The limit leaves room for slow CI machines and still
// fails for a 500 ms timer.
expect(fetchInvocation?.durationMs).toBeLessThan(250);
expect(rpcInvocation?.durationMs).toBeLessThan(250);
expect(alarmInvocation?.durationMs).toBeLessThan(250);
// A pending SDK timer delays every invocation. A busy CI machine delays only some of them, by as
// much as the timer does. Only the fastest of several invocations tells the two apart.
async function measureFastestInvocation(path: string, trigger: string): Promise<number> {
// Each request invokes a new instance, because the first invocation of an instance also sets up the SDK.
for (let instance = 0; instance < INVOCATIONS_PER_TRIGGER; instance++) {
await runner.makeRequest('get', `${path}?instance=${instance}`);
}

const durations = await vi.waitFor(
async () => {
const invocations = (await runner.makeRequest<Invocation[]>('get', '/invocations')) ?? [];
const durationsOfTrigger = invocations
.filter(invocation => invocation.trigger === trigger)
.map(invocation => invocation.durationMs);

expect(durationsOfTrigger).toHaveLength(INVOCATIONS_PER_TRIGGER);

return durationsOfTrigger;
},
{ timeout: 10_000, interval: 100 },
);

return Math.min(...durations);
}

const fetchDuration = await measureFastestInvocation('/fetch', 'fetch');
const rpcDuration = await measureFastestInvocation('/rpc', 'ping');
const alarmDuration = await measureFastestInvocation('/alarm', 'alarm');

// An invocation takes a few ms locally, and 500 ms with the timer of the regression.
expect(fetchDuration).toBeLessThan(250);
expect(rpcDuration).toBeLessThan(250);
expect(alarmDuration).toBeLessThan(250);
});
Loading