Skip to content

Commit

Permalink
fix: shut down workers before reporter.onEnd (#30329)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Apr 16, 2024
1 parent 5818707 commit 3cea17a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/test-api/class-testconfig.md
Expand Up @@ -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**

Expand Down
11 changes: 8 additions & 3 deletions packages/playwright/src/runner/taskRunner.ts
Expand Up @@ -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 };
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright/types/test.d.ts
Expand Up @@ -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**
Expand Down
44 changes: 44 additions & 0 deletions tests/playwright-test/runner.spec.ts
Expand Up @@ -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);
});

0 comments on commit 3cea17a

Please sign in to comment.