a9c306fab6077846f44b1afb7041f863d5c6c072
[p5sagit/Try-Tiny.git] / t / global_destruction_forked.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Try::Tiny;
5
6 {
7   package WithCatch;
8   use Try::Tiny;
9
10   sub DESTROY {
11     try {}
12     catch {};
13     return;
14   }
15 }
16
17 {
18   package WithFinally;
19   use Try::Tiny;
20
21   sub DESTROY {
22     try {}
23     finally {};
24     return;
25   }
26 }
27
28 my $parent = $$;
29
30 try {
31   my $pid = fork;
32   unless ($pid) {
33     my $o = bless {}, 'WithCatch';
34     $SIG{__DIE__} = sub {
35       exit 1
36         if $_[0] =~ /A try\(\) may not be followed by multiple catch\(\) blocks/;
37       exit 2;
38     };
39     exit 0;
40   }
41   waitpid $pid, 0;
42   is $?, 0, 'nested try in cleanup after fork does not maintain outer catch block';
43 }
44 catch {};
45
46 try {
47   my $pid = fork;
48   unless ($pid) {
49     my $o = bless {}, 'WithFinally';
50     exit 0;
51   }
52   waitpid $pid, 0;
53   is $?, 0, 'nested try in cleanup after fork does not maintain outer finally block';
54 }
55 finally { exit 1 if $parent != $$ };
56
57 done_testing;