File: 26pid.t

package info (click to toggle)
libio-async-perl 0.64-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,068 kB
  • ctags: 491
  • sloc: perl: 12,530; makefile: 8
file content (89 lines) | stat: -rw-r--r-- 1,946 bytes parent folder | download | duplicates (5)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl

use strict;
use warnings;

use IO::Async::Test;

use Test::More;
use Test::Refcount;

use POSIX qw( SIGTERM );

use IO::Async::PID;

use IO::Async::Loop;

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

testing_loop( $loop );

{
   my $kid = fork;
   defined $kid or die "Cannot fork() - $!";

   if( $kid == 0 ) {
      # child
      exit( 3 );
      # this exists as a zombie for now, but we'll deal with this later
   }

   my $exitcode;
   my $pid = IO::Async::PID->new(
      pid     => $kid,
      on_exit => sub { ( undef, $exitcode ) = @_; }
   );

   ok( defined $pid, '$pid defined' );
   isa_ok( $pid, "IO::Async::PID", '$pid isa IO::Async::PID' );

   is_oneref( $pid, '$pid has refcount 1 initially' );

   is( $pid->pid, $kid, '$pid->pid' );

   is( $pid->notifier_name, "$kid", '$pid->notifier_name' );

   $loop->add( $pid );

   is_refcount( $pid, 2, '$pid has refcount 2 after adding to Loop' );

   # reap zombie
   wait_for { defined $exitcode };

   ok( ($exitcode & 0x7f) == 0, 'WIFEXITED($exitcode) after process exit' );
   is( ($exitcode >> 8), 3,     'WEXITSTATUS($exitcode) after process exit' );
}

SKIP: {
   skip "This OS has no signals", 1 unless IO::Async::OS->HAVE_SIGNALS;

   # We require that SIGTERM perform its default action; i.e. terminate the
   # process. Ensure this definitely happens, in case the test harness has it
   # ignored or handled elsewhere.
   local $SIG{TERM} = "DEFAULT";

   my $kid = fork;
   defined $kid or die "Cannot fork() - $!";

   if( $kid == 0 ) {
      sleep( 10 );
      # Just in case the parent died already and didn't kill us
      exit( 0 );
   }

   my $exitcode;
   my $pid = IO::Async::PID->new(
      pid     => $kid,
      on_exit => sub { ( undef, $exitcode ) = @_; }
   );

   $loop->add( $pid );

   $pid->kill( SIGTERM );

   wait_for { defined $exitcode };

   is( ($exitcode & 0x7f), SIGTERM, 'WTERMSIG($exitcode) after SIGTERM' );
}

done_testing;