From 8c341d14996d36f921fce8c9889a23ee8b666781 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 6 Nov 2024 14:11:48 +0100 Subject: [PATCH] allow disabling --- .../playwright/src/plugins/webServerPlugin.ts | 3 + tests/playwright-test/web-server.spec.ts | 70 ++++++++++++------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/packages/playwright/src/plugins/webServerPlugin.ts b/packages/playwright/src/plugins/webServerPlugin.ts index 5f5b9c8fd9..b1ae5d1cc6 100644 --- a/packages/playwright/src/plugins/webServerPlugin.ts +++ b/packages/playwright/src/plugins/webServerPlugin.ts @@ -108,6 +108,9 @@ export class WebServerPlugin implements TestRunnerPlugin { if (process.platform === 'win32') throw new Error('Graceful shutdown is not supported on Windows'); + if (this._options.shutdownTimeout === 0) + throw new Error('skip graceful shutdown'); + const success = launchedProcess.kill('SIGINT'); if (!success) throw new Error(`SIGINT didn't succeed, fall back to non-graceful shutdown`); diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index 71ae366b84..aa447f9a3f 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -745,36 +745,52 @@ test('should forward stdout when set to "pipe" before server is ready', async ({ expect(result.output).not.toContain('Timed out waiting 3000ms'); }); -test('should gracefully kill server', async ({ interactWithTestRunner }, { workerIndex }) => { +test.describe('graceful shutdown', () => { test.skip(process.platform === 'win32', 'No sending SIGINT on Windows'); - const port = workerIndex * 2 + 10510; + const files = (additionalOptions = {}) => { + const port = test.info().workerIndex * 2 + 10510; + return { + 'web-server.js': ` + process.on('SIGINT', () => { console.log('%%webserver received SIGINT but stubbornly refuses to wind down') }) + const server = require('http').createServer((req, res) => { res.end("ok"); }) + server.listen(process.argv[2], () => { console.log('webserver started'); }); + `, + 'test.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + 'playwright.config.ts': ` + module.exports = { + webServer: { + command: 'node web-server.js ${port}', + port: ${port}, + stdout: 'pipe', + timeout: 3000, + ...${JSON.stringify(additionalOptions)} + }, + }; + `, + }; + }; - const testProcess = await interactWithTestRunner({ - 'web-server.js': ` - process.on('SIGINT', () => { console.log('%%webserver received SIGINT but stubbornly refuses to wind down') }) - const server = require('http').createServer((req, res) => { res.end("ok"); }) - server.listen(process.argv[2], () => { console.log('webserver started'); }); - `, - 'test.spec.ts': ` - import { test, expect } from '@playwright/test'; - test('pass', async ({}) => {}); - `, - 'playwright.config.ts': ` - module.exports = { - webServer: { - command: 'node web-server.js ${port}', - port: ${port}, - stdout: 'pipe', - timeout: 3000, - }, - }; - `, - }, { workers: 1 }); + test('sends SIGINT by default', async ({ interactWithTestRunner }) => { + const testProcess = await interactWithTestRunner(files(), { workers: 1 }); - await testProcess.waitForOutput('webserver started'); - process.kill(testProcess.process.pid!, 'SIGINT'); - await testProcess.exited; + await testProcess.waitForOutput('webserver started'); + process.kill(testProcess.process.pid!, 'SIGINT'); + await testProcess.exited; - expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual(['webserver received SIGINT but stubbornly refuses to wind down']); + expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual(['webserver received SIGINT but stubbornly refuses to wind down']); + }); + + test('can be disabled', async ({ interactWithTestRunner }) => { + const testProcess = await interactWithTestRunner(files({ shutdownTimeout: 0 }), { workers: 1 }); + + await testProcess.waitForOutput('webserver started'); + process.kill(testProcess.process.pid!, 'SIGINT'); + await testProcess.exited; + + expect(testProcess.outputLines({ prefix: '[WebServer] ' })).toEqual([]); + }); });