]> Sergey Matveev's repositories - public-inbox.git/blob - t/ipc.t
update copyrights for 2021
[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 require_ok 'PublicInbox::IPC';
9 state $once = eval <<'';
10 package PublicInbox::IPC;
11 use strict;
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' }
17 sub test_pid { $$ }
18 1;
19
20 my $ipc = bless {}, 'PublicInbox::IPC';
21 my @t = qw(array scalar scalarref undef);
22 my $test = sub {
23         my $x = shift;
24         for my $type (@t) {
25                 my $m = "test_$type";
26                 my @ret = $ipc->ipc_do($m);
27                 my @exp = $ipc->$m;
28                 is_deeply(\@ret, \@exp, "wantarray $m $x");
29
30                 $ipc->ipc_do($m);
31
32                 my $ret = $ipc->ipc_do($m);
33                 my $exp = $ipc->$m;
34                 is_deeply($ret, $exp, "!wantarray $m $x");
35         }
36         my $ret = eval { $ipc->test_die('phail') };
37         my $exp = $@;
38         $ret = eval { $ipc->ipc_do('test_die', 'phail') };
39         my $err = $@;
40         my %lines;
41         for ($err, $exp) {
42                 s/ line (\d+).*//s and $lines{$1}++;
43         }
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");
48
49         eval { $ipc->test_die(['arrayref']) };
50         $exp = $@;
51         $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
52         $err = $@;
53         is_deeply($err, $exp, 'die with unblessed ref');
54         is(ref($err), 'ARRAY', 'got an array ref');
55
56         $exp = bless ['blessed'], 'PublicInbox::WTF';
57         $ret = eval { $ipc->ipc_do('test_die', $exp) };
58         $err = $@;
59         is_deeply($err, $exp, 'die with blessed ref');
60         is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
61 };
62 $test->('local');
63
64 SKIP: {
65         require_mods(qw(Storable||Sereal), 16);
66         my $pid = $ipc->ipc_worker_spawn('test worker');
67         ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
68         defined($pid) or BAIL_OUT 'no spawn, no test';
69         is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
70         $test->('worker');
71         {
72                 my ($tmp, $for_destroy) = tmpdir();
73                 $ipc->ipc_lock_init("$tmp/lock");
74                 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
75         }
76         $ipc->ipc_worker_stop;
77         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
78 }
79 $ipc->ipc_worker_stop; # idempotent
80 done_testing;