2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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;
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' }
21 sub test_write_each_fd {
22 my ($self, @args) = @_;
24 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
29 my ($self, $buf) = @_;
30 print { $self->{1} } sha1_hex($buf), "\n";
34 my ($self, $file) = @_;
35 open my $fh, '>>', $file or die "open: $!";
37 print $fh "$$\n" or die "print: $!";
41 my $ipc = bless {}, 'PublicInbox::IPC';
42 my @t = qw(array scalar scalarref undef);
47 my @ret = $ipc->ipc_do($m);
49 is_deeply(\@ret, \@exp, "wantarray $m $x");
53 my $ret = $ipc->ipc_do($m);
55 is_deeply($ret, $exp, "!wantarray $m $x");
57 my $ret = eval { $ipc->test_die('phail') };
59 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
63 s/ line (\d+).*//s and $lines{$1}++;
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']) };
72 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
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) };
80 is_deeply($err, $exp, 'die with blessed ref');
81 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
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');
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
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;
103 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
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);
112 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
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)");
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)");
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)");
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)");
144 # wq_io_do works across fork (siblings can feed)
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 $!;
152 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
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');
165 is(waitpid($pid, 0), $pid, 'waitpid complete');
166 is($?, 0, 'child wq producer exited');
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');
178 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
179 seek($warn, 0, SEEK_SET) or BAIL_OUT;
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");
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');