lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / ipc.t
blob23ae2e7b2455dbb0b7febdfd25702408b36722b0
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 use Fcntl qw(SEEK_SET);
7 use PublicInbox::SHA qw(sha1_hex);
8 require_mods(qw(Storable||Sereal));
9 require_ok 'PublicInbox::IPC';
10 my ($tmpdir, $for_destroy) = tmpdir();
11 state $once = eval <<'';
12 package PublicInbox::IPC;
13 use strict;
14 use PublicInbox::SHA qw(sha1_hex);
15 sub test_array { qw(test array) }
16 sub test_scalar { 'scalar' }
17 sub test_scalarref { \'scalarref' }
18 sub test_undef { undef }
19 sub test_die { shift; die @_; 'unreachable' }
20 sub test_pid { $$ }
21 sub test_write_each_fd {
22         my ($self, @args) = @_;
23         for my $fd (0..2) {
24                 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
25                 $self->{$fd}->flush;
26         }
28 sub test_sha {
29         my ($self, $buf) = @_;
30         print { $self->{1} } sha1_hex($buf), "\n";
31         $self->{1}->flush;
33 sub test_append_pid {
34         my ($self, $file) = @_;
35         open my $fh, '>>', $file or die "open: $!";
36         $fh->autoflush(1);
37         print $fh "$$\n" or die "print: $!";
41 my $ipc = bless {}, 'PublicInbox::IPC';
42 my @t = qw(array scalar scalarref undef);
43 my $test = sub {
44         my $x = shift;
45         for my $type (@t) {
46                 my $m = "test_$type";
47                 my @ret = $ipc->ipc_do($m);
48                 my @exp = $ipc->$m;
49                 is_deeply(\@ret, \@exp, "wantarray $m $x");
51                 $ipc->ipc_do($m);
53                 my $ret = $ipc->ipc_do($m);
54                 my $exp = $ipc->$m;
55                 is_deeply($ret, $exp, "!wantarray $m $x");
56         }
57         my $ret = eval { $ipc->test_die('phail') };
58         my $exp = $@;
59         $ret = eval { $ipc->ipc_do('test_die', 'phail') };
60         my $err = $@;
61         my %lines;
62         for ($err, $exp) {
63                 s/ line (\d+).*//s and $lines{$1}++;
64         }
65         is(scalar keys %lines, 1, 'line numbers match');
66         is((values %lines)[0], 2, '2 hits on same line number');
67         is($err, $exp, "$x die matches");
68         is($ret, undef, "$x die did not return");
70         eval { $ipc->test_die(['arrayref']) };
71         $exp = $@;
72         $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
73         $err = $@;
74         is_deeply($err, $exp, 'die with unblessed ref');
75         is(ref($err), 'ARRAY', 'got an array ref');
77         $exp = bless ['blessed'], 'PublicInbox::WTF';
78         $ret = eval { $ipc->ipc_do('test_die', $exp) };
79         $err = $@;
80         is_deeply($err, $exp, 'die with blessed ref');
81         is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
83 $test->('local');
86         my $pid = $ipc->ipc_worker_spawn('test worker');
87         ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
88         defined($pid) or BAIL_OUT 'no spawn, no test';
89         is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
90         $test->('worker');
91         is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
92         $ipc->ipc_worker_stop;
93         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
95 $ipc->ipc_worker_stop; # idempotent
97 # work queues
98 pipe(my ($ra, $wa)) or BAIL_OUT $!;
99 pipe(my ($rb, $wb)) or BAIL_OUT $!;
100 pipe(my ($rc, $wc)) or BAIL_OUT $!;
101 open my $warn, '+>', undef or BAIL_OUT;
102 $warn->autoflush(0);
103 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
104 my @ppids;
105 open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!";
106 my $big = do { local $/; <$agpl> } // BAIL_OUT "read: $!";
107 close $agpl or BAIL_OUT "close: $!";
109 for my $t ('worker', 'worker again') {
110         my $ppid = $ipc->wq_workers_start('wq', 1);
111         push(@ppids, $ppid);
112         $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
113         my $i = 0;
114         for my $fh ($ra, $rb, $rc) {
115                 my $buf = readline($fh);
116                 is(chop($buf), "\n", "trailing CR ($t)");
117                 like($buf, qr/\Ai=$i \d+ hello world\z/, "got expected ($t)");
118                 $i++;
119         }
120         $ipc->wq_io_do('test_die', [ $wa, $wb, $wc ]);
121         $ipc->wq_io_do('test_sha', [ $wa, $wb ], 'hello world');
122         is(readline($rb), sha1_hex('hello world')."\n", "SHA small ($t)");
123         {
124                 my $bigger = $big x 10; # to hit EMSGSIZE
125                 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
126                 my $exp = sha1_hex($bigger)."\n";
127                 is(readline($rb), $exp, "SHA big for EMSGSIZE ($t)");
129                 # to hit the WQWorker recv_and_run length
130                 substr($bigger, my $MY_MAX_ARG_STRLEN = 4096 * 33, -1) = '';
131                 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
132                 $exp = sha1_hex($bigger)."\n";
133                 is(readline($rb), $exp, "SHA WQWorker limit ($t)");
134         }
135         SKIP: {
136                 $ENV{TEST_EXPENSIVE} or skip 'TEST_EXPENSIVE not set', 1;
137                 my $bigger = $big x 75000; # over 2G to trigger partial sendmsg
138                 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
139                 my $exp = sha1_hex($bigger)."\n";
140                 is(readline($rb), $exp, "SHA WQWorker sendmsg limit ($t)");
141         }
144 # wq_io_do works across fork (siblings can feed)
145 SKIP: {
146         skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0];
147         is_xdeeply(\@ppids, [$$, undef],
148                 'parent pid returned in wq_workers_start');
149         my $pid = fork // BAIL_OUT $!;
150         if ($pid == 0) {
151                 use POSIX qw(_exit);
152                 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
153                 _exit(0);
154         } else {
155                 my $i = 0;
156                 my ($wpid, @rest) = keys %{$ipc->{-wq_workers}};
157                 is(scalar(@rest), 0, 'only one worker');
158                 for my $fh ($ra, $rb, $rc) {
159                         my $buf = readline($fh);
160                         is(chop($buf), "\n", "trailing CR #$i");
161                         like($buf, qr/^i=$i $wpid $pid\z/,
162                                 'got expected from sibling');
163                         $i++;
164                 }
165                 is(waitpid($pid, 0), $pid, 'waitpid complete');
166                 is($?, 0, 'child wq producer exited');
167         }
168         my @ary = $ipc->wq_do('test_array');
169         is_deeply(\@ary, [ qw(test array) ], 'wq_do wantarray');
170         is(my $s = $ipc->wq_do('test_scalar'), 'scalar', 'defined wantarray');
171         my $exp = bless ['blessed'], 'PublicInbox::WTF';
172         my $ret = eval { $ipc->wq_do('test_die', $exp) };
173         is_deeply($@, $exp, 'die with blessed ref');
176 $ipc->wq_close;
177 SKIP: {
178         skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
179         seek($warn, 0, SEEK_SET) or BAIL_OUT;
180         my @warn = <$warn>;
181         is(scalar(@warn), 2, 'warned 3 times');
182         like($warn[0], qr/ wq_worker: /, '2nd warned from wq_worker');
183         is($warn[0], $warn[1], 'worker did not die');
185         $SIG{__WARN__} = 'DEFAULT';
186         is($ipc->wq_workers_start('wq', 2), $$, 'workers started again');
187         $ipc->wq_broadcast('test_append_pid', "$tmpdir/append_pid");
188         $ipc->wq_close;
189         open my $fh, '<', "$tmpdir/append_pid" or BAIL_OUT "open: $!";
190         chomp(my @pids = <$fh>);
191         my %pids = map { $_ => 1 } grep(/\A[0-9]+\z/, @pids);
192         is(scalar keys %pids, 2, 'broadcast hit both PIDs');
195 done_testing;