]> Sergey Matveev's repositories - public-inbox.git/blob - t/ipc.t
lei_xsearch: transfer 4 FDs internally, drop IO::FDPass
[public-inbox.git] / t / ipc.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use Fcntl qw(SEEK_SET);
9 require_mods(qw(Storable||Sereal));
10 require_ok 'PublicInbox::IPC';
11 state $once = eval <<'';
12 package PublicInbox::IPC;
13 use strict;
14 sub test_array { qw(test array) }
15 sub test_scalar { 'scalar' }
16 sub test_scalarref { \'scalarref' }
17 sub test_undef { undef }
18 sub test_die { shift; die @_; 'unreachable' }
19 sub test_pid { $$ }
20 sub test_write_each_fd {
21         my ($self, @args) = @_;
22         for my $fd (0..2) {
23                 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
24                 $self->{$fd}->flush;
25         }
26 }
27 1;
28
29 my $ipc = bless {}, 'PublicInbox::IPC';
30 my @t = qw(array scalar scalarref undef);
31 my $test = sub {
32         my $x = shift;
33         my @res;
34         for my $type (@t) {
35                 my $m = "test_$type";
36                 my @ret = $ipc->ipc_do($m);
37                 my @exp = $ipc->$m;
38                 is_deeply(\@ret, \@exp, "wantarray $m $x");
39
40                 $ipc->ipc_do($m);
41
42                 $ipc->ipc_async($m, [], sub { push @res, \@_ }, \$m);
43
44                 my $ret = $ipc->ipc_do($m);
45                 my $exp = $ipc->$m;
46                 is_deeply($ret, $exp, "!wantarray $m $x");
47
48                 is_deeply(\@res, [ [ \$m, \@exp ] ], "async $m $x");
49                 @res = ();
50         }
51         $ipc->ipc_async_wait(-1);
52         is_deeply(\@res, [], 'no leftover results');
53         $ipc->ipc_async('test_die', ['die test'],
54                         sub { push @res, \@_  }, 'die arg');
55         $ipc->ipc_async_wait(1);
56         is(scalar(@res), 1, 'only one result');
57         is(scalar(@{$res[0]}), 2, 'result has 2-element array');
58         is($res[0]->[0], 'die arg', 'got async die arg '.$x);
59         is(ref($res[0]->[1]), 'PublicInbox::IPC::Die',
60                 "exception type $x");
61         {
62                 my $nr = PublicInbox::IPC::PIPE_BUF();
63                 my $count = 0;
64                 my $cb = sub { ++$count };
65                 $ipc->ipc_async('test_undef', [], $cb) for (1..$nr);
66                 $ipc->ipc_async_wait(-1);
67                 is($count, $nr, "$x async runs w/o deadlock");
68         }
69
70         my $ret = eval { $ipc->test_die('phail') };
71         my $exp = $@;
72         $ret = eval { $ipc->ipc_do('test_die', 'phail') };
73         my $err = $@;
74         my %lines;
75         for ($err, $exp) {
76                 s/ line (\d+).*//s and $lines{$1}++;
77         }
78         is(scalar keys %lines, 1, 'line numbers match');
79         is((values %lines)[0], 2, '2 hits on same line number');
80         is($err, $exp, "$x die matches");
81         is($ret, undef, "$x die did not return");
82
83         eval { $ipc->test_die(['arrayref']) };
84         $exp = $@;
85         $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
86         $err = $@;
87         is_deeply($err, $exp, 'die with unblessed ref');
88         is(ref($err), 'ARRAY', 'got an array ref');
89
90         $exp = bless ['blessed'], 'PublicInbox::WTF';
91         $ret = eval { $ipc->ipc_do('test_die', $exp) };
92         $err = $@;
93         is_deeply($err, $exp, 'die with blessed ref');
94         is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
95 };
96 $test->('local');
97
98 {
99         my $pid = $ipc->ipc_worker_spawn('test worker');
100         ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
101         defined($pid) or BAIL_OUT 'no spawn, no test';
102         is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
103         $test->('worker');
104         {
105                 my ($tmp, $for_destroy) = tmpdir();
106                 $ipc->ipc_lock_init("$tmp/lock");
107                 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
108         }
109         $ipc->ipc_worker_stop;
110         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
111 }
112 $ipc->ipc_worker_stop; # idempotent
113
114 # work queues
115 $ipc->wq_set_recv_modes(qw( >&= >&= >&= ));
116 pipe(my ($ra, $wa)) or BAIL_OUT $!;
117 pipe(my ($rb, $wb)) or BAIL_OUT $!;
118 pipe(my ($rc, $wc)) or BAIL_OUT $!;
119 open my $warn, '+>', undef or BAIL_OUT;
120 $warn->autoflush(0);
121 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
122 my @ppids;
123 for my $t ('local', 'worker', 'worker again') {
124         $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
125         my $i = 0;
126         for my $fh ($ra, $rb, $rc) {
127                 my $buf = readline($fh);
128                 is(chop($buf), "\n", "trailing CR ($t)");
129                 like($buf, qr/\Ai=$i \d+ hello world\z/, "got expected ($t)");
130                 $i++;
131         }
132         $ipc->wq_do('test_die', [ $wa, $wb, $wc ]);
133         my $ppid = $ipc->wq_workers_start('wq', 1);
134         push(@ppids, $ppid);
135 }
136
137 # wq_do works across fork (siblings can feed)
138 SKIP: {
139         skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0];
140         is_deeply(\@ppids, [$$, undef, undef],
141                 'parent pid returned in wq_workers_start');
142         my $pid = fork // BAIL_OUT $!;
143         if ($pid == 0) {
144                 use POSIX qw(_exit);
145                 $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
146                 _exit(0);
147         } else {
148                 my $i = 0;
149                 my ($wpid, @rest) = keys %{$ipc->{-wq_workers}};
150                 is(scalar(@rest), 0, 'only one worker');
151                 for my $fh ($ra, $rb, $rc) {
152                         my $buf = readline($fh);
153                         is(chop($buf), "\n", "trailing CR #$i");
154                         like($buf, qr/^i=$i $wpid $pid\z/,
155                                 'got expected from sibling');
156                         $i++;
157                 }
158                 is(waitpid($pid, 0), $pid, 'waitpid complete');
159                 is($?, 0, 'child wq producer exited');
160         }
161 }
162
163 $ipc->wq_close;
164 SKIP: {
165         skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
166         seek($warn, 0, SEEK_SET) or BAIL_OUT;
167         my @warn = <$warn>;
168         is(scalar(@warn), 3, 'warned 3 times');
169         like($warn[0], qr/ wq_do: /, '1st warned from wq_do');
170         like($warn[1], qr/ wq_worker: /, '2nd warned from wq_worker');
171         is($warn[2], $warn[1], 'worker did not die');
172
173         $SIG{__WARN__} = 'DEFAULT';
174         is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
175         is($ipc->wq_workers, 1, '1 worker started');
176         SKIP: {
177                 $ipc->WQ_MAX_WORKERS > 1 or
178                         skip 'Inline::C or Socket::MsgHdr not available', 4;
179                 $ipc->wq_worker_incr;
180                 is($ipc->wq_workers, 2, 'worker count bumped');
181                 $ipc->wq_worker_decr;
182                 $ipc->wq_worker_decr_wait(10);
183                 is($ipc->wq_workers, 1, 'worker count lowered');
184                 is($ipc->wq_workers(2), 2, 'worker count set');
185                 is($ipc->wq_workers, 2, 'worker count stayed set');
186         }
187         $ipc->wq_close;
188         is($ipc->wq_workers, undef, 'workers undef after close');
189 }
190
191 done_testing;