@@ -306,6 +306,110 @@ describe('server', () => {
306306 expect ( await exitCode ( ) ) . to . have . equal ( 15 )
307307 } )
308308
309+ it ( 'given on run_complete with exit event listener (15)' , async ( ) => {
310+ mockProcess ( process )
311+
312+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
313+ resolveExitCode ( exitCode )
314+ } )
315+
316+ // last non-zero exit code will be taken
317+ server . on ( 'exit' , ( done ) => {
318+ setTimeout ( ( ) => done ( 30 ) )
319+ } )
320+ server . on ( 'exit' , ( done ) => {
321+ setTimeout ( ( ) => done ( 15 ) )
322+ } )
323+ server . on ( 'exit' , ( done ) => {
324+ setTimeout ( ( ) => done ( 0 ) )
325+ } )
326+
327+ // Provided run_complete exitCode will be overridden by exit listeners
328+ server . emit ( 'run_complete' , browserCollection , { exitCode : 5 } )
329+
330+ function mockProcess ( process ) {
331+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
332+ }
333+ expect ( await exitCode ( ) ) . to . have . equal ( 15 )
334+ } )
335+
336+ it ( 'given on run_complete with exit event listener (0)' , async ( ) => {
337+ mockProcess ( process )
338+
339+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
340+ resolveExitCode ( exitCode )
341+ } )
342+
343+ // exit listeners can't set exit code back to 0
344+ server . on ( 'exit' , ( done ) => {
345+ setTimeout ( ( ) => done ( 0 ) )
346+ } )
347+
348+ server . emit ( 'run_complete' , browserCollection , { exitCode : 15 } )
349+
350+ function mockProcess ( process ) {
351+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
352+ }
353+ expect ( await exitCode ( ) ) . to . have . equal ( 15 )
354+ } )
355+
356+ it ( '1 on run_complete with exit event listener throws' , async ( ) => {
357+ mockProcess ( process )
358+
359+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
360+ resolveExitCode ( exitCode )
361+ } )
362+
363+ server . on ( 'exit' , ( done ) => {
364+ throw new Error ( 'async error from exit event listener' )
365+ } )
366+
367+ server . emit ( 'run_complete' , browserCollection , { exitCode : 0 } )
368+
369+ function mockProcess ( process ) {
370+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
371+ }
372+ expect ( await exitCode ( ) ) . to . have . equal ( 1 )
373+ } )
374+
375+ it ( '1 on run_complete with exit event listener rejects' , async ( ) => {
376+ mockProcess ( process )
377+
378+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
379+ resolveExitCode ( exitCode )
380+ } )
381+
382+ function onExit ( done ) {
383+ // Need to remove listener to prevent endless loop via unhandledRejection handler
384+ // which again calls disconnectBrowsers to fire the 'exit' event
385+ server . off ( 'exit' , onExit )
386+ return Promise . reject ( new Error ( 'async error from exit event listener' ) )
387+ }
388+ server . on ( 'exit' , onExit )
389+
390+ server . emit ( 'run_complete' , browserCollection , { exitCode : 0 } )
391+
392+ function mockProcess ( process ) {
393+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
394+ }
395+ expect ( await exitCode ( ) ) . to . have . equal ( 1 )
396+ } )
397+
398+ it ( '0 on server stop' , async ( ) => {
399+ mockProcess ( process )
400+
401+ await server . _start ( mockConfig , mockLauncher , null , mockFileList , browserCollection , mockExecutor , ( exitCode ) => {
402+ resolveExitCode ( exitCode )
403+ } )
404+
405+ server . stop ( )
406+
407+ function mockProcess ( process ) {
408+ sinon . stub ( process , 'kill' ) . callsFake ( ( pid , ev ) => process . emit ( ev ) )
409+ }
410+ expect ( await exitCode ( ) ) . to . have . equal ( 0 )
411+ } )
412+
309413 it ( '1 on browser_process_failure (singleRunBrowserNotCaptured)' , async ( ) => {
310414 mockProcess ( process )
311415
0 commit comments