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 state $once = eval <<'';
13 package PublicInbox::IPC;
15 use Digest::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";
36 my $ipc = bless {}, 'PublicInbox::IPC';
37 my @t = qw(array scalar scalarref undef);
42 my @ret = $ipc->ipc_do($m);
44 is_deeply(\@ret, \@exp, "wantarray $m $x");
48 my $ret = $ipc->ipc_do($m);
50 is_deeply($ret, $exp, "!wantarray $m $x");
52 my $ret = eval { $ipc->test_die('phail') };
54 $ret = eval { $ipc->ipc_do('test_die', 'phail') };
58 s/ line (\d+).*//s and $lines{$1}++;
60 is(scalar keys %lines, 1, 'line numbers match');
61 is((values %lines)[0], 2, '2 hits on same line number');
62 is($err, $exp, "$x die matches");
63 is($ret, undef, "$x die did not return");
65 eval { $ipc->test_die(['arrayref']) };
67 $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
69 is_deeply($err, $exp, 'die with unblessed ref');
70 is(ref($err), 'ARRAY', 'got an array ref');
72 $exp = bless ['blessed'], 'PublicInbox::WTF';
73 $ret = eval { $ipc->ipc_do('test_die', $exp) };
75 is_deeply($err, $exp, 'die with blessed ref');
76 is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
81 my $pid = $ipc->ipc_worker_spawn('test worker');
82 ok($pid > 0 && kill(0, $pid), 'worker spawned and running');
83 defined($pid) or BAIL_OUT 'no spawn, no test';
84 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
87 my ($tmp, $for_destroy) = tmpdir();
88 $ipc->ipc_lock_init("$tmp/lock");
89 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
91 $ipc->ipc_worker_stop;
92 ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
94 $ipc->ipc_worker_stop; # idempotent
97 pipe(my ($ra, $wa)) or BAIL_OUT $!;
98 pipe(my ($rb, $wb)) or BAIL_OUT $!;
99 pipe(my ($rc, $wc)) or BAIL_OUT $!;
100 open my $warn, '+>', undef or BAIL_OUT;
102 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
104 open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!";
105 my $big = do { local $/; <$agpl> } // BAIL_OUT "read: $!";
106 close $agpl or BAIL_OUT "close: $!";
108 for my $t ('local', 'worker', 'worker again') {
109 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
111 for my $fh ($ra, $rb, $rc) {
112 my $buf = readline($fh);
113 is(chop($buf), "\n", "trailing CR ($t)");
114 like($buf, qr/\Ai=$i \d+ hello world\z/, "got expected ($t)");
117 $ipc->wq_io_do('test_die', [ $wa, $wb, $wc ]);
118 $ipc->wq_io_do('test_sha', [ $wa, $wb ], 'hello world');
119 is(readline($rb), sha1_hex('hello world')."\n", "SHA small ($t)");
121 my $bigger = $big x 10;
122 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
123 my $exp = sha1_hex($bigger)."\n";
125 is(readline($rb), $exp, "SHA big ($t)");
127 my $ppid = $ipc->wq_workers_start('wq', 1);
131 # wq_io_do works across fork (siblings can feed)
133 skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0];
134 is_deeply(\@ppids, [$$, undef, undef],
135 'parent pid returned in wq_workers_start');
136 my $pid = fork // BAIL_OUT $!;
139 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
143 my ($wpid, @rest) = keys %{$ipc->{-wq_workers}};
144 is(scalar(@rest), 0, 'only one worker');
145 for my $fh ($ra, $rb, $rc) {
146 my $buf = readline($fh);
147 is(chop($buf), "\n", "trailing CR #$i");
148 like($buf, qr/^i=$i $wpid $pid\z/,
149 'got expected from sibling');
152 is(waitpid($pid, 0), $pid, 'waitpid complete');
153 is($?, 0, 'child wq producer exited');
159 skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
160 seek($warn, 0, SEEK_SET) or BAIL_OUT;
162 is(scalar(@warn), 3, 'warned 3 times');
163 like($warn[0], qr/ wq_io_do: /, '1st warned from wq_do');
164 like($warn[1], qr/ wq_worker: /, '2nd warned from wq_worker');
165 is($warn[2], $warn[1], 'worker did not die');
167 $SIG{__WARN__} = 'DEFAULT';
168 is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
169 is($ipc->wq_workers, 1, '1 worker started');
171 $ipc->WQ_MAX_WORKERS > 1 or
172 skip 'Inline::C or Socket::MsgHdr not available', 4;
173 $ipc->wq_worker_incr;
174 is($ipc->wq_workers, 2, 'worker count bumped');
175 $ipc->wq_worker_decr;
176 $ipc->wq_worker_decr_wait(10);
177 is($ipc->wq_workers, 1, 'worker count lowered');
178 is($ipc->wq_workers(2), 2, 'worker count set');
179 is($ipc->wq_workers, 2, 'worker count stayed set');
182 is($ipc->wq_workers, undef, 'workers undef after close');