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
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import * as Sentry from '@sentry/react-router';
import type { Route } from './+types/isolation';

const waitingRequests = new Map<string, () => void>();
const waitingRequests = new Set<string>();

// 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<void> {
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<void> {
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) {
Expand Down
18 changes: 14 additions & 4 deletions dev-packages/test-utils/src/event-proxy-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SerializedStreamedSpan[]> {
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))),
);
}

Expand Down
Loading