]> Sergey Matveev's repositories - public-inbox.git/blob - t/on_destroy.t
www_coderepo: /tree/ redirects to /$OID/s/
[public-inbox.git] / t / on_destroy.t
1 #!perl -w
2 use v5.12;
3 use Test::More;
4 require_ok 'PublicInbox::OnDestroy';
5 my @x;
6 my $od = PublicInbox::OnDestroy->new(sub { push @x, 'hi' });
7 is_deeply(\@x, [], 'not called, yet');
8 undef $od;
9 is_deeply(\@x, [ 'hi' ], 'no args works');
10 $od = PublicInbox::OnDestroy->new(sub { $x[0] = $_[0] }, 'bye');
11 is_deeply(\@x, [ 'hi' ], 'nothing changed while alive');
12 undef $od;
13 is_deeply(\@x, [ 'bye' ], 'arg passed');
14 $od = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));
15 undef $od;
16 is_deeply(\@x, [ 'x', 'y' ], '2 args passed');
17
18 open my $tmp, '+>>', undef or BAIL_OUT $!;
19 $tmp->autoflush(1);
20 $od = PublicInbox::OnDestroy->new(1, sub { print $tmp "$$ DESTROY\n" });
21 undef $od;
22 is(-s $tmp, 0, '$tmp is empty on pid mismatch');
23 $od = PublicInbox::OnDestroy->new($$, sub { $tmp = $$ });
24 undef $od;
25 is($tmp, $$, '$tmp set to $$ by callback');
26
27 $od = PublicInbox::OnDestroy->new($$, sub { $tmp = 'foo' });
28 $od->cancel;
29 $od = undef;
30 isnt($tmp, 'foo', '->cancel');
31
32 if (my $nr = $ENV{TEST_LEAK_NR}) {
33         for (0..$nr) {
34                 $od = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));
35         }
36 }
37
38 done_testing;