1#!/usr/bin/perl
2
3=pod
4
5=head1 NAME
6
7timeout.t - Test suite for IPC::Run timeouts
8
9=cut
10
11use strict;
12
13BEGIN {
14    $|  = 1;
15    $^W = 1;
16}
17
18## Separate from run.t so run.t is not too slow.
19use Test::More;
20use IPC::Run qw( harness timeout );
21
22plan skip_all => 'Skipping on Win32' if $ENV{GITHUB_WINDOWS_TESTING};
23plan tests => 26;
24
25my $h;
26my $t;
27my $in;
28my $out;
29my $started;
30
31$h = harness( [$^X], \$in, \$out, $t = timeout(1) );
32ok( $h->isa('IPC::Run') );
33ok( !!$t->is_reset );
34ok( !$t->is_running );
35ok( !$t->is_expired );
36$started = time;
37$h->start;
38ok(1);
39ok( !$t->is_reset );
40ok( !!$t->is_running );
41ok( !$t->is_expired );
42$in = '';
43eval { $h->pump };
44
45# Older perls' Test.pms don't know what to do with qr//s
46$@ =~ /IPC::Run: timeout/ ? ok(1) : is( $@, qr/IPC::Run: timeout/ );
47
48SCOPE: {
49    my $elapsed = time - $started;
50    $elapsed >= 1 ? ok(1) : is( $elapsed, ">= 1" );
51    is( $t->interval, 1 );
52    ok( !$t->is_reset );
53    ok( !$t->is_running );
54    ok( !!$t->is_expired );
55
56    ##
57    ## Starting from an expired state
58    ##
59    $started = time;
60    $h->start;
61    ok(1);
62    ok( !$t->is_reset );
63    ok( !!$t->is_running );
64    ok( !$t->is_expired );
65    $in = '';
66    eval { $h->pump };
67    $@ =~ /IPC::Run: timeout/ ? ok(1) : is( $@, qr/IPC::Run: timeout/ );
68    ok( !$t->is_reset );
69    ok( !$t->is_running );
70    ok( !!$t->is_expired );
71}
72
73SCOPE: {
74    my $elapsed = time - $started;
75    $elapsed >= 1 ? ok(1) : is( $elapsed, ">= 1" );
76    $h = harness( [$^X], \$in, \$out, timeout(1) );
77    $started = time;
78    $h->start;
79    $in = '';
80    eval { $h->pump };
81    $@ =~ /IPC::Run: timeout/ ? ok(1) : is( $@, qr/IPC::Run: timeout/ );
82}
83
84SCOPE: {
85    my $elapsed = time - $started;
86    $elapsed >= 1 ? ok(1) : is( $elapsed, ">= 1" );
87}
88
89{
90    $h = harness( [ $^X, '-e', 'sleep 1' ], timeout(10), debug => 0 );
91    my $started_at = time;
92    $h->start;
93    $h->finish;
94    my $finished_at = time;
95    ok( $finished_at - $started_at <= 2, 'not too slow to reap' )
96      or diag( $finished_at - $started_at . " seconds passed" );
97}
98