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