File: 06_events.t

package info (click to toggle)
libmojo-ioloop-readwriteprocess-perl 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 540 kB
  • sloc: perl: 4,655; sh: 101; makefile: 2
file content (165 lines) | stat: -rw-r--r-- 4,982 bytes parent folder | download | duplicates (4)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl

use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Mojo::File qw(tempfile path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");
use Mojo::IOLoop::ReadWriteProcess              qw(process);
use Mojo::IOLoop::ReadWriteProcess::Session     qw(session);
use Mojo::IOLoop::ReadWriteProcess::Test::Utils qw(attempt check_bin);

subtest SIG_CHLD => sub {
  my $test_script = check_bin("$FindBin::Bin/data/process_check.sh");
  my $reached;
  my $collect = 0;

  my $p = process(sub { print "Hello\n" });
  $p->session->collect_status(0);
  $p->on(collect_status => sub { $collect++ });
  $p->session->on(
    SIG_CHLD => sub {
      my $self = shift;
      $reached++;
      waitpid $p->pid, 0;
    });

  $p->start;
  attempt {
    attempts  => 20,
    condition => sub { defined $reached && $reached == 1 },
    cb        => sub { $p->signal(POSIX::SIGTERM); sleep 1; }
  };

  is $reached, 1, 'SIG_CHLD fired';
  is $collect, 0, 'collect_status not fired';

  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all_orphans->size, 0);
  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all->size,         1);

  session->reset;
  my $p2 = process(execute => $test_script);
  $p2->session->collect_status(1);

  $reached = 0;

  $p2->on(
    SIG_CHLD => sub {
      my $self = shift;
      $reached++;
    });

  $p2->start;

  attempt {
    attempts  => 20,
    condition => sub { defined $reached && $reached == 1 },
    cb        => sub { $p2->signal(POSIX::SIGTERM); sleep 1; }
  };

  is $reached, 1, 'SIG_CHLD fired';
  ok defined($p2->exit_status), 'SIG_CHLD fired';

  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all_orphans->size, 0);
  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all->size,         1);
};

subtest collect_status => sub {
  session->reset;

  my $sigcld;
  my $p = process(sub { print "Hello\n" });
  $p->session->collect_status(0);
  $p->session->on(
    SIG_CHLD => sub {
      $sigcld++;
      waitpid $p->pid, 0;
    });
  $p->start;

  attempt {
    attempts  => 10,
    condition => sub { defined $sigcld && $sigcld == 1 },
    cb        => sub { $p->signal(POSIX::SIGTERM); sleep 1 }
  };

  is $sigcld, 1, 'SIG_CHLD fired';

  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all_orphans->size, 0);
  is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all->size,         1);
};

subtest collect_from_signal_handler => sub {
  check_bin('/bin/true');
  my $p         = process(execute => '/bin/true');
  my $collected = 0;
  my $orphan    = 0;
  my $sig_chld  = 0;
  $p->session->reset();
  $p->session->collect_status(1);    # needed, because previous test set it to 0
  $p->session->on(SIG_CHLD         => sub { $sig_chld++ });
  $p->session->on(collected        => sub { $collected++ });
  $p->session->on(collected_orphan => sub { $orphan++ });
  $p->start();

  attempt {
    attempts  => 10,
    condition => sub { $sig_chld > 0 && $collected > 0 },
  };

  is($sig_chld,  1, "Event for SIG_CHILD was emitted");
  is($collected, 1, "Event collected apear without doing active wait()");
  is($orphan,    0, "No orphans where collected");

  $p->wait_stop();
  is($collected,      1, "No more collect events emitted");
  is($orphan,         0, "No more orphans events emitted");
  is($p->exit_status, 0, '/bin/true exited with 0');

  exec('/bin/true') if (fork() == 0);

  attempt {attempts => 10, condition => sub { $sig_chld > 1 && $orphan > 0 },};

  is($sig_chld,  2, "Event for SIG_CHILD was emitted");
  is($collected, 1, "No more collect events emitted (2)");
  is($orphan,    1, "Collect one orphan");
};

subtest emit_from_sigchld_off => sub {
  check_bin('/bin/true');
  my $p         = process(execute => '/bin/true');
  my $collected = 0;
  my $orphan    = 0;
  my $sig_chld  = 0;
  $p->session->reset();
  $p->session->collect_status(1);
  $p->session->emit_from_sigchld(0);
  $p->session->on(SIG_CHLD         => sub { $sig_chld++ });
  $p->session->on(collected        => sub { $collected++ });
  $p->session->on(collected_orphan => sub { $orphan++ });
  $p->start();

  attempt {attempts => 10, condition => sub { $sig_chld > 0 },};
  is($sig_chld,  1, "Event for SIG_CHILD was emitted");
  is($collected, 0, "Event collected didn't appear from sighandler");
  is($orphan,    0, "No orphans where collected");

  $p->wait_stop();
  is($collected,      1, "No more collect events emitted");
  is($orphan,         0, "No more orphans events emitted");
  is($p->exit_status, 0, '/bin/true exited with 0');

  exec('/bin/true') if (fork() == 0);
  attempt {attempts => 10, condition => sub { $sig_chld > 1 },};
  is($collected, 1, "No more collect events emitted (2)");
  is($orphan,    0, "collect_orphan didn't appear from sighandler");

  $p->session->consume_collected_info();
  is($sig_chld,  2, "Event for SIG_CHILD was emitted");
  is($collected, 1, "No more collect events emitted (3)");
  is($orphan,    1, "Collect one orphan");
};

done_testing();