Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 29 additions & 3 deletions packages/playwright/src/reporters/perfetto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,52 @@ class PerfettoReporter implements ReporterV2 {
// Writes into a ".gz" file through a gzip stream, into a plain file otherwise.
class ChunkWriter {
private _stream: Writable;
private _fileStream: Writable;
private _closed: Promise<void>;
private _error: Error | undefined;

constructor(file: string) {
const fileStream = fs.createWriteStream(file);
this._fileStream = fileStream;
const gzip = file.endsWith('.gz') ? zlib.createGzip() : undefined;
gzip?.pipe(fileStream);
this._stream = gzip ?? fileStream;
// The file is only complete once the destination closes, which is later than
// the gzip stream ending.
this._closed = new Promise(resolve => fileStream.on('close', resolve));
this._closed = new Promise(resolve => {
fileStream.on('close', resolve);
fileStream.on('error', error => {
this._error ??= error;
resolve();
});
});
for (const stream of new Set<Writable>([this._stream, fileStream]))
stream.on('error', error => this._error ??= error);
}

async write(chunk: string) {
if (this._error)
throw this._error;
if (!this._stream.write(chunk))
await new Promise<void>(resolve => this._stream.once('drain', () => resolve()));
if (!this._stream.write(chunk)) {
await new Promise<void>((resolve, reject) => {
if (this._error)
return reject(this._error);
const onDrain = () => finish(resolve);
const onError = (error: Error) => {
this._error ??= error;
finish(() => reject(error));
};
const finish = (done: () => void) => {
this._stream.off('drain', onDrain);
this._stream.off('error', onError);
this._fileStream.off('error', onError);
done();
};
this._stream.once('drain', onDrain);
this._stream.once('error', onError);
Comment thread
SebTardif marked this conversation as resolved.
Resolved
this._fileStream.once('error', onError);
});
}
}

async close() {
Expand Down
18 changes: 18 additions & 0 deletions tests/playwright-test/reporter-perfetto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ test('should report step params', async ({ runInlineTest }, testInfo) => {
expect(findSlice(events, 'my step')!.args.params).toEqual({ foo: 'bar', count: 7 });
});

test('should fail when gzip perfetto output stream errors', async ({ runInlineTest }, testInfo) => {
test.skip(!fs.existsSync('/dev/full'), 'needs /dev/full');
const gz = testInfo.outputPath('trace.json.gz');
await fs.promises.symlink('/dev/full', gz);
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { reporter: [['perfetto', { outputFile: ${JSON.stringify(gz)} }]] };
`,
'a.test.ts': `
import { test } from '@playwright/test';
test('one', async () => {
test.info().annotations.push({ type: 'blob', description: ${JSON.stringify('x'.repeat(64 * 1024))} });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still passes without the fix because the repeated x compresses too well to reproduce the hang

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Rousso (@dcrousso)

this still passes without the fix because the repeated x compresses too well to reproduce the hang

The test now writes incompressible data to a fifo and closes the reader so the dest stream errors. Without the dest listener the inner run exits 0. With it, exit is non-zero. f98bd0941

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Rousso (@dcrousso)

this still passes without the fix because the repeated x compresses too well to reproduce the hang

The test now writes a 256KB incompressible annotation to a directory named trace.json or trace.json.gz. On main the inner run exits 0. With the once(drain) wait it is non-zero. No fifo.

});
`,
});
expect(result.exitCode).not.toBe(0);
});

test('should respect outputFile option', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'playwright.config.ts': `
Expand Down
Loading