use strict; use warnings; use Proc::Simple; use Test::More tests => 19; use FindBin; chdir "$FindBin::Bin/../t"; my $TestCmd = "../bin/proc-harness.pl"; my $proc = Proc::Simple->new; # when a process is not running, start it test_restart($proc, '01.conf', '01.sh', 3, 3, 'get killed and start every 3 sec'); # when a process's log in not increasing, restart it test_restart($proc, '02.conf', '02.sh', 3, 4, 'no log writed and restart every 3 sec'); # when proc-harness down(kill -9), restart proc-harness will restart all processes $proc->start("$^X $TestCmd -f 03.conf"); $proc->kill_on_destroy(1); sleep 2; my $ps_cmd = "ps auxwww|grep 03.sh|grep -v grep|awk '{print \$2}'|sort"; my $prev = `$ps_cmd` || ''; my @prev_ps = split /\n/, $prev; is scalar(@prev_ps), 3, 'start 3 processes'; $proc->kill('SIGKILL'); my $out = `$ps_cmd` || ''; my @ps = split /\n/, $out; is scalar(@ps), 3, 'remain 3 process after kill -9 proc-harness'; $proc->start("$^X $TestCmd -f 03.conf"); $proc->kill_on_destroy(1); sleep 2; $out = `$ps_cmd` || ''; @ps = split /\n/, $out; isnt $out, $prev, 'kill the remain process and start new process when start proc-harness'; is scalar(@ps), 3, 'restart 3 new process'; $proc->kill(); # test the redirect log `echo -n '' > 0.log`; $proc->start("$^X $TestCmd -f 04.conf"); $proc->kill_on_destroy(1); sleep 1; for (1..3) { my $lines = `wc -l 0.log | awk '{print \$1}'`; chomp $lines; is $lines, $_, "test for stderr redirect"; sleep 3; } sleep 1; $proc->kill(); sub test_restart { my ($proc, $conf_file, $grep_cmd, $check_times, $sleep_time, $message) = @_; $proc->start("$^X $TestCmd -f $conf_file"); $proc->kill_on_destroy(1); my $ps_cmd = "ps auxwww|grep $grep_cmd|grep -v grep|awk '{print \$2}'|sort"; sleep 2; my $prev = `$ps_cmd` || ''; my @prev_ps = split /\n/, $prev; for (1..$check_times) { sleep $sleep_time; my $out = `$ps_cmd` || ''; # warn "Out: $out\n"; my @ps = split /\n/, $prev; isnt $out, $prev, $message; is scalar(@ps), scalar(@prev_ps), 'proc count equal'; $prev = $out; @prev_ps = @ps; } $proc->kill(); } unlink glob "*.log"; unlink glob "*.stat"; chdir "$FindBin::Bin/..";