2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use Fcntl qw(SEEK_SET);
8 use PublicInbox::SHA qw(sha1_hex);
9 require_mods(qw(Storable||Sereal));
10 require_ok 'PublicInbox::IPC';
11 my ($tmpdir, $for_destroy) = tmpdir();
12 state $once = eval <<'';
13 package PublicInbox::IPC;
15 use PublicInbox::SHA qw(sha1_hex);
16 sub test_array { qw(test array) }
17 sub test_scalar { 'scalar' }
18 sub test_scalarref { \'scalarref' }
19 sub test_undef { undef }
20 sub test_die { shift; die @_; 'unreachable' }
22 sub test_write_each_fd {
23 my ($self, @args) = @_;
25 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
30 my ($self, $buf) = @_;
31 print { $self->{1} } sha1_hex($buf), "\n";
35 my ($self, $file) = @_;
36 open my $fh, '>>', $file or die "open: $!";
38 print $fh "$$\n" or die "print: $!";
42 my $ipc = bless {}, 'PublicInbox::IPC';
43 my @t = qw(array scalar scalarref undef);
48 my @ret = $ipc->ipc_do($m);
50 is_deeply(\@ret, \@exp, "wantarray $m $x");
54 my $ret = $ipc->ipc_do($m);
56 is_deeply($ret, $exp, "!wantarray $m $x");
58 my $ret = eval { $ipc->test_die('phail') };
60 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
64 s/ line (\d+).*//s and $lines{$1}++;
66 is(scalar keys %lines, 1, 'line numbers match');
67 is((values %lines)[0], 2, '2 hits on same line number');
68 is($err, $exp, "$x die matches");
69 is($ret, undef, "$x die did not return");
71 eval { $ipc->test_die(['arrayref']) };
73 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
75 is_deeply($err, $exp, 'die with unblessed ref');
76 is(ref($err), 'ARRAY', 'got an array ref');
78 $exp = bless ['blessed'], 'PublicInbox::WTF';
79 $ret = eval { $ipc->ipc_do('test_die', $exp) };
81 is_deeply($err, $exp, 'die with blessed ref');
82 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
87 my $pid = $ipc->ipc_worker_spawn('test worker');
88 ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
89 defined($pid) or BAIL_OUT 'no spawn, no test';
90 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
92 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
93 $ipc->ipc_worker_stop;
94 ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
96 $ipc->ipc_worker_stop; # idempotent
99 pipe(my ($ra, $wa)) or BAIL_OUT $!;
100 pipe(my ($rb, $wb)) or BAIL_OUT $!;
101 pipe(my ($rc, $wc)) or BAIL_OUT $!;
102 open my $warn, '+>', undef or BAIL_OUT;
104 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
106 open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!";
107 my $big = do { local $/; <$agpl> } // BAIL_OUT "read: $!";
108 close $agpl or BAIL_OUT "close: $!";
110 for my $t ('worker', 'worker again') {
111 my $ppid = $ipc->wq_workers_start('wq', 1);
113 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
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)");
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)");
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)");
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)");
137 $ENV{TEST_EXPENSIVE} or skip 'TEST_EXPENSIVE not set', 1;
138 my $bigger = $big x 75000; # over 2G to trigger partial sendmsg
139 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
140 my $exp = sha1_hex($bigger)."\n";
141 is(readline($rb), $exp, "SHA WQWorker sendmsg limit ($t)");
145 # wq_io_do works across fork (siblings can feed)
147 skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0];
148 is_xdeeply(\@ppids, [$$, undef],
149 'parent pid returned in wq_workers_start');
150 my $pid = fork // BAIL_OUT $!;
153 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
157 my ($wpid, @rest) = keys %{$ipc->{-wq_workers}};
158 is(scalar(@rest), 0, 'only one worker');
159 for my $fh ($ra, $rb, $rc) {
160 my $buf = readline($fh);
161 is(chop($buf), "\n", "trailing CR #$i");
162 like($buf, qr/^i=$i $wpid $pid\z/,
163 'got expected from sibling');
166 is(waitpid($pid, 0), $pid, 'waitpid complete');
167 is($?, 0, 'child wq producer exited');
169 my @ary = $ipc->wq_do('test_array');
170 is_deeply(\@ary, [ qw(test array) ], 'wq_do wantarray');
171 is(my $s = $ipc->wq_do('test_scalar'), 'scalar', 'defined wantarray');
172 my $exp = bless ['blessed'], 'PublicInbox::WTF';
173 my $ret = eval { $ipc->wq_do('test_die', $exp) };
174 is_deeply($@, $exp, 'die with blessed ref');
179 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
180 seek $warn, 0, SEEK_SET;
182 is(scalar(@warn), 2, 'warned 3 times');
183 like($warn[0], qr/ wq_worker: /, '2nd warned from wq_worker');
184 is($warn[0], $warn[1], 'worker did not die');
186 $SIG{__WARN__} = 'DEFAULT';
187 is($ipc->wq_workers_start('wq', 2), $$, 'workers started again');
188 $ipc->wq_broadcast('test_append_pid', "$tmpdir/append_pid");
190 open my $fh, '<', "$tmpdir/append_pid" or BAIL_OUT "open: $!";
191 chomp(my @pids = <$fh>);
192 my %pids = map { $_ => 1 } grep(/\A[0-9]+\z/, @pids);
193 is(scalar keys %pids, 2, 'broadcast hit both PIDs');