]> Sergey Matveev's repositories - public-inbox.git/blob - t/ipc.t
ipc: work queue support via SOCK_SEQPACKET
[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 use Fcntl qw(SEEK_SET);
9 require_ok 'PublicInbox::IPC';
10 state $once = eval <<'';
11 package PublicInbox::IPC;
12 use strict;
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' }
18 sub test_pid { $$ }
19 sub test_write_each_fd {
20         my ($self, @args) = @_;
21         for my $fd (0..2) {
22                 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
23                 $self->{$fd}->flush;
24         }
25 }
26 1;
27
28 my $ipc = bless {}, 'PublicInbox::IPC';
29 my @t = qw(array scalar scalarref undef);
30 my $test = sub {
31         my $x = shift;
32         my @res;
33         for my $type (@t) {
34                 my $m = "test_$type";
35                 my @ret = $ipc->ipc_do($m);
36                 my @exp = $ipc->$m;
37                 is_deeply(\@ret, \@exp, "wantarray $m $x");
38
39                 $ipc->ipc_do($m);
40
41                 $ipc->ipc_async($m, [], sub { push @res, \@_ }, \$m);
42
43                 my $ret = $ipc->ipc_do($m);
44                 my $exp = $ipc->$m;
45                 is_deeply($ret, $exp, "!wantarray $m $x");
46
47                 is_deeply(\@res, [ [ \$m, \@exp ] ], "async $m $x");
48                 @res = ();
49         }
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',
59                 "exception type $x");
60         {
61                 my $nr = PublicInbox::IPC::PIPE_BUF();
62                 my $count = 0;
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");
67         }
68
69         my $ret = eval { $ipc->test_die('phail') };
70         my $exp = $@;
71         $ret = eval { $ipc->ipc_do('test_die', 'phail') };
72         my $err = $@;
73         my %lines;
74         for ($err, $exp) {
75                 s/ line (\d+).*//s and $lines{$1}++;
76         }
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");
81
82         eval { $ipc->test_die(['arrayref']) };
83         $exp = $@;
84         $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
85         $err = $@;
86         is_deeply($err, $exp, 'die with unblessed ref');
87         is(ref($err), 'ARRAY', 'got an array ref');
88
89         $exp = bless ['blessed'], 'PublicInbox::WTF';
90         $ret = eval { $ipc->ipc_do('test_die', $exp) };
91         $err = $@;
92         is_deeply($err, $exp, 'die with blessed ref');
93         is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
94 };
95 $test->('local');
96
97 SKIP: {
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');
103         $test->('worker');
104         {
105                 my ($tmp, $for_destroy) = tmpdir();
106                 $ipc->ipc_lock_init("$tmp/lock");
107                 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
108         }
109         $ipc->ipc_worker_stop;
110         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
111 }
112 $ipc->ipc_worker_stop; # idempotent
113
114 # work queues
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;
120 $warn->autoflush(0);
121 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
122 my @ppids;
123 for my $t ('local', 'worker', 'worker again') {
124         $ipc->wq_do('test_write_each_fd', $wa, $wb, $wc, 'hello world');
125         my $i = 0;
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)");
130                 $i++;
131         }
132         $ipc->wq_do('test_die', $wa, $wb, $wc);
133         my $ppid = $ipc->wq_workers_start('wq', 1);
134         push(@ppids, $ppid);
135 }
136
137 # wq_do works across fork (siblings can feed)
138 SKIP: {
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 $!;
143         if ($pid == 0) {
144                 use POSIX qw(_exit);
145                 $ipc->wq_do('test_write_each_fd', $wa, $wb, $wc, $$);
146                 _exit(0);
147         } else {
148                 my $i = 0;
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');
156                         $i++;
157                 }
158                 is(waitpid($pid, 0), $pid, 'waitpid complete');
159                 is($?, 0, 'child wq producer exited');
160         }
161 }
162
163 $ipc->wq_close;
164 seek($warn, 0, SEEK_SET) or BAIL_OUT;
165 my @warn = <$warn>;
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');
170
171 done_testing;