public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob e8fdf35e3f0db7a7a73ef296e86ec4d6bb541616 1163 bytes (raw)
$ git show HEAD:t/on_destroy.t	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
#!perl -w
use v5.12;
use Test::More;
use PublicInbox::OnDestroy;
use POSIX qw(_exit);
my @x;
my $od = on_destroy sub { push @x, 'hi' };
is_deeply(\@x, [], 'not called, yet');
undef $od;
is_deeply(\@x, [ 'hi' ], 'no args works');
$od = on_destroy sub { $x[0] = $_[0] }, 'bye';
is_deeply(\@x, [ 'hi' ], 'nothing changed while alive');
undef $od;
is_deeply(\@x, [ 'bye' ], 'arg passed');
$od = on_destroy sub { @x = @_ }, qw(x y);
undef $od;
is_deeply(\@x, [ 'x', 'y' ], '2 args passed');

open my $tmp, '+>>', undef or BAIL_OUT $!;
$tmp->autoflush(1);
$od = on_destroy sub { print $tmp "$$ DESTROY\n" };
my $pid = PublicInbox::OnDestroy::fork_tmp;
if ($pid == 0) { undef $od; _exit 0; };
waitpid($pid, 0);
is $?, 0, 'test process exited';
is(-s $tmp, 0, '$tmp is empty on pid mismatch');
$od->cancel;
undef $od;
is(-s $tmp, 0, '$tmp is empty after ->cancel');
$od = on_destroy sub { $tmp = $$ };
undef $od;
is($tmp, $$, '$tmp set to $$ by callback');

$od = on_destroy sub { $tmp = 'foo' };
$od->cancel;
$od = undef;
isnt($tmp, 'foo', '->cancel');

if (my $nr = $ENV{TEST_LEAK_NR}) {
	for (0..$nr) {
		$od = on_destroy sub { @x = @_ }, qw(x y);
	}
}

done_testing;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git