fix: shut down workers before reporter.onEnd (#30329)

This commit is contained in:
Yury Semikhatsky 2024-04-16 09:49:11 -07:00 committed by GitHub
parent 58187075c8
commit 3cea17abb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 5 deletions

View File

@ -148,7 +148,7 @@ export default defineConfig({
* since: v1.10
- type: ?<[int]>
Maximum time in milliseconds the whole test suite can run. Zero timeout (default) disables this behavior. Useful on CI to prevent broken setup from running too long and wasting resources. Learn more about [various timeouts](../test-timeouts.md).
Maximum time in milliseconds the whole test suite can run. Zero timeout (default) disables this behavior. Useful on CI to prevent broken setup from running too long and wasting resources. Note that even if global timeout is reached, Playwright will still allow up to 30 extra seconds for teardown hooks to finish. Learn more about [various timeouts](../test-timeouts.md).
**Usage**

View File

@ -105,9 +105,14 @@ export class TaskRunner<Context> {
status = 'failed';
}
cancelPromise?.resolve();
// Note that upon hitting deadline, we "run cleanup", but it exits immediately
// because of the same deadline. Essentially, we're not performing any cleanup.
const cleanup = () => teardownRunner.runDeferCleanup(context, deadline).then(r => r.status);
const cleanup = async () => {
// Upon hitting deadline we add extra 30s to actually perform cleanup, otherwise
// the task exits immediately because of the same deadline and we may continue
// while the test workers are still running.
const extraTime = timeoutWatcher.timedOut() ? 30_000 : 0;
const { status } = await teardownRunner.runDeferCleanup(context, deadline + extraTime);
return status;
};
return { status, cleanup };
}
}

View File

@ -1060,7 +1060,8 @@ interface TestConfig<TestArgs = {}, WorkerArgs = {}> {
/**
* Maximum time in milliseconds the whole test suite can run. Zero timeout (default) disables this behavior. Useful on
* CI to prevent broken setup from running too long and wasting resources. Learn more about
* CI to prevent broken setup from running too long and wasting resources. Note that even if global timeout is
* reached, Playwright will still allow up to 30 extra seconds for teardown hooks to finish. Learn more about
* [various timeouts](https://playwright.dev/docs/test-timeouts).
*
* **Usage**

View File

@ -774,3 +774,47 @@ test('unhandled exception in test.fail should restart worker and continue', asyn
expect(result.failed).toBe(0);
expect(result.outputLines).toEqual(['bad running worker=0', 'good running worker=1']);
});
test('wait for workers to finish before reporter.onEnd', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
export default {
globalTimeout: 2000,
fullyParallel: true,
reporter: './reporter'
}
`,
'reporter.ts': `
export default class MyReporter {
onTestEnd(test) {
console.log('MyReporter.onTestEnd', test.title);
}
onEnd(status) {
console.log('MyReporter.onEnd');
}
async onExit() {
console.log('MyReporter.onExit');
}
}
`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('first', async ({ }) => {
await new Promise(() => {});
});
test('second', async ({ }) => {
expect(1).toBe(2);
});
`,
}, { workers: 2 });
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(0);
const endIndex = result.output.indexOf('MyReporter.onEnd');
expect(endIndex).not.toBe(-1);
const firstIndex = result.output.indexOf('MyReporter.onTestEnd first');
expect(firstIndex).not.toBe(-1);
expect(firstIndex).toBeLessThan(endIndex);
const secondIndex = result.output.indexOf('MyReporter.onTestEnd second');
expect(secondIndex).not.toBe(-1);
expect(secondIndex).toBeLessThan(endIndex);
});