X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fipc.t;h=ce89f94bddc0d3daaa541f3b54aaae8545dd2cb0;hb=refs%2Fheads%2Fmaster;hp=face572632a5be27971ece8949cc08b2782d6a17;hpb=2d610f0e645fd2b01bf9108b2d06022ab730815c;p=public-inbox.git diff --git a/t/ipc.t b/t/ipc.t index face5726..fd4d5599 100644 --- a/t/ipc.t +++ b/t/ipc.t @@ -1,18 +1,19 @@ #!perl -w -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ use strict; use v5.10.1; use Test::More; use PublicInbox::TestCommon; use Fcntl qw(SEEK_SET); -use Digest::SHA qw(sha1_hex); +use PublicInbox::SHA qw(sha1_hex); require_mods(qw(Storable||Sereal)); require_ok 'PublicInbox::IPC'; +my ($tmpdir, $for_destroy) = tmpdir(); state $once = eval <<''; package PublicInbox::IPC; use strict; -use Digest::SHA qw(sha1_hex); +use PublicInbox::SHA qw(sha1_hex); sub test_array { qw(test array) } sub test_scalar { 'scalar' } sub test_scalarref { \'scalarref' } @@ -31,6 +32,12 @@ sub test_sha { print { $self->{1} } sha1_hex($buf), "\n"; $self->{1}->flush; } +sub test_append_pid { + my ($self, $file) = @_; + open my $fh, '>>', $file or die "open: $!"; + $fh->autoflush(1); + print $fh "$$\n" or die "print: $!"; +} 1; my $ipc = bless {}, 'PublicInbox::IPC'; @@ -83,11 +90,8 @@ $test->('local'); defined($pid) or BAIL_OUT 'no spawn, no test'; is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned'); $test->('worker'); - { - my ($tmp, $for_destroy) = tmpdir(); - $ipc->ipc_lock_init("$tmp/lock"); - is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned'); - } + $ipc->ipc_lock_init("$tmpdir/lock"); + is($ipc->ipc_do('test_pid'), $pid, 'worker pid returned'); $ipc->ipc_worker_stop; ok(!kill(0, $pid) && $!{ESRCH}, 'worker stopped'); } @@ -106,7 +110,7 @@ my $big = do { local $/; <$agpl> } // BAIL_OUT "read: $!"; close $agpl or BAIL_OUT "close: $!"; for my $t ('local', 'worker', 'worker again') { - $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world'); + $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], 'hello world'); my $i = 0; for my $fh ($ra, $rb, $rc) { my $buf = readline($fh); @@ -114,21 +118,26 @@ for my $t ('local', 'worker', 'worker again') { like($buf, qr/\Ai=$i \d+ hello world\z/, "got expected ($t)"); $i++; } - $ipc->wq_do('test_die', [ $wa, $wb, $wc ]); - $ipc->wq_do('test_sha', [ $wa, $wb ], 'hello world'); + $ipc->wq_io_do('test_die', [ $wa, $wb, $wc ]); + $ipc->wq_io_do('test_sha', [ $wa, $wb ], 'hello world'); is(readline($rb), sha1_hex('hello world')."\n", "SHA small ($t)"); { - my $bigger = $big x 10; - $ipc->wq_do('test_sha', [ $wa, $wb ], $bigger); + my $bigger = $big x 10; # to hit EMSGSIZE + $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger); my $exp = sha1_hex($bigger)."\n"; - undef $bigger; - is(readline($rb), $exp, "SHA big ($t)"); + is(readline($rb), $exp, "SHA big for EMSGSIZE ($t)"); + + # to hit the WQWorker recv_and_run length + substr($bigger, my $MY_MAX_ARG_STRLEN = 4096 * 33, -1) = ''; + $ipc->wq_io_do('test_sha', [ $wa, $wb ], $bigger); + $exp = sha1_hex($bigger)."\n"; + is(readline($rb), $exp, "SHA WQWorker limit ($t)"); } my $ppid = $ipc->wq_workers_start('wq', 1); push(@ppids, $ppid); } -# wq_do works across fork (siblings can feed) +# wq_io_do works across fork (siblings can feed) SKIP: { skip 'Socket::MsgHdr or Inline::C missing', 3 if !$ppids[0]; is_deeply(\@ppids, [$$, undef, undef], @@ -136,7 +145,7 @@ SKIP: { my $pid = fork // BAIL_OUT $!; if ($pid == 0) { use POSIX qw(_exit); - $ipc->wq_do('test_write_each_fd', [ $wa, $wb, $wc ], $$); + $ipc->wq_io_do('test_write_each_fd', [ $wa, $wb, $wc ], $$); _exit(0); } else { my $i = 0; @@ -152,6 +161,12 @@ SKIP: { is(waitpid($pid, 0), $pid, 'waitpid complete'); is($?, 0, 'child wq producer exited'); } + my @ary = $ipc->wq_do('test_array'); + is_deeply(\@ary, [ qw(test array) ], 'wq_do wantarray'); + is(my $s = $ipc->wq_do('test_scalar'), 'scalar', 'defined wantarray'); + my $exp = bless ['blessed'], 'PublicInbox::WTF'; + my $ret = eval { $ipc->wq_do('test_die', $exp) }; + is_deeply($@, $exp, 'die with blessed ref'); } $ipc->wq_close; @@ -160,26 +175,18 @@ SKIP: { seek($warn, 0, SEEK_SET) or BAIL_OUT; my @warn = <$warn>; is(scalar(@warn), 3, 'warned 3 times'); - like($warn[0], qr/ wq_do: /, '1st warned from wq_do'); + like($warn[0], qr/ wq_io_do: /, '1st warned from wq_do'); like($warn[1], qr/ wq_worker: /, '2nd warned from wq_worker'); is($warn[2], $warn[1], 'worker did not die'); $SIG{__WARN__} = 'DEFAULT'; - is($ipc->wq_workers_start('wq', 1), $$, 'workers started again'); - is($ipc->wq_workers, 1, '1 worker started'); - SKIP: { - $ipc->WQ_MAX_WORKERS > 1 or - skip 'Inline::C or Socket::MsgHdr not available', 4; - $ipc->wq_worker_incr; - is($ipc->wq_workers, 2, 'worker count bumped'); - $ipc->wq_worker_decr; - $ipc->wq_worker_decr_wait(10); - is($ipc->wq_workers, 1, 'worker count lowered'); - is($ipc->wq_workers(2), 2, 'worker count set'); - is($ipc->wq_workers, 2, 'worker count stayed set'); - } + is($ipc->wq_workers_start('wq', 2), $$, 'workers started again'); + $ipc->wq_broadcast('test_append_pid', "$tmpdir/append_pid"); $ipc->wq_close; - is($ipc->wq_workers, undef, 'workers undef after close'); + open my $fh, '<', "$tmpdir/append_pid" or BAIL_OUT "open: $!"; + chomp(my @pids = <$fh>); + my %pids = map { $_ => 1 } grep(/\A[0-9]+\z/, @pids); + is(scalar keys %pids, 2, 'broadcast hit both PIDs'); } done_testing;