]> Sergey Matveev's repositories - public-inbox.git/blob - t/ipc.t
ipc: wq_do => wq_io_do
[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 state $once = eval <<'';
13 package PublicInbox::IPC;
14 use strict;
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' }
21 sub test_pid { $$ }
22 sub test_write_each_fd {
23         my ($self, @args) = @_;
24         for my $fd (0..2) {
25                 print { $self->{$fd} } "i=$fd $$ ", @args, "\n";
26                 $self->{$fd}->flush;
27         }
28 }
29 sub test_sha {
30         my ($self, $buf) = @_;
31         print { $self->{1} } sha1_hex($buf), "\n";
32         $self->{1}->flush;
33 }
34 1;
35
36 my $ipc = bless {}, 'PublicInbox::IPC';
37 my @t = qw(array scalar scalarref undef);
38 my $test = sub {
39         my $x = shift;
40         for my $type (@t) {
41                 my $m = "test_$type";
42                 my @ret = $ipc->ipc_do($m);
43                 my @exp = $ipc->$m;
44                 is_deeply(\@ret, \@exp, "wantarray $m $x");
45
46                 $ipc->ipc_do($m);
47
48                 my $ret = $ipc->ipc_do($m);
49                 my $exp = $ipc->$m;
50                 is_deeply($ret, $exp, "!wantarray $m $x");
51         }
52         my $ret = eval { $ipc->test_die('phail') };
53         my $exp = $@;
54         $ret = eval { $ipc->ipc_do('test_die', 'phail') };
55         my $err = $@;
56         my %lines;
57         for ($err, $exp) {
58                 s/ line (\d+).*//s and $lines{$1}++;
59         }
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");
64
65         eval { $ipc->test_die(['arrayref']) };
66         $exp = $@;
67         $ret = eval { $ipc->ipc_do('test_die', ['arrayref']) };
68         $err = $@;
69         is_deeply($err, $exp, 'die with unblessed ref');
70         is(ref($err), 'ARRAY', 'got an array ref');
71
72         $exp = bless ['blessed'], 'PublicInbox::WTF';
73         $ret = eval { $ipc->ipc_do('test_die', $exp) };
74         $err = $@;
75         is_deeply($err, $exp, 'die with blessed ref');
76         is(ref($err), 'PublicInbox::WTF', 'got blessed ref');
77 };
78 $test->('local');
79
80 {
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');
85         $test->('worker');
86         {
87                 my ($tmp, $for_destroy) = tmpdir();
88                 $ipc->ipc_lock_init("$tmp/lock");
89                 is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned');
90         }
91         $ipc->ipc_worker_stop;
92         ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped');
93 }
94 $ipc->ipc_worker_stop; # idempotent
95
96 # work queues
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;
101 $warn->autoflush(0);
102 local $SIG{__WARN__} = sub { print $warn "PID:$$ ", @_ };
103 my @ppids;
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: $!";
107
108 for my $t ('local', 'worker', 'worker again') {
109         $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world');
110         my $i = 0;
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)");
115                 $i++;
116         }
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)");
120         {
121                 my $bigger = $big x 10;
122                 $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger);
123                 my $exp = sha1_hex($bigger)."\n";
124                 undef $bigger;
125                 is(readline($rb), $exp, "SHA big ($t)");
126         }
127         my $ppid = $ipc->wq_workers_start('wq', 1);
128         push(@ppids, $ppid);
129 }
130
131 # wq_io_do works across fork (siblings can feed)
132 SKIP: {
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 $!;
137         if ($pid == 0) {
138                 use POSIX qw(_exit);
139                 $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$);
140                 _exit(0);
141         } else {
142                 my $i = 0;
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');
150                         $i++;
151                 }
152                 is(waitpid($pid, 0), $pid, 'waitpid complete');
153                 is($?, 0, 'child wq producer exited');
154         }
155 }
156
157 $ipc->wq_close;
158 SKIP: {
159         skip 'Socket::MsgHdr or Inline::C missing', 11 if !$ppids[0];
160         seek($warn, 0, SEEK_SET) or BAIL_OUT;
161         my @warn = <$warn>;
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');
166
167         $SIG{__WARN__} = 'DEFAULT';
168         is($ipc->wq_workers_start('wq', 1), $$, 'workers started again');
169         is($ipc->wq_workers, 1, '1 worker started');
170         SKIP: {
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');
180         }
181         $ipc->wq_close;
182         is($ipc->wq_workers, undef, 'workers undef after close');
183 }
184
185 done_testing;