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>
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;
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' }
23 sub test_write_each_fd {
24 my ($self, @args) = @_;
26 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
31 my ($self, $buf) = @_;
32 print { $self->{1} } sha1_hex($buf), "\n";
36 my ($self, $file) = @_;
37 open my $fh, '>>', $file or die "open: $!";
39 print $fh "$$\n" or die "print: $!";
43 my $ipc = bless {}, 'PublicInbox::IPC';
44 my @t = qw(array scalar scalarref undef);
49 my @ret = $ipc->ipc_do($m);
51 is_deeply(\@ret, \@exp, "wantarray $m $x");
55 my $ret = $ipc->ipc_do($m);
57 is_deeply($ret, $exp, "!wantarray $m $x");
59 my $ret = eval { $ipc->test_die('phail') };
61 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
65 s/ line (\d+).*//s and $lines{$1}++;
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");
72 eval { $ipc->test_die(['arrayref']) };
74 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
76 is_deeply($err, $exp, 'die with unblessed ref');
77 is(ref($err), 'ARRAY', 'got an array ref');
79 $exp = bless ['blessed'], 'PublicInbox::WTF';
80 $ret = eval { $ipc->ipc_do('test_die', $exp) };
82 is_deeply($err, $exp, 'die with blessed ref');
83 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
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');
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');
98 $ipc->ipc_worker_stop; # idempotent
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;
106 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
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: $!";
112 for my $t ('local', 'worker', 'worker again') {
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)");
136 my $ppid = $ipc->wq_workers_start('wq', 1);
140 # wq_io_do works across fork (siblings can feed)
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 $!;
148 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
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');
161 is(waitpid($pid, 0), $pid, 'waitpid complete');
162 is($?, 0, 'child wq producer exited');
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');
174 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
175 seek($warn, 0, SEEK_SET) or BAIL_OUT;
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');
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");
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');