diff --git a/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/errors.test.ts index e3c44c67d4d6..8a496a69eedc 100644 --- a/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/errors.test.ts @@ -188,10 +188,12 @@ test.describe('middleware errors', () => { return event.exception?.values?.[0]?.value === 'Service Unavailable from middleware'; }); + // On Cloudflare the middleware span can reach the proxy in a later envelope than the segment span. const segmentPromise = collectStreamedSpansUntilSegment( APP_NAME, segment => getSpanOp(segment) === 'http.server' && !!segment.name?.includes('/test-errors/middleware-http-exception'), + span => getSpanOp(span) === 'middleware', ); const response = await fetch(`${baseURL}/test-errors/middleware-http-exception`); @@ -226,14 +228,19 @@ test.describe('middleware errors', () => { return false; }); - const segmentPromise = collectStreamedSpansUntilSegment(APP_NAME, segment => { - if (RUNTIME === 'cloudflare') { - return ( - getSpanOp(segment) === 'http.server' && !!segment.name?.includes('/test-errors/middleware-http-exception-4xx') - ); - } - return getSpanOp(segment) === 'http.server' && segment.name === 'GET /'; - }); + const segmentPromise = collectStreamedSpansUntilSegment( + APP_NAME, + segment => { + if (RUNTIME === 'cloudflare') { + return ( + getSpanOp(segment) === 'http.server' && + !!segment.name?.includes('/test-errors/middleware-http-exception-4xx') + ); + } + return getSpanOp(segment) === 'http.server' && segment.name === 'GET /'; + }, + RUNTIME === 'cloudflare' ? span => getSpanOp(span) === 'middleware' : undefined, + ); const response = await fetch(`${baseURL}/test-errors/middleware-http-exception-4xx`); expect(response.status).toBe(401); diff --git a/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/middleware.test.ts b/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/middleware.test.ts index 6c2a04ac1040..9d75ea5eb6c1 100644 --- a/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/middleware.test.ts +++ b/dev-packages/e2e-tests/test-applications/hono-4-legacy/tests/middleware.test.ts @@ -179,8 +179,12 @@ for (const { name, prefix } of SCENARIOS) { // Other tests request the same route and only wait for the error event, so their segment can // still arrive while this test listens. Pin the trace to tell this request's spans apart. const traceId = randomBytes(16).toString('hex'); - const segmentPromise = collectStreamedSpans(APP_NAME, spansOfTrace => - spansOfTrace.some(span => span.is_segment && span.trace_id === traceId), + // On Cloudflare the middleware span can reach the proxy in a later envelope than the segment span. + const segmentPromise = collectStreamedSpans( + APP_NAME, + spansOfTrace => + spansOfTrace.some(span => span.is_segment && span.trace_id === traceId) && + spansOfTrace.some(span => getSpanOp(span) === 'middleware'), ); await fetch(`${baseURL}${prefix}/error`, { diff --git a/dev-packages/e2e-tests/test-applications/hono-4/tests/errors.test.ts b/dev-packages/e2e-tests/test-applications/hono-4/tests/errors.test.ts index b315233ec15b..4534a914aa6c 100644 --- a/dev-packages/e2e-tests/test-applications/hono-4/tests/errors.test.ts +++ b/dev-packages/e2e-tests/test-applications/hono-4/tests/errors.test.ts @@ -144,10 +144,12 @@ test.describe('middleware errors', () => { return event.exception?.values?.[0]?.value === 'Service Unavailable from middleware'; }); + // On Cloudflare the middleware span can reach the proxy in a later envelope than the segment span. const segmentPromise = collectStreamedSpansUntilSegment( APP_NAME, segment => getSpanOp(segment) === 'http.server' && !!segment.name?.includes('/test-errors/middleware-http-exception'), + span => getSpanOp(span) === 'middleware', ); const response = await fetch(`${baseURL}/test-errors/middleware-http-exception`); diff --git a/dev-packages/e2e-tests/test-applications/hono-4/tests/middleware.test.ts b/dev-packages/e2e-tests/test-applications/hono-4/tests/middleware.test.ts index 6c2a04ac1040..9d75ea5eb6c1 100644 --- a/dev-packages/e2e-tests/test-applications/hono-4/tests/middleware.test.ts +++ b/dev-packages/e2e-tests/test-applications/hono-4/tests/middleware.test.ts @@ -179,8 +179,12 @@ for (const { name, prefix } of SCENARIOS) { // Other tests request the same route and only wait for the error event, so their segment can // still arrive while this test listens. Pin the trace to tell this request's spans apart. const traceId = randomBytes(16).toString('hex'); - const segmentPromise = collectStreamedSpans(APP_NAME, spansOfTrace => - spansOfTrace.some(span => span.is_segment && span.trace_id === traceId), + // On Cloudflare the middleware span can reach the proxy in a later envelope than the segment span. + const segmentPromise = collectStreamedSpans( + APP_NAME, + spansOfTrace => + spansOfTrace.some(span => span.is_segment && span.trace_id === traceId) && + spansOfTrace.some(span => getSpanOp(span) === 'middleware'), ); await fetch(`${baseURL}${prefix}/error`, { diff --git a/dev-packages/e2e-tests/test-applications/nextjs-15-t3/global-setup.mjs b/dev-packages/e2e-tests/test-applications/nextjs-15-t3/global-setup.mjs new file mode 100644 index 000000000000..a4eceeddd5dc --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nextjs-15-t3/global-setup.mjs @@ -0,0 +1,6 @@ +// In dev mode, Next.js compiles a route when it is requested for the first time. On CI this takes about 30 seconds, +// which is as long as the timeout of a test. So both routes are requested here, before the tests start. +export default async function globalSetup() { + await fetch('http://localhost:3030/'); + await fetch('http://localhost:3030/api/trpc/post.getLatest'); +} diff --git a/dev-packages/e2e-tests/test-applications/nextjs-15-t3/playwright.config.mjs b/dev-packages/e2e-tests/test-applications/nextjs-15-t3/playwright.config.mjs index 8448829443d6..98718083dbe7 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-15-t3/playwright.config.mjs +++ b/dev-packages/e2e-tests/test-applications/nextjs-15-t3/playwright.config.mjs @@ -13,6 +13,7 @@ const config = getPlaywrightConfig( { // This comes with the risk of tests leaking into each other but the tests run quite slow so we should parallelize workers: '100%', + globalSetup: './global-setup.mjs', }, ); diff --git a/dev-packages/e2e-tests/test-applications/react-router-8-framework/app/routes/isolation.ts b/dev-packages/e2e-tests/test-applications/react-router-8-framework/app/routes/isolation.ts index 1a0783990068..739487c8665c 100644 --- a/dev-packages/e2e-tests/test-applications/react-router-8-framework/app/routes/isolation.ts +++ b/dev-packages/e2e-tests/test-applications/react-router-8-framework/app/routes/isolation.ts @@ -1,18 +1,20 @@ import * as Sentry from '@sentry/react-router'; import type { Route } from './+types/isolation'; -const waitingRequests = new Map void>(); +const waitingRequests = new Set(); // Holds the first of two requests until the second one has set its tag, so both requests use their scopes at the -// same time. -function waitForPartner(id: string, partner: string): Promise { - const releasePartner = waitingRequests.get(partner); - if (releasePartner) { - waitingRequests.delete(partner); - releasePartner(); - return Promise.resolve(); +// same time. The first request polls on a timer until the second one removes the entry of the first: workerd +// cancels a request that waits with no pending I/O, and a promise must not be resolved from another request. +async function waitForPartner(id: string, partner: string): Promise { + if (waitingRequests.delete(partner)) { + return; + } + + waitingRequests.add(id); + while (waitingRequests.has(id)) { + await new Promise(resolve => setTimeout(resolve, 10)); } - return new Promise(resolve => waitingRequests.set(id, resolve)); } export async function loader({ params, request }: Route.LoaderArgs) { diff --git a/dev-packages/test-utils/src/event-proxy-server.ts b/dev-packages/test-utils/src/event-proxy-server.ts index ae3aea0ff297..9969479ea8b1 100644 --- a/dev-packages/test-utils/src/event-proxy-server.ts +++ b/dev-packages/test-utils/src/event-proxy-server.ts @@ -660,24 +660,34 @@ export function collectStreamedSpans( * segment span's exact name or a predicate over the segment span, for cases where the name alone is * not unique (e.g. matching on `url.path` or the op). * - * Use {@link collectStreamedSpans} directly when the trace is only complete once specific child - * spans have arrived as well. + * On Cloudflare, a child span can arrive after its segment span. If the test checks a child span, + * pass `childToWaitFor` to wait for that span too. For any other condition, use + * {@link collectStreamedSpans} directly. * * @example * ```ts * const spans = await collectStreamedSpansUntilSegment(PROXY_SERVER_NAME, 'GET /nested-layout'); * const spans = await collectStreamedSpansUntilSegment(PROXY_SERVER_NAME, span => getSpanOp(span) === 'pageload'); + * const spans = await collectStreamedSpansUntilSegment( + * PROXY_SERVER_NAME, + * 'GET /users', + * span => getSpanOp(span) === 'middleware', + * ); * ``` */ export function collectStreamedSpansUntilSegment( proxyServerName: string, segment: string | ((segmentSpan: SerializedStreamedSpan) => boolean), + childToWaitFor?: (span: SerializedStreamedSpan) => boolean, ): Promise { const matchesSegment = typeof segment === 'string' ? (span: SerializedStreamedSpan) => span.name === segment : segment; - return collectStreamedSpans(proxyServerName, spansOfTrace => - spansOfTrace.some(span => span.is_segment && matchesSegment(span)), + return collectStreamedSpans( + proxyServerName, + spansOfTrace => + spansOfTrace.some(span => span.is_segment && matchesSegment(span)) && + (!childToWaitFor || spansOfTrace.some(span => childToWaitFor(span))), ); }