| 
									
										
										
										
											2021-04-01 19:13:08 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2019 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { playwrightTest as test, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2021-04-01 19:13:08 -07:00
										 |  |  | import { execSync } from 'child_process'; | 
					
						
							| 
									
										
										
										
											2022-04-07 19:20:54 -07:00
										 |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2023-05-17 01:13:09 +02:00
										 |  |  | import os from 'os'; | 
					
						
							| 
									
										
										
										
											2021-04-01 19:13:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 09:51:09 -07:00
										 |  |  | test.slow(); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:25:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | test('should close the browser when the node process closes', async ({ startRemoteServer, isWindows, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |   const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2022-10-31 12:45:18 -07:00
										 |  |  |   try { | 
					
						
							|  |  |  |     if (isWindows) | 
					
						
							|  |  |  |       execSync(`taskkill /pid ${remoteServer.child().pid} /T /F`, { stdio: 'ignore' }); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       process.kill(remoteServer.child().pid); | 
					
						
							|  |  |  |   } catch (error) { | 
					
						
							|  |  |  |     console.log(error); | 
					
						
							|  |  |  |     if (error.stdout) | 
					
						
							|  |  |  |       console.log('--- stdout ---\n', error.stdout); | 
					
						
							|  |  |  |     if (error.stderr) | 
					
						
							|  |  |  |       console.log('--- stderr ---\n', error.stderr); | 
					
						
							|  |  |  |     throw error; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-04-01 19:13:08 -07:00
										 |  |  |   // We might not get browser exitCode in time when killing the parent node process,
 | 
					
						
							|  |  |  |   // so we don't check it here.
 | 
					
						
							|  |  |  |   expect(await remoteServer.childExitCode()).toBe(isWindows ? 1 : 0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-24 15:24:29 -07:00
										 |  |  | test('should remove temp dir on process.exit', async ({ startRemoteServer, server, platform }, testInfo) => { | 
					
						
							|  |  |  |   test.skip(platform === 'win32', 'Removing user data dir synchronously is blocked on Windows'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-14 16:07:03 -07:00
										 |  |  |   const file = testInfo.outputPath('exit.file'); | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |   const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE, exitOnFile: file }); | 
					
						
							| 
									
										
										
										
											2022-04-14 16:07:03 -07:00
										 |  |  |   const tempDir = await remoteServer.out('tempDir'); | 
					
						
							|  |  |  |   const before = fs.existsSync(tempDir); | 
					
						
							|  |  |  |   fs.writeFileSync(file, 'data', 'utf-8'); | 
					
						
							|  |  |  |   expect(await remoteServer.childExitCode()).toBe(42); | 
					
						
							|  |  |  |   const after = fs.existsSync(tempDir); | 
					
						
							|  |  |  |   expect(before).toBe(true); | 
					
						
							|  |  |  |   expect(after).toBe(false); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  | test.describe('signals', () => { | 
					
						
							| 
									
										
										
										
											2022-04-12 12:28:33 -07:00
										 |  |  |   test.skip(({ platform }) => platform === 'win32'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 01:13:09 +02:00
										 |  |  |   test('should report browser close signal 2', async ({ startRemoteServer, server, isMac, browserName }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     const pid = await remoteServer.out('pid'); | 
					
						
							|  |  |  |     process.kill(-pid, 'SIGKILL'); | 
					
						
							| 
									
										
										
										
											2023-12-15 17:39:31 -08:00
										 |  |  |     if (isMac && browserName === 'webkit' && parseInt(os.release(), 10) > 22 && os.arch() === 'arm64') { | 
					
						
							| 
									
										
										
										
											2024-05-06 15:39:31 -07:00
										 |  |  |       // WebKit on newer macOS exits sometimes with exit code, sometimes with signal.
 | 
					
						
							|  |  |  |       expect('exitCode:' + await remoteServer.out('exitCode') + | 
					
						
							|  |  |  |              'signal:' + await remoteServer.out('signal')).toMatch(/exitCode:137|signal:SIGKILL/); | 
					
						
							| 
									
										
										
										
											2023-07-27 13:36:43 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       expect(await remoteServer.out('exitCode')).toBe('null'); | 
					
						
							|  |  |  |       expect(await remoteServer.out('signal')).toBe('SIGKILL'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid); | 
					
						
							|  |  |  |     await remoteServer.childExitCode(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should close the browser on SIGINT', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGINT'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('0'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(130); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should close the browser on SIGTERM', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGTERM'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('0'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(0); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   test('should close the browser on SIGHUP', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGHUP'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('0'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(0); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:36:43 -07:00
										 |  |  |   test('should kill the browser on double SIGINT and remove temp dir', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2022-04-07 19:20:54 -07:00
										 |  |  |     const tempDir = await remoteServer.out('tempDir'); | 
					
						
							|  |  |  |     const before = fs.existsSync(tempDir); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGINT'); | 
					
						
							|  |  |  |     await remoteServer.out('stalled'); | 
					
						
							|  |  |  |     process.kill(remoteServer.child().pid, 'SIGINT'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('SIGKILL'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(130); | 
					
						
							| 
									
										
										
										
											2022-04-07 19:20:54 -07:00
										 |  |  |     const after = fs.existsSync(tempDir); | 
					
						
							|  |  |  |     expect(before).toBe(true); | 
					
						
							|  |  |  |     expect(after).toBe(false); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:36:43 -07:00
										 |  |  |   test('should kill the browser on SIGINT + SIGTERM', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGINT'); | 
					
						
							|  |  |  |     await remoteServer.out('stalled'); | 
					
						
							|  |  |  |     process.kill(remoteServer.child().pid, 'SIGTERM'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('SIGKILL'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(0); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:36:43 -07:00
										 |  |  |   test('should kill the browser on SIGTERM + SIGINT', async ({ startRemoteServer, server }) => { | 
					
						
							| 
									
										
										
										
											2022-12-07 14:36:32 -08:00
										 |  |  |     const remoteServer = await startRemoteServer('launchServer', { stallOnClose: true, url: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2021-04-02 11:19:26 -07:00
										 |  |  |     process.kill(remoteServer.child().pid, 'SIGTERM'); | 
					
						
							|  |  |  |     await remoteServer.out('stalled'); | 
					
						
							|  |  |  |     process.kill(remoteServer.child().pid, 'SIGINT'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('exitCode')).toBe('null'); | 
					
						
							|  |  |  |     expect(await remoteServer.out('signal')).toBe('SIGKILL'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(130); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2024-01-16 14:41:26 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('should not prevent default SIGTERM handling after browser close', async ({ startRemoteServer, server, platform }, testInfo) => { | 
					
						
							|  |  |  |     const remoteServer = await startRemoteServer('launchServer', { startStopAndRunHttp: true }); | 
					
						
							|  |  |  |     expect(await remoteServer.out('closed')).toBe('success'); | 
					
						
							|  |  |  |     process.kill(remoteServer.child().pid, 'SIGTERM'); | 
					
						
							|  |  |  |     expect(await remoteServer.childExitCode()).toBe(null); | 
					
						
							|  |  |  |     expect(await remoteServer.childSignal()).toBe('SIGTERM'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-04-01 19:13:08 -07:00
										 |  |  | }); |