Skip to content

Commit b139ffc

Browse files
committed
cherry-pick(#22126): fix(tracing): avoid clashing network file names
With two contexts in the same test, we can get: - `<testId>.network` and `<testId>-1.network` files; - for export, we can copy `<testId>.network` into `<testId>-1.network` and try to copy into a file when another trace is reading from it. Fixes #22089.
1 parent d59972a commit b139ffc

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

packages/playwright-core/src/server/trace/recorder/tracing.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,12 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
261261
return {};
262262

263263
// Network file survives across chunks, make a snapshot before returning the resulting entries.
264-
const suffix = state.chunkOrdinal ? `-${state.chunkOrdinal}` : ``;
265-
const networkFile = path.join(state.tracesDir, state.traceName + `${suffix}.network`);
264+
// We should pick a name starting with "traceName" and ending with .network.
265+
// Something like <traceName>someSuffixHere.network.
266+
// However, this name must not clash with any other "traceName".network in the same tracesDir.
267+
// We can use <traceName>-<guid>.network, but "-pwnetcopy-0" suffix is more readable
268+
// and makes it easier to debug future issues.
269+
const networkFile = path.join(state.tracesDir, state.traceName + `-pwnetcopy-${state.chunkOrdinal}.network`);
266270
await fs.promises.copyFile(state.networkFile, networkFile);
267271

268272
const entries: NameValue[] = [];

tests/playwright-test/playwright.trace.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ test('should not throw with trace: on-first-retry and two retries in the same wo
132132
expect(result.flaky).toBe(6);
133133
});
134134

135+
test('should not mixup network files between contexts', async ({ runInlineTest, server }, testInfo) => {
136+
// NOTE: this test reproduces the issue 10% of the time. Running with --repeat-each=20 helps.
137+
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/22089' });
138+
139+
const result = await runInlineTest({
140+
'playwright.config.ts': `
141+
export default { use: { trace: 'on' } };
142+
`,
143+
'a.spec.ts': `
144+
import { test, expect } from '@playwright/test';
145+
146+
let page1, page2;
147+
148+
test.beforeAll(async ({ browser }) => {
149+
page1 = await browser.newPage();
150+
await page1.goto("${server.EMPTY_PAGE}");
151+
152+
page2 = await browser.newPage();
153+
await page2.goto("${server.EMPTY_PAGE}");
154+
});
155+
156+
test.afterAll(async () => {
157+
await page1.close();
158+
await page2.close();
159+
});
160+
161+
test('example', async ({ page }) => {
162+
await page.goto("${server.EMPTY_PAGE}");
163+
});
164+
`,
165+
}, { workers: 1, timeout: 15000 });
166+
expect(result.exitCode).toEqual(0);
167+
expect(result.passed).toBe(1);
168+
expect(fs.existsSync(testInfo.outputPath('test-results', 'a-example', 'trace.zip'))).toBe(true);
169+
});
170+
135171
test('should save sources when requested', async ({ runInlineTest }, testInfo) => {
136172
const result = await runInlineTest({
137173
'playwright.config.ts': `

tests/playwright-test/ui-mode-test-run.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ test('should stop', async ({ runUITest }) => {
228228
`,
229229
});
230230

231-
await expect(page.getByTitle('Run all')).toBeEnabled();
231+
await expect(page.getByTitle('Run all')).toBeEnabled({ timeout: 15000 });
232232
await expect(page.getByTitle('Stop')).toBeDisabled();
233233

234234
await page.getByTitle('Run all').click();

0 commit comments

Comments
 (0)