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 require_ok 'PublicInbox::IPC';
10 state $once = eval <<'';
11 package PublicInbox::IPC;
13 sub test_array { qw(test array) }
14 sub test_scalar { 'scalar' }
15 sub test_scalarref { \'scalarref' }
16 sub test_undef { undef }
17 sub test_die { shift; die @_; 'unreachable' }
19 sub test_write_each_fd {
20 my ($self, @args) = @_;
22 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
28 my $ipc = bless {}, 'PublicInbox::IPC';
29 my @t = qw(array scalar scalarref undef);
35 my @ret = $ipc->ipc_do($m);
37 is_deeply(\@ret, \@exp, "wantarray $m $x");
41 $ipc->ipc_async($m, [], sub { push @res, \@_ }, \$m);
43 my $ret = $ipc->ipc_do($m);
45 is_deeply($ret, $exp, "!wantarray $m $x");
47 is_deeply(\@res, [ [ \$m, \@exp ] ], "async $m $x");
50 $ipc->ipc_async_wait(-1);
51 is_deeply(\@res, [], 'no leftover results');
52 $ipc->ipc_async('test_die', ['die test'],
53 sub { push @res, \@_ }, 'die arg');
54 $ipc->ipc_async_wait(1);
55 is(scalar(@res), 1, 'only one result');
56 is(scalar(@{$res[0]}), 2, 'result has 2-element array');
57 is($res[0]->[0], 'die arg', 'got async die arg '.$x);
58 is(ref($res[0]->[1]), 'PublicInbox::IPC::Die',
61 my $nr = PublicInbox::IPC::PIPE_BUF();
63 my $cb = sub { ++$count };
64 $ipc->ipc_async('test_undef', [], $cb) for (1..$nr);
65 $ipc->ipc_async_wait(-1);
66 is($count, $nr, "$x async runs w/o deadlock");
69 my $ret = eval { $ipc->test_die('phail') };
71 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
75 s/ line (\d+).*//s and $lines{$1}++;
77 is(scalar keys %lines, 1, 'line numbers match');
78 is((values %lines)[0], 2, '2 hits on same line number');
79 is($err, $exp, "$x die matches");
80 is($ret, undef, "$x die did not return");
82 eval { $ipc->test_die(['arrayref']) };
84 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
86 is_deeply($err, $exp, 'die with unblessed ref');
87 is(ref($err), 'ARRAY', 'got an array ref');
89 $exp = bless ['blessed'], 'PublicInbox::WTF';
90 $ret = eval { $ipc->ipc_do('test_die', $exp) };
92 is_deeply($err, $exp, 'die with blessed ref');
93 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
98 require_mods(qw(Storable||Sereal), 16);
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');
105 my ($tmp, $for_destroy) = tmpdir();
106 $ipc->ipc_lock_init("$tmp/lock");
107 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
109 $ipc->ipc_worker_stop;
110 ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
112 $ipc->ipc_worker_stop; # idempotent
115 $ipc->{wq_open_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;
121 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
123 for my $t ('local', 'worker', 'worker again') {
124 $ipc->wq_do('test_write_each_fd', $wa, $wb, $wc, 'hello world');
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)");
132 $ipc->wq_do('test_die', $wa, $wb, $wc);
133 my $ppid = $ipc->wq_workers_start('wq', 1);
137 # wq_do works across fork (siblings can feed)
139 skip 'Socket::MsgHdr, IO::FDPass, Inline::C missing', 7 if !$ppids[0];
140 is_deeply(\@ppids, [$$, undef, undef],
141 'parent pid returned in wq_workers_start');
142 my $pid = fork // BAIL_OUT $!;
145 $ipc->wq_do('test_write_each_fd', $wa, $wb, $wc, $$);
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');
158 is(waitpid($pid, 0), $pid, 'waitpid complete');
159 is($?, 0, 'child wq producer exited');
164 seek($warn, 0, SEEK_SET) or BAIL_OUT;
166 is(scalar(@warn), 3, 'warned 3 times');
167 like($warn[0], qr/ wq_do: /, '1st warned from wq_do');
168 like($warn[1], qr/ wq_worker: /, '2nd warned from wq_worker');
169 is($warn[2], $warn[1], 'worker did not die');