File: 35loop-openprocess.t

package info (click to toggle)
libio-async-perl 0.805-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: perl: 13,938; makefile: 8
file content (69 lines) | stat: -rw-r--r-- 1,529 bytes parent folder | download | duplicates (2)
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
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

use v5.14;
use warnings;

use IO::Async::Test;

use Test2::V0;

use IO::Async::Loop;
use IO::Async::OS;

plan skip_all => "POSIX fork() is not available" unless IO::Async::OS->HAVE_POSIX_FORK;

my $loop = IO::Async::Loop->new_builtin;

testing_loop( $loop );

my $exitcode;

my $proc = $loop->open_process(
   code => sub { 0 },
   on_finish => sub { ( undef, $exitcode ) = @_; },
);

isa_ok( $proc, [ "IO::Async::Process" ], '$proc from ->open_process isa IO::Async::Process' );

undef $exitcode;
wait_for { defined $exitcode };

ok( ($exitcode & 0x7f) == 0, 'WIFEXITED($exitcode) after sub { 0 }' );
is( ($exitcode >> 8), 0,     'WEXITSTATUS($exitcode) after sub { 0 }' );

ok( dies { $loop->open_process(
         command => [ $^X, "-e", 1 ]
      ) },
   'Missing on_finish fails'
);

ok( dies { $loop->open_process(
         command => [ $^X, "-e", 1 ],
         on_finish => sub {},
         on_exit => sub {},
      ) },
   'on_exit parameter fails'
);

# open_child compatibility wrapper
{
   my $exitpid;
   my $pid = $loop->open_child(
      code => sub { 0 },
      on_finish => sub { ( $exitpid, undef ) = @_; },
   );

   like( $pid, qr/^\d+$/, '$loop->open_child returns a PID-like number' );

   wait_for { defined $exitpid };
   is( $exitpid, $pid, 'on_finish passed the same PID as returned from ->open_child' );

   ok( dies { $loop->open_child(
            command => [ $^X, "-e", 1 ],
            on_finish => "hello"
         ) },
      'on_finish not CODE ref fails'
   );
}

done_testing;