]> Sergey Matveev's repositories - public-inbox.git/blob - t/ipc.t
ipc: support Sereal
[public-inbox.git] / t / ipc.t
1 #!perl -w
2 # Copyright (C) 2020 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 $test->('local');
50
51 SKIP: {
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');
57         $test->('worker');
58         {
59                 my ($tmp, $for_destroy) = tmpdir();
60                 $ipc->ipc_lock_init("$tmp/lock");
61                 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
62         }
63         $ipc->ipc_worker_stop;
64         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
65 }
66 $ipc->ipc_worker_stop; # idempotent
67 done_testing;