From e5dbb4abd6636c282f7ca33ddd495505d6af19bb Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:52:33 +0100 Subject: [PATCH 1/2] test(cloudflare): Report the cause when a test request fails `makeRequest` returned `undefined` for a failed request. The test then went on and failed with an assertion on `undefined`, which hid the cause in 23 CI runs. It now throws the error. Also, do not log the sub-worker exit that the teardown causes as an error. Mark the promise of `collectStreamedSpans` as handled, so a test that fails before it awaits the promise does not report a second "Unhandled error". --- .../cloudflare-integration-tests/runner.ts | 63 ++++++++++--------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/dev-packages/cloudflare-integration-tests/runner.ts b/dev-packages/cloudflare-integration-tests/runner.ts index aa2c0ee73dae..bff5bc5ce42a 100644 --- a/dev-packages/cloudflare-integration-tests/runner.ts +++ b/dev-packages/cloudflare-integration-tests/runner.ts @@ -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}`)); }); @@ -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 ( method: 'get' | 'post', @@ -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 { const spansByTrace = new Map(); let matched: SerializedStreamedSpan[] = []; - await waitForSpans(spans => { + const collected = waitForSpans(spans => { for (const span of spans) { const spansOfTrace = spansByTrace.get(span.trace_id); if (spansOfTrace) { @@ -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 { const matchesSegment = From d8b35f790e3eac79938bbaa3c9ee276027647c4b Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:52:33 +0100 Subject: [PATCH 2/2] test(cloudflare): Measure the fastest of five Durable Object invocations The test compared one invocation per trigger with a wall-clock limit of 250 ms. On a busy runner, single invocations took 258 to 636 ms without the regression, which failed 11 CI runs in two weeks. A pending SDK timer delays every invocation, and a busy runner delays only some. The test now takes the fastest of five invocations, each on a new Durable Object instance. --- .../invocation-duration/index.ts | 3 +- .../durableobject/invocation-duration/test.ts | 62 +++++++++++-------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/index.ts b/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/index.ts index abc4a78578af..8d75f8b36dfd 100644 --- a/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/index.ts +++ b/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/index.ts @@ -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()); diff --git a/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/test.ts b/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/test.ts index 11db17ae2348..d9e1cda6eeb2 100644 --- a/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/test.ts +++ b/dev-packages/cloudflare-integration-tests/suites/durableobject/invocation-duration/test.ts @@ -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. @@ -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('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 { + // 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('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); });