File: on_destroy.t

package info (click to toggle)
public-inbox 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,152 kB
  • sloc: perl: 52,771; sh: 302; ansic: 106; makefile: 37
file content (34 lines) | stat: -rw-r--r-- 987 bytes parent folder | download
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
#!perl -w
use strict;
use v5.10.1;
use Test::More;
require_ok 'PublicInbox::OnDestroy';
my @x;
my $od = PublicInbox::OnDestroy->new(sub { push @x, 'hi' });
is_deeply(\@x, [], 'not called, yet');
undef $od;
is_deeply(\@x, [ 'hi' ], 'no args works');
$od = PublicInbox::OnDestroy->new(sub { $x[0] = $_[0] }, 'bye');
is_deeply(\@x, [ 'hi' ], 'nothing changed while alive');
undef $od;
is_deeply(\@x, [ 'bye' ], 'arg passed');
$od = PublicInbox::OnDestroy->new(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 = PublicInbox::OnDestroy->new(1, sub { print $tmp "$$ DESTROY\n" });
undef $od;
is(-s $tmp, 0, '$tmp is empty on pid mismatch');
$od = PublicInbox::OnDestroy->new($$, sub { $tmp = $$ });
undef $od;
is($tmp, $$, '$tmp set to $$ by callback');

if (my $nr = $ENV{TEST_LEAK_NR}) {
	for (0..$nr) {
		$od = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));
	}
}

done_testing;