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