@@ -12,6 +12,10 @@ use lib ("$FindBin::Bin/lib", "../lib", "lib");
1212use Mojo::IOLoop::ReadWriteProcess qw( process) ;
1313use Mojo::IOLoop::ReadWriteProcess::Test::Utils qw( attempt check_bin) ;
1414
15+ my $interval = $ENV {MOJO_PROCESS_TEST_SLEEP_INTERVAL } // 0.01;
16+ my $timeout = $ENV {MOJO_PROCESS_SUBTEST_TIMEOUT } // 15;
17+ my $kill_sleeptime = $ENV {TOTAL_SLEEPTIME_DURING_KILL } // ($interval * 5.0);
18+
1519subtest process => sub {
1620
1721 my $c = Mojo::IOLoop::ReadWriteProcess-> new();
@@ -40,8 +44,8 @@ subtest 'process basic functions' => sub {
4044 " Process with no code nor execute command, will fail" ;
4145
4246 $p = Mojo::IOLoop::ReadWriteProcess-> new(
43- kill_sleeptime => 0.01 ,
44- sleeptime_during_kill => 0.01
47+ kill_sleeptime => $interval ,
48+ sleeptime_during_kill => $interval
4549 );
4650 eval { $p -> _fork(); };
4751 ok $@ , " Error expected" ;
@@ -52,8 +56,8 @@ subtest 'process basic functions' => sub {
5256 pipe (PARENT, CHILD);
5357
5458 my $p = Mojo::IOLoop::ReadWriteProcess-> new(
55- kill_sleeptime => 0.01 ,
56- sleeptime_during_kill => 0.01 ,
59+ kill_sleeptime => $interval ,
60+ sleeptime_during_kill => $interval ,
5761 code => sub {
5862 close (PARENT);
5963 open STDERR , " >&" , \*CHILD or die $! ;
@@ -68,13 +72,13 @@ subtest 'process basic functions' => sub {
6872};
6973
7074subtest ' process is_running()' => sub {
71-
7275 my @output ;
7376 pipe (PARENT, CHILD);
7477
75- my $p = Mojo::IOLoop::ReadWriteProcess-> new(
76- kill_sleeptime => 0.01,
77- sleeptime_during_kill => 0.01,
78+ my $patience = $timeout / $interval ;
79+ my $p = Mojo::IOLoop::ReadWriteProcess-> new(
80+ kill_sleeptime => $interval ,
81+ sleeptime_during_kill => $interval ,
7882 code => sub {
7983 close (PARENT);
8084 open STDERR , " >&" , \*CHILD or die $! ;
@@ -85,6 +89,7 @@ subtest 'process is_running()' => sub {
8589 close (CHILD);
8690 @output = scalar <PARENT>;
8791 $p -> stop();
92+ sleep $interval while $p -> is_running && --$patience > 0;
8893
8994 close (PARENT);
9095 chomp @output ;
@@ -101,10 +106,12 @@ subtest 'process is_running()' => sub {
101106 1 while 1;
102107 });
103108 $p -> restart()-> restart()-> restart();
109+ sleep $interval until $p -> is_running || --$patience <= 0;
104110 is $p -> is_running, 1, " Process now is running" ;
105111 close (CHILD);
106112 @output = scalar <PARENT>;
107113 $p -> stop();
114+ sleep $interval while $p -> is_running && --$patience > 0;
108115 chomp @output ;
109116 is $output [0], " FOOBAZFTW" , ' right output from process' ;
110117 is $p -> is_running, 0, " Process now is not running" ;
@@ -113,13 +120,7 @@ subtest 'process is_running()' => sub {
113120 pipe (PARENT, CHILD);
114121 $p -> restart();
115122
116- # Give time to the child to be up
117- my $attempts = 100;
118- until ($p -> is_running || $attempts == 0) {
119- sleep .1;
120- $attempts --;
121- }
122-
123+ sleep $interval until $p -> is_running || --$patience <= 0;
123124 is $p -> is_running, 1, " Process now is running" ;
124125 close (CHILD);
125126 @output = scalar <PARENT>;
@@ -132,7 +133,7 @@ subtest 'process execute()' => sub {
132133 my $test_script = check_bin(" $FindBin::Bin /data/process_check.sh" );
133134 my $test_script_sigtrap = check_bin(" $FindBin::Bin /data/term_trap.sh" );
134135 my $p = Mojo::IOLoop::ReadWriteProcess-> new(
135- sleeptime_during_kill => 0.1 ,
136+ sleeptime_during_kill => $interval ,
136137 execute => $test_script
137138 )-> start();
138139 is $p -> getline, " TEST normal print\n " , ' Get right output from stdout' ;
@@ -145,8 +146,8 @@ subtest 'process execute()' => sub {
145146 is $p -> is_running, 0, ' process is not running anymore' ;
146147
147148 $p = Mojo::IOLoop::ReadWriteProcess-> new(
148- kill_sleeptime => 0.01 ,
149- sleeptime_during_kill => 0.01 ,
149+ kill_sleeptime => $interval ,
150+ sleeptime_during_kill => $interval ,
150151 execute => $test_script ,
151152 args => [
152153 qw( FOO
@@ -164,7 +165,7 @@ subtest 'process execute()' => sub {
164165 is $p -> exit_status, 100, ' able to retrieve function return' ;
165166
166167 $p = Mojo::IOLoop::ReadWriteProcess-> new(
167- sleeptime_during_kill => 0.1 ,
168+ sleeptime_during_kill => $interval ,
168169 execute => $test_script
169170 )-> args([qw( FOO BAZ) ])-> start();
170171 is $p -> stdout, " TEST normal print\n " , ' Get right output from stdout' ;
@@ -178,13 +179,15 @@ subtest 'process execute()' => sub {
178179 is $p -> getline, " FOO BAZ\n " , ' process received extra arguments' ;
179180 is $p -> exit_status, 100, ' able to retrieve function return' ;
180181
182+ my $patience = $timeout / $interval ;
181183 $p = Mojo::IOLoop::ReadWriteProcess-> new(
182- kill_sleeptime => 0.01 ,
183- sleeptime_during_kill => 0.01 ,
184+ kill_sleeptime => $interval ,
185+ sleeptime_during_kill => $interval ,
184186 separate_err => 0,
185187 execute => $test_script
186188 );
187189 $p -> start();
190+ sleep $interval until $p -> is_running || --$patience <= 0;
188191 is $p -> is_running, 1, ' process is still running' ;
189192 is $p -> getline, " TEST error print\n " ,
190193 ' Get STDERR output from stdout, always in getline()' ;
@@ -193,8 +196,8 @@ subtest 'process execute()' => sub {
193196 ' Still able to get stdout output, always in getline()' ;
194197
195198 my $p2 = Mojo::IOLoop::ReadWriteProcess-> new(
196- kill_sleeptime => 0.01 ,
197- sleeptime_during_kill => 0.01 ,
199+ kill_sleeptime => $interval ,
200+ sleeptime_during_kill => $interval ,
198201 separate_err => 0,
199202 execute => $test_script ,
200203 set_pipes => 0
@@ -206,8 +209,8 @@ subtest 'process execute()' => sub {
206209 ' take exit status even with set_pipes = 0 (we killed it)' ;
207210
208211 $p = Mojo::IOLoop::ReadWriteProcess-> new(
209- kill_sleeptime => 0.01 ,
210- sleeptime_during_kill => 0.01 ,
212+ kill_sleeptime => $interval ,
213+ sleeptime_during_kill => $interval ,
211214 verbose => 1,
212215 separate_err => 0,
213216 execute => $test_script_sigtrap ,
@@ -234,8 +237,8 @@ subtest 'process execute()' => sub {
234237
235238
236239 $p = Mojo::IOLoop::ReadWriteProcess-> new(
237- kill_sleeptime => 0.01 ,
238- sleeptime_during_kill => 0.01 ,
240+ kill_sleeptime => $interval ,
241+ sleeptime_during_kill => $interval ,
239242 verbose => 1,
240243 separate_err => 0,
241244 blocking_stop => 1,
@@ -248,8 +251,8 @@ subtest 'process execute()' => sub {
248251
249252 my $pidfile = tempfile;
250253 $p = Mojo::IOLoop::ReadWriteProcess-> new(
251- kill_sleeptime => 0.01 ,
252- sleeptime_during_kill => 0.01 ,
254+ kill_sleeptime => $interval ,
255+ sleeptime_during_kill => $interval ,
253256 verbose => 1,
254257 separate_err => 0,
255258 blocking_stop => 1,
@@ -265,8 +268,8 @@ subtest 'process execute()' => sub {
265268
266269 $pidfile = tempfile;
267270 $p = Mojo::IOLoop::ReadWriteProcess-> new(
268- kill_sleeptime => 0.01 ,
269- sleeptime_during_kill => 0.01 ,
271+ kill_sleeptime => $interval ,
272+ sleeptime_during_kill => $interval ,
270273 verbose => 1,
271274 separate_err => 0,
272275 blocking_stop => 1,
@@ -281,8 +284,8 @@ subtest 'process execute()' => sub {
281284 is -e $pidfile , undef , ' Pidfile got removed after stop()' ;
282285
283286 $p = Mojo::IOLoop::ReadWriteProcess-> new(
284- kill_sleeptime => 0.01 ,
285- sleeptime_during_kill => 0.01 ,
287+ kill_sleeptime => $interval ,
288+ sleeptime_during_kill => $interval ,
286289 verbose => 1,
287290 separate_err => 0,
288291 blocking_stop => 1,
@@ -309,8 +312,8 @@ subtest 'process(execute => /bin/true)' => sub {
309312
310313subtest ' process code()' => sub {
311314 my $p = Mojo::IOLoop::ReadWriteProcess-> new(
312- kill_sleeptime => 0.01 ,
313- sleeptime_during_kill => 0.01 ,
315+ kill_sleeptime => $interval ,
316+ sleeptime_during_kill => $interval ,
314317 code => sub {
315318 my ($self ) = shift ;
316319 my $parent_output = $self -> channel_out;
@@ -338,7 +341,7 @@ subtest 'process code()' => sub {
338341 is $p -> channel_out-> getline, " FOOBARftw\n " , " can read from internal channel" ;
339342 is $p -> channel_read_handle-> getline, " PONG\n " ,
340343 " can read from internal channel" ;
341- $p -> stop() ;
344+ $p -> stop-> wait ;
342345 is $p -> is_running, 0, ' process is not running' ;
343346 $p -> restart();
344347
@@ -354,7 +357,7 @@ subtest 'process code()' => sub {
354357
355358 is $p -> read_all, " Enter something : you entered FOOBAR\n " ,
356359 ' Get right output from stdout' ;
357- $p -> stop() ;
360+ $p -> stop-> wait ;
358361
359362 my @result = $p -> read_all;
360363 is @result , 0, ' output buffer is now empty' ;
@@ -364,8 +367,8 @@ subtest 'process code()' => sub {
364367 is $p -> is_running, 0, ' process is not running' ;
365368
366369 $p = Mojo::IOLoop::ReadWriteProcess-> new(
367- kill_sleeptime => 0.01 ,
368- sleeptime_during_kill => 0.01 ,
370+ kill_sleeptime => $interval ,
371+ sleeptime_during_kill => $interval ,
369372 separate_err => 0,
370373 code => sub {
371374 my ($self ) = shift ;
@@ -382,8 +385,8 @@ subtest 'process code()' => sub {
382385 is $p -> is_running, 0, ' process is not running' ;
383386 is $p -> return_status, 256, ' right return code' ;
384387
385- $p = Mojo::IOLoop::ReadWriteProcess-> new(sub { die " Fatal error" ; },
386- sleeptime_during_kill => 0.1 );
388+ $p = Mojo::IOLoop::ReadWriteProcess-> new(sub { die " Fatal error" },
389+ sleeptime_during_kill => $interval );
387390 my $event_fired = 0;
388391 $p -> on(
389392 process_error => sub {
@@ -401,8 +404,8 @@ subtest 'process code()' => sub {
401404
402405 $p = Mojo::IOLoop::ReadWriteProcess-> new(
403406 sub { return 42 },
404- kill_sleeptime => 0.01 ,
405- sleeptime_during_kill => 0.01 ,
407+ kill_sleeptime => $interval ,
408+ sleeptime_during_kill => $interval ,
406409 internal_pipes => 0
407410 );
408411 $p -> start();
@@ -413,8 +416,8 @@ subtest 'process code()' => sub {
413416
414417 $p = Mojo::IOLoop::ReadWriteProcess-> new(
415418 sub { die " Bah" },
416- kill_sleeptime => 0.01 ,
417- sleeptime_during_kill => 0.01 ,
419+ kill_sleeptime => $interval ,
420+ sleeptime_during_kill => $interval ,
418421 internal_pipes => 0
419422 );
420423 $p -> start();
@@ -425,8 +428,8 @@ subtest 'process code()' => sub {
425428# XXX: flaky test temporarly skip it. is !!$p->exit_status, 1, 'Exit status is there';
426429
427430 $p = Mojo::IOLoop::ReadWriteProcess-> new(
428- kill_sleeptime => 0.01 ,
429- sleeptime_during_kill => 0.01 ,
431+ kill_sleeptime => $interval ,
432+ sleeptime_during_kill => $interval ,
430433 separate_err => 0,
431434 set_pipes => 0,
432435 code => sub {
@@ -440,8 +443,8 @@ subtest 'process code()' => sub {
440443 is $p -> return_status, 256, " grab exit_status even if no pipes are set" ;
441444
442445 $p = Mojo::IOLoop::ReadWriteProcess-> new(
443- kill_sleeptime => 0.01 ,
444- sleeptime_during_kill => 0.01 ,
446+ kill_sleeptime => $interval ,
447+ sleeptime_during_kill => $interval ,
445448 separate_err => 0,
446449 set_pipes => 1,
447450 code => sub {
@@ -451,8 +454,8 @@ subtest 'process code()' => sub {
451454 is $p -> exit_status, 100, " grab exit_status even if no pipes are set" ;
452455
453456 $p = Mojo::IOLoop::ReadWriteProcess-> new(
454- kill_sleeptime => 0.01 ,
455- sleeptime_during_kill => 0.01 ,
457+ kill_sleeptime => $interval ,
458+ sleeptime_during_kill => $interval ,
456459 separate_err => 0,
457460 code => sub {
458461 print STDERR " TEST error print\n " for (1 .. 6);
@@ -482,11 +485,8 @@ subtest stop_whole_process_group_gracefully => sub {
482485 # to check whether the sub processes would actually be granted
483486 # this number of seconds before getting killed. This is not set by
484487 # default to avoid slowing down the CI.
485- my $interval = $ENV {MOJO_PROCESS_STOP_PGROUP_SLEEP_INTERVAL } // 0.01;
486- my $timeout = $ENV {MOJO_PROCESS_STOP_PGROUP_TIMEOUT } // 15;
487- my $kill_sleeptime = $ENV {TOTAL_SLEEPTIME_DURING_KILL } // ($interval * 5.0);
488- my $patience = $timeout / $interval ;
489- my $sub_process = Mojo::IOLoop::ReadWriteProcess-> new(
488+ my $patience = $timeout / $interval ;
489+ my $sub_process = Mojo::IOLoop::ReadWriteProcess-> new(
490490 kill_sleeptime => $interval ,
491491 sleeptime_during_kill => $interval ,
492492 max_kill_attempts => 1,
@@ -534,8 +534,8 @@ subtest process_debug => sub {
534534 eval " no warnings; require Mojo::IOLoop::ReadWriteProcess" ; # # no critic
535535 Mojo::IOLoop::ReadWriteProcess-> new(
536536 code => sub { 1; },
537- kill_sleeptime => 0.01 ,
538- sleeptime_during_kill => 0.01
537+ kill_sleeptime => $interval ,
538+ sleeptime_during_kill => $interval
539539 )-> start()-> stop();
540540 }
541541
@@ -551,8 +551,8 @@ process';
551551 eval " no warnings; require Mojo::IOLoop::ReadWriteProcess" ; # # no critic
552552 Mojo::IOLoop::ReadWriteProcess-> new(
553553 execute => " $FindBin::Bin /data/process_check.sh" ,
554- kill_sleeptime => 0.01 ,
555- sleeptime_during_kill => 0.01 ,
554+ kill_sleeptime => $interval ,
555+ sleeptime_during_kill => $interval ,
556556 )-> start()-> stop();
557557 }
558558
0 commit comments