File: 30loop-fork.t

package info (click to toggle)
libio-async-perl 0.802-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,184 kB
  • sloc: perl: 13,932; makefile: 8
file content (109 lines) | stat: -r--r--r-- 2,522 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl

use strict;
use warnings;

use IO::Async::Test;

use Test::More;
use Test::Metrics::Any;

use POSIX qw( SIGINT );

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;
   $loop->fork(
      code    => sub { return 5; },
      on_exit => sub { ( undef, $exitcode ) = @_ },
   );

   wait_for { defined $exitcode };

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

{
   my $exitcode;
   $loop->fork(
      code    => sub { die "error"; },
      on_exit => sub { ( undef, $exitcode ) = @_ },
   );

   wait_for { defined $exitcode };

   ok( ($exitcode & 0x7f) == 0, 'WIFEXITED($exitcode) after child die' );
   is( ($exitcode >> 8), 255, 'WEXITSTATUS($exitcode) after child die' );
}

SKIP: {
   skip "This OS does not have signals", 1 unless IO::Async::OS->HAVE_SIGNALS;

   local $SIG{INT} = sub { exit( 22 ) };

   my $exitcode;
   $loop->fork(
      code    => sub { kill SIGINT, $$ },
      on_exit => sub { ( undef, $exitcode ) = @_ },
   );

   wait_for { defined $exitcode };

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

SKIP: {
   skip "This OS does not have signals", 2 unless IO::Async::OS->HAVE_SIGNALS;

   local $SIG{INT} = sub { exit( 22 ) };

   my $exitcode;
   $loop->fork(
      code    => sub { kill SIGINT, $$ },
      on_exit => sub { ( undef, $exitcode ) = @_ },
      keep_signals => 1,
   );

   wait_for { defined $exitcode };

   ok( ($exitcode & 0x7f) == 0, 'WIFEXITED($exitcode) after child SIGINT with keep_signals' );
   is( ($exitcode >> 8), 22,    'WEXITSTATUS($exitcode) after child SIGINT with keep_signals' );
}

{
   my $exitcode;

   $loop->fork(
      code => sub {
         my $innerloop = IO::Async::Loop->new;
         return 0 if $innerloop != $loop; # success
         return 1;
      },
      on_exit => sub { ( undef, $exitcode ) = @_ },
   );

   wait_for { defined $exitcode };

   ok( $exitcode == 0, 'IO::Async::Loop->new inside forked process code gets new loop instance' );
}

# Metrics
SKIP: {
   skip "Metrics are unavailable" unless $IO::Async::Metrics::METRICS;
   is_metrics_from(
      sub { $loop->fork( code => sub {}, on_exit => sub {} ) },
      { io_async_forks => 1 },
      '$loop->fork increments fork counter'
   );
}

done_testing;