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>
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;
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' }
22 sub test_write_each_fd {
23 my ($self, @args) = @_;
25 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
30 my ($self, $buf) = @_;
31 print { $self->{1} } sha1_hex($buf), "\n";
36 my $ipc = bless {}, 'PublicInbox::IPC';
37 my @t = qw(array scalar scalarref undef);
43 my @ret = $ipc->ipc_do($m);
45 is_deeply(\@ret, \@exp, "wantarray $m $x");
49 $ipc->ipc_async($m, [], sub { push @res, \@_ }, \$m);
51 my $ret = $ipc->ipc_do($m);
53 is_deeply($ret, $exp, "!wantarray $m $x");
55 is_deeply(\@res, [ [ \$m, \@exp ] ], "async $m $x");
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',
69 my $nr = PublicInbox::IPC::PIPE_BUF();
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");
77 my $ret = eval { $ipc->test_die('phail') };
79 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
83 s/ line (\d+).*//s and $lines{$1}++;
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");
90 eval { $ipc->test_die(['arrayref']) };
92 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
94 is_deeply($err, $exp, 'die with unblessed ref');
95 is(ref($err), 'ARRAY', 'got an array ref');
97 $exp = bless ['blessed'], 'PublicInbox::WTF';
98 $ret = eval { $ipc->ipc_do('test_die', $exp) };
100 is_deeply($err, $exp, 'die with blessed ref');
101 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
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');
112 my ($tmp, $for_destroy) = tmpdir();
113 $ipc->ipc_lock_init("$tmp/lock");
114 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
116 $ipc->ipc_worker_stop;
117 ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
119 $ipc->ipc_worker_stop; # idempotent
122 pipe(my ($ra, $wa)) or BAIL_OUT $!;
123 pipe(my ($rb, $wb)) or BAIL_OUT $!;
124 pipe(my ($rc, $wc)) or BAIL_OUT $!;
125 open my $warn, '+>', undef or BAIL_OUT;
127 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
129 open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!";
130 my $big = do { local $/; <$agpl> } // BAIL_OUT "read: $!";
131 close $agpl or BAIL_OUT "close: $!";
133 for my $t ('local', 'worker', 'worker again') {
134 $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
136 for my $fh ($ra, $rb, $rc) {
137 my $buf = readline($fh);
138 is(chop($buf), "\n", "trailing CR ($t)");
139 like($buf, qr/\Ai=$i \d+ hello world\z/, "got expected ($t)");
142 $ipc->wq_do('test_die', [ $wa, $wb, $wc ]);
143 $ipc->wq_do('test_sha', [ $wa, $wb ], 'hello world');
144 is(readline($rb), sha1_hex('hello world')."\n", "SHA small ($t)");
146 my $bigger = $big x 10;
147 $ipc->wq_do('test_sha', [ $wa, $wb ], $bigger);
148 my $exp = sha1_hex($bigger)."\n";
150 is(readline($rb), $exp, "SHA big ($t)");
152 my $ppid = $ipc->wq_workers_start('wq', 1);
156 # wq_do works across fork (siblings can feed)
158 skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0];
159 is_deeply(\@ppids, [$$, undef, undef],
160 'parent pid returned in wq_workers_start');
161 my $pid = fork // BAIL_OUT $!;
164 $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
168 my ($wpid, @rest) = keys %{$ipc->{-wq_workers}};
169 is(scalar(@rest), 0, 'only one worker');
170 for my $fh ($ra, $rb, $rc) {
171 my $buf = readline($fh);
172 is(chop($buf), "\n", "trailing CR #$i");
173 like($buf, qr/^i=$i $wpid $pid\z/,
174 'got expected from sibling');
177 is(waitpid($pid, 0), $pid, 'waitpid complete');
178 is($?, 0, 'child wq producer exited');
184 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
185 seek($warn, 0, SEEK_SET) or BAIL_OUT;
187 is(scalar(@warn), 3, 'warned 3 times');
188 like($warn[0], qr/ wq_do: /, '1st warned from wq_do');
189 like($warn[1], qr/ wq_worker: /, '2nd warned from wq_worker');
190 is($warn[2], $warn[1], 'worker did not die');
192 $SIG{__WARN__} = 'DEFAULT';
193 is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
194 is($ipc->wq_workers, 1, '1 worker started');
196 $ipc->WQ_MAX_WORKERS > 1 or
197 skip 'Inline::C or Socket::MsgHdr not available', 4;
198 $ipc->wq_worker_incr;
199 is($ipc->wq_workers, 2, 'worker count bumped');
200 $ipc->wq_worker_decr;
201 $ipc->wq_worker_decr_wait(10);
202 is($ipc->wq_workers, 1, 'worker count lowered');
203 is($ipc->wq_workers(2), 2, 'worker count set');
204 is($ipc->wq_workers, 2, 'worker count stayed set');
207 is($ipc->wq_workers, undef, 'workers undef after close');