]> Sergey Matveev's repositories - public-inbox.git/blob - t/on_destroy.t
No ext_urls
[public-inbox.git] / t / on_destroy.t
1 #!perl -w
2 use strict;
3 use v5.10.1;
4 use Test::More;
5 require_ok 'PublicInbox::OnDestroy';
6 my @x;
7 my $od = PublicInbox::OnDestroy->new(sub { push @x, 'hi' });
8 is_deeply(\@x, [], 'not called, yet');
9 undef $od;
10 is_deeply(\@x, [ 'hi' ], 'no args works');
11 $od = PublicInbox::OnDestroy->new(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 = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));
16 undef $od;
17 is_deeply(\@x, [ 'x', 'y' ], '2 args passed');
18
19 open my $tmp, '+>>', undef or BAIL_OUT $!;
20 $tmp->autoflush(1);
21 $od = PublicInbox::OnDestroy->new(1, sub { print $tmp "$$ DESTROY\n" });
22 undef $od;
23 is(-s $tmp, 0, '$tmp is empty on pid mismatch');
24 $od = PublicInbox::OnDestroy->new($$, sub { $tmp = $$ });
25 undef $od;
26 is($tmp, $$, '$tmp set to $$ by callback');
27
28 if (my $nr = $ENV{TEST_LEAK_NR}) {
29         for (0..$nr) {
30                 $od = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));
31         }
32 }
33
34 done_testing;