summaryrefslogtreecommitdiff
path: root/t/05-timed-process.t
blob: f09482fd0775c9740fb1e0b0f6e5a6669c660bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use Test;
BEGIN { plan tests => 9; }
use Proc::Background qw(timeout_system);

# Find the lib directory.
my $lib;
foreach my $l (qw(lib ../lib)) {
  if (-d $l) {
    $lib = $l;
    last;
  }
}
$lib or die "Cannot find lib directory.\n";

# Find the sleep_exit.pl and timed-process scripts.  The sleep_exit.pl
# script takes a sleep time and an exit value.  timed-process takes a
# sleep time and a command to run.
my $sleep_exit;
my $timed_process;
foreach my $dir (qw(. ./bin ./t ../bin ../t Proc-Background/t /usr/bin)) {
  unless ($sleep_exit) {
    my $s = "$dir/sleep_exit.pl";
    $sleep_exit = $s if -r $s;
  }
  unless ($timed_process) {
    my $t = "$dir/timed-process";
    $timed_process = $t if -r $t;
  }
}
$sleep_exit or die "Cannot find sleep_exit.pl.\n";
$timed_process or die "Cannot find timed-process.\n";
my @sleep_exit    = ($^X, '-w', $sleep_exit);
my @timed_process = ($^X, '-w', "-I$lib", $timed_process);
my $sleep_exit_cmdline= join ' ', map { $_ =~ /\S/? qq{"$_"} : $_ } @sleep_exit;

sub System {
  my $result = system(@_);
  return ($? >> 8, $? & 127, $? & 128);
}

# Test the timed-process script.  First test a normal exit.
my @t_args = ($^X, '-w', "-I$lib", $timed_process);
my @result = System(@t_args, '-e', 153, 3, "$sleep_exit_cmdline 0 237");
ok($result[0], 237);
ok($result[1],   0);
ok($result[2],   0);

@result = System(@t_args, 1, "$sleep_exit_cmdline 10 27");
ok($result[0], 255);
ok($result[1],   0);
ok($result[2],   0);

@result = System(@t_args, '-e', 153, 1, "$sleep_exit_cmdline 10 27");
ok($result[0], 153);
ok($result[1],   0);
ok($result[2],   0);