lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / on_destroy.t
blobe8fdf35e3f0db7a7a73ef296e86ec4d6bb541616
1 #!perl -w
2 use v5.12;
3 use Test::More;
4 use PublicInbox::OnDestroy;
5 use POSIX qw(_exit);
6 my @x;
7 my $od = on_destroy sub { push @x, 'hi' };
8 is_deeply(\@x, [], 'not called, yet');
9 undef $od;
10 is_deeply(\@x, [ 'hi' ], 'no args works');
11 $od = on_destroy sub { $x[0] = $_[0] }, 'bye';
12 is_deeply(\@x, [ 'hi' ], 'nothing changed while alive');
13 undef $od;
14 is_deeply(\@x, [ 'bye' ], 'arg passed');
15 $od = on_destroy sub { @x = @_ }, qw(x y);
16 undef $od;
17 is_deeply(\@x, [ 'x', 'y' ], '2 args passed');
19 open my $tmp, '+>>', undef or BAIL_OUT $!;
20 $tmp->autoflush(1);
21 $od = on_destroy sub { print $tmp "$$ DESTROY\n" };
22 my $pid = PublicInbox::OnDestroy::fork_tmp;
23 if ($pid == 0) { undef $od; _exit 0; };
24 waitpid($pid, 0);
25 is $?, 0, 'test process exited';
26 is(-s $tmp, 0, '$tmp is empty on pid mismatch');
27 $od->cancel;
28 undef $od;
29 is(-s $tmp, 0, '$tmp is empty after ->cancel');
30 $od = on_destroy sub { $tmp = $$ };
31 undef $od;
32 is($tmp, $$, '$tmp set to $$ by callback');
34 $od = on_destroy sub { $tmp = 'foo' };
35 $od->cancel;
36 $od = undef;
37 isnt($tmp, 'foo', '->cancel');
39 if (my $nr = $ENV{TEST_LEAK_NR}) {
40         for (0..$nr) {
41                 $od = on_destroy sub { @x = @_ }, qw(x y);
42         }
45 done_testing;