Here goes nothing
[p5sagit/Exception-Guaranteed.git] / t / 01basic.t
CommitLineData
3233f09d 1use warnings;
2use strict;
3
4use Test::More;
5use Exception::Guaranteed;
6
7use lib 't';
8use __SelfDestruct;
9
10eval {
11 guarantee_exception { die "Simple exception" }
12};
13like( $@, qr/^Simple exception/, 'A plain exception shoots through' );
14
15my $dummy = 0;
16my $fail = 0;
17eval {
18 guarantee_exception {
19 __SelfDestruct->spawn_n_kill(sub {
20 die 'Exception outer';
21 });
22 };
23
24 while( $dummy < 2**31) {
25 $dummy++;
26 }
27
28 $fail = 1; # we should never reach here
29};
30print STDERR "\n";
31diag( ($dummy||0) . " inc-ops executed before kill-signal delivery (outer g_e)\n" );
32ok (!$fail, 'execution stopped after trappable destroy exception');
33like( $@, qr/^Exception outer/, 'DESTROY exception thrown and caught from outside' );
34
35$fail = 0;
36# when using the fork+signal based approach, I can't make the exception
37# happen fast enough to not shoot out of its real containing eval :(
38# Hence the dummy count here is essential
39$dummy = 0;
40eval {
41 __SelfDestruct->spawn_n_kill( sub {
42 guarantee_exception {
43 die 'Exception inner';
44 };
45 });
46
47 while( $dummy < 2**31) {
48 $dummy++;
49 }
50
51 $fail = 1; # we should never reach here
52};
53
54diag( ($dummy||0) . " inc-ops executed before kill-signal delivery (DESTROY g_e)\n" );
55ok (!$fail, 'execution stopped after trappable destroy exception');
56like( $@, qr/^Exception inner/, 'DESTROY exception thrown and caught from inside of DESTROY block' );
57
58# important, for the thread re-test
59if ($ENV{EXCEPTION_GUARANTEED_SUBTEST}) {
60 $ENV{EXCEPTION_GUARANTEED_SUBTEST} = 42;
61 0; # like an exit(0)
62}
63else {
64 done_testing;
65}