2323var common = require ( '../common' ) ;
2424var assert = require ( 'assert' ) ;
2525
26- var pass ;
27- var wait = setInterval ( function ( ) { } , 1000 ) ;
28-
29- process . on ( 'exit' , function ( code ) {
30- if ( code === 0 ) {
31- assert . ok ( pass ) ;
32- }
33- } ) ;
34-
3526// test variants of pid
3627//
3728// null: TypeError
@@ -43,58 +34,55 @@ process.on('exit', function(code) {
4334//
4435// Nan, Infinity, -Infinity: TypeError
4536//
46- // 0: our group process
37+ // 0, String(0) : our group process
4738//
48- // process.pid: ourself
39+ // process.pid, String(process.pid) : ourself
4940
5041assert . throws ( function ( ) { process . kill ( 'SIGTERM' ) ; } , TypeError ) ;
51- assert . throws ( function ( ) { process . kill ( String ( process . pid ) ) ; } , TypeError ) ;
5242assert . throws ( function ( ) { process . kill ( null ) ; } , TypeError ) ;
5343assert . throws ( function ( ) { process . kill ( undefined ) ; } , TypeError ) ;
5444assert . throws ( function ( ) { process . kill ( + 'not a number' ) ; } , TypeError ) ;
5545assert . throws ( function ( ) { process . kill ( 1 / 0 ) ; } , TypeError ) ;
5646assert . throws ( function ( ) { process . kill ( - 1 / 0 ) ; } , TypeError ) ;
5747
58- /* Sending SIGHUP is not supported on Windows */
59- if ( process . platform === 'win32' ) {
60- pass = true ;
61- clearInterval ( wait ) ;
62- return ;
63- }
48+ // Test kill argument processing in valid cases.
49+ //
50+ // Monkey patch _kill so that we don't actually send any signals, particularly
51+ // that we don't kill our process group, or try to actually send ANY signals on
52+ // windows, which doesn't support them.
53+ function kill ( tryPid , trySig , expectPid , expectSig ) {
54+ var getPid ;
55+ var getSig ;
56+ var origKill = process . _kill ;
57+ process . _kill = function ( pid , sig ) {
58+ getPid = pid ;
59+ getSig = sig ;
6460
65- process . once ( 'SIGHUP' , function ( ) {
66- process . once ( 'SIGHUP' , function ( ) {
67- pass = true ;
68- clearInterval ( wait ) ;
69- } ) ;
70- process . kill ( process . pid , 'SIGHUP' ) ;
71- } ) ;
61+ // un-monkey patch process._kill
62+ process . _kill = origKill ;
63+ } ;
7264
65+ process . kill ( tryPid , trySig ) ;
7366
74- /*
75- * Monkey patch _kill so that, in the specific case
76- * when we want to test sending a signal to pid 0,
77- * we don't actually send the signal to the process group.
78- * Otherwise, it could cause a lot of trouble, like terminating
79- * the test runner, or any other process that belongs to the
80- * same process group as this test.
81- */
82- var origKill = process . _kill ;
83- process . _kill = function ( pid , sig ) {
84- /*
85- * make sure we patch _kill only when
86- * we want to test sending a signal
87- * to the process group.
88- */
89- assert . strictEqual ( pid , 0 ) ;
67+ assert . equal ( getPid , expectPid ) ;
68+ assert . equal ( getSig , expectSig ) ;
69+ }
9070
91- // make sure that _kill is passed the correct args types
92- assert ( typeof pid === 'number' ) ;
93- assert ( typeof sig === 'number' ) ;
71+ // Note that SIGHUP and SIGTERM map to 1 and 15 respectively, even on Windows
72+ // (for Windows, libuv maps 1 and 15 to the correct behaviour).
9473
95- // un-monkey patch process._kill
96- process . _kill = origKill ;
97- process . _kill ( process . pid , sig ) ;
98- }
74+ kill ( 0 , 'SIGHUP' , 0 , 1 ) ;
75+ kill ( 0 , undefined , 0 , 15 ) ;
76+ kill ( '0' , 'SIGHUP' , 0 , 1 ) ;
77+ kill ( '0' , undefined , 0 , 15 ) ;
78+
79+ // negative numbers are meaningful on unix
80+ kill ( - 1 , 'SIGHUP' , - 1 , 1 ) ;
81+ kill ( - 1 , undefined , - 1 , 15 ) ;
82+ kill ( '-1' , 'SIGHUP' , - 1 , 1 ) ;
83+ kill ( '-1' , undefined , - 1 , 15 ) ;
9984
100- process . kill ( 0 , 'SIGHUP' ) ;
85+ kill ( process . pid , 'SIGHUP' , process . pid , 1 ) ;
86+ kill ( process . pid , undefined , process . pid , 15 ) ;
87+ kill ( String ( process . pid ) , 'SIGHUP' , process . pid , 1 ) ;
88+ kill ( String ( process . pid ) , undefined , process . pid , 15 ) ;
0 commit comments