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_mods(qw(Storable||Sereal));
10 require_ok 'PublicInbox::IPC';
11 state $once = eval <<'';
12 package PublicInbox::IPC;
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' }
20 sub test_write_each_fd {
21 my ($self, @args) = @_;
23 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
29 my $ipc = bless {}, 'PublicInbox::IPC';
30 my @t = qw(array scalar scalarref undef);
36 my @ret = $ipc->ipc_do($m);
38 is_deeply(\@ret, \@exp, "wantarray $m $x");
42 $ipc->ipc_async($m, [], sub { push @res, \@_ }, \$m);
44 my $ret = $ipc->ipc_do($m);
46 is_deeply($ret, $exp, "!wantarray $m $x");
48 is_deeply(\@res, [ [ \$m, \@exp ] ], "async $m $x");
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',
62 my $nr = PublicInbox::IPC::PIPE_BUF();
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");
70 my $ret = eval { $ipc->test_die('phail') };
72 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
76 s/ line (\d+).*//s and $lines{$1}++;
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");
83 eval { $ipc->test_die(['arrayref']) };
85 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
87 is_deeply($err, $exp, 'die with unblessed ref');
88 is(ref($err), 'ARRAY', 'got an array ref');
90 $exp = bless ['blessed'], 'PublicInbox::WTF';
91 $ret = eval { $ipc->ipc_do('test_die', $exp) };
93 is_deeply($err, $exp, 'die with blessed ref');
94 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
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_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;
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 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 $!;
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');
165 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
166 seek($warn, 0, SEEK_SET) or BAIL_OUT;
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');
173 $SIG{__WARN__} = 'DEFAULT';
174 is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
175 is($ipc->wq_workers, 1, '1 worker started');
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');
188 is($ipc->wq_workers, undef, 'workers undef after close');