2 # Copyright (C) 2020 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 require_ok 'PublicInbox::IPC';
9 state $once = eval <<'';
10 package PublicInbox::IPC;
12 sub test_array { qw(test array) }
13 sub test_scalar { 'scalar' }
14 sub test_scalarref { \'scalarref' }
15 sub test_undef { undef }
16 sub test_die { shift; die @_; 'unreachable' }
20 my $ipc = bless {}, 'PublicInbox::IPC';
21 my @t = qw(array scalar scalarref undef);
26 my @ret = $ipc->ipc_do($m);
28 is_deeply(\@ret, \@exp, "wantarray $m $x");
32 my $ret = $ipc->ipc_do($m);
34 is_deeply($ret, $exp, "!wantarray $m $x");
36 my $ret = eval { $ipc->test_die('phail') };
38 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
42 s/ line (\d+).*//s and $lines{$1}++;
44 is(scalar keys %lines, 1, 'line numbers match');
45 is((values %lines)[0], 2, '2 hits on same line number');
46 is($err, $exp, "$x die matches");
47 is($ret, undef, "$x die did not return");
52 require_mods(qw(Storable||Sereal), 16);
53 my $pid = $ipc->ipc_worker_spawn('test worker');
54 ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
55 defined($pid) or BAIL_OUT 'no spawn, no test';
56 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
59 my ($tmp, $for_destroy) = tmpdir();
60 $ipc->ipc_lock_init("$tmp/lock");
61 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
63 $ipc->ipc_worker_stop;
64 ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
66 $ipc->ipc_worker_stop; # idempotent