Add zones for route callback

This commit is contained in:
Yury Semikhatsky 2024-11-06 15:19:11 -08:00
parent df2ceb726a
commit 0d5ece5992
2 changed files with 53 additions and 2 deletions

View file

@ -22,14 +22,14 @@ import { Worker } from './worker';
import type { Headers, RemoteAddr, SecurityDetails, WaitForEventOptions } from './types'; import type { Headers, RemoteAddr, SecurityDetails, WaitForEventOptions } from './types';
import fs from 'fs'; import fs from 'fs';
import { mime } from '../utilsBundle'; import { mime } from '../utilsBundle';
import { assert, isString, headersObjectToArray, isRegExp, rewriteErrorMessage } from '../utils'; import { assert, isString, headersObjectToArray, isRegExp, rewriteErrorMessage, MultiMap, urlMatches, zones } from '../utils';
import type { URLMatch, Zone } from '../utils';
import { ManualPromise, LongStandingScope } from '../utils/manualPromise'; import { ManualPromise, LongStandingScope } from '../utils/manualPromise';
import { Events } from './events'; import { Events } from './events';
import type { Page } from './page'; import type { Page } from './page';
import { Waiter } from './waiter'; import { Waiter } from './waiter';
import type * as api from '../../types/types'; import type * as api from '../../types/types';
import type { HeadersArray } from '../common/types'; import type { HeadersArray } from '../common/types';
import { MultiMap, urlMatches, type URLMatch } from '../utils';
import { APIResponse } from './fetch'; import { APIResponse } from './fetch';
import type { Serializable } from '../../types/structs'; import type { Serializable } from '../../types/structs';
import type { BrowserContext } from './browserContext'; import type { BrowserContext } from './browserContext';
@ -811,12 +811,14 @@ export class RouteHandler {
readonly handler: RouteHandlerCallback; readonly handler: RouteHandlerCallback;
private _ignoreException: boolean = false; private _ignoreException: boolean = false;
private _activeInvocations: Set<{ complete: Promise<void>, route: Route }> = new Set(); private _activeInvocations: Set<{ complete: Promise<void>, route: Route }> = new Set();
private _svedZone: Zone;
constructor(baseURL: string | undefined, url: URLMatch, handler: RouteHandlerCallback, times: number = Number.MAX_SAFE_INTEGER) { constructor(baseURL: string | undefined, url: URLMatch, handler: RouteHandlerCallback, times: number = Number.MAX_SAFE_INTEGER) {
this._baseURL = baseURL; this._baseURL = baseURL;
this._times = times; this._times = times;
this.url = url; this.url = url;
this.handler = handler; this.handler = handler;
this._svedZone = zones.currentZone();
} }
static prepareInterceptionPatterns(handlers: RouteHandler[]) { static prepareInterceptionPatterns(handlers: RouteHandler[]) {
@ -840,6 +842,10 @@ export class RouteHandler {
} }
public async handle(route: Route): Promise<boolean> { public async handle(route: Route): Promise<boolean> {
return await this._svedZone.run(async () => this._handleImpl(route));
}
private async _handleImpl(route: Route): Promise<boolean> {
const handlerInvocation = { complete: new ManualPromise(), route } ; const handlerInvocation = { complete: new ManualPromise(), route } ;
this._activeInvocations.add(handlerInvocation); this._activeInvocations.add(handlerInvocation);
try { try {

View file

@ -1363,3 +1363,48 @@ fixture | fixture: page
fixture | fixture: context fixture | fixture: context
`); `);
}); });
test('calls from page.route callback should be under its parent step', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/33186' }
}, async ({ runInlineTest, server }) => {
const result = await runInlineTest({
'reporter.ts': stepIndentReporter,
'playwright.config.ts': `module.exports = { reporter: './reporter' };`,
'a.test.ts': `
import { test, expect } from '@playwright/test';
test('waitForResponse step nesting', async ({ page }) => {
await test.step('custom step', async () => {
await page.route('**/empty.html', async route => {
const response = await route.fetch();
const text = await response.text();
expect(text).toBe('');
await route.fulfill({ response })
});
await page.goto('${server.EMPTY_PAGE}');
});
});
`
}, { reporter: '', workers: 1, timeout: 3000 });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(0);
expect(result.output).not.toContain('Internal error');
expect(stripAnsi(result.output)).toBe(`
hook |Before Hooks
fixture | fixture: browser
pw:api | browserType.launch
fixture | fixture: context
pw:api | browser.newContext
fixture | fixture: page
pw:api | browserContext.newPage
test.step |custom step @ a.test.ts:4
pw:api | page.route @ a.test.ts:5
pw:api | page.goto(http://localhost:8907/empty.html) @ a.test.ts:11
pw:api | apiResponse.text @ a.test.ts:7
expect | expect.toBe @ a.test.ts:8
hook |After Hooks
fixture | fixture: page
fixture | fixture: context
`);
});