Abstract our internal capture_stderr test routine
[dbsrgits/DBIx-Class.git] / t / 35exception_inaction.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6
7 use DBICTest::RunMode;
8 BEGIN {
9   if( DBICTest::RunMode->is_plain ) {
10     print "1..0 # SKIP not running dangerous segfault-prone test on plain install\n";
11     exit 0;
12   }
13 }
14
15 use DBICTest::Util 'capture_stderr';
16 use DBIx::Class::Schema;
17
18 # Do not use T::B - the test is hard enough not to segfault as it is
19 my $test_count = 0;
20
21 # start with one failure, and decrement it at the end
22 my $failed = 1;
23
24 sub ok {
25   printf STDOUT ("%s %u - %s\n",
26     ( $_[0] ? 'ok' : 'not ok' ),
27     ++$test_count,
28     $_[1] || '',
29   );
30
31   unless( $_[0] ) {
32     $failed++;
33     printf STDERR ("# Failed test #%d at %s line %d\n",
34       $test_count,
35       (caller(0))[1,2]
36     );
37   }
38
39   return !!$_[0];
40 }
41
42
43 # this is incredibly horrible...
44 # demonstrate utter breakage of the reconnection/retry logic
45 #
46 my $output = capture_stderr {
47 ESCAPE:
48 {
49   # yes, make it even dirtier
50   my $schema = 'DBIx::Class::Schema';
51
52   $schema->connection('dbi:SQLite::memory:');
53   $schema->storage->ensure_connected;
54   $schema->storage->_dbh->disconnect;
55
56   # silences "exitting sub via last"
57   local $SIG{__WARN__} = sub {};
58
59   $schema->exception_action(sub {
60     ok(1, 'exception_action invoked');
61     # essentially what Dancer2's redirect() does after https://github.com/PerlDancer/Dancer2/pull/485
62     # which "nicely" combines with: https://metacpan.org/source/MARKOV/Log-Report-1.12/lib/Dancer2/Plugin/LogReport.pm#L143
63     # as encouraged by: https://metacpan.org/pod/release/MARKOV/Log-Report-1.12/lib/Dancer2/Plugin/LogReport.pod#Logging-DBIC-database-queries-and-errors
64     last ESCAPE;
65   });
66
67   # this *DOES* throw, but the exception will *NEVER SHOW UP*
68   $schema->storage->dbh_do(sub { $_[1]->selectall_arrayref("SELECT * FROM wfwqfdqefqef") } );
69
70   # NEITHER will this
71   ok(0, "Nope");
72 }};
73
74 ok(1, "Post-escape reached");
75
76 ok(
77   !!( $output =~ /DBIx::Class INTERNAL PANIC.+FIX YOUR ERROR HANDLING/s ),
78   'Proper warning emitted on STDERR'
79 ) or print STDERR "Instead found:\n\n$output\n";
80
81 print "1..$test_count\n";
82
83 # this is our "done_testing"
84 $failed--;
85
86 # avoid tasty segfaults on 5.8.x
87 exit( $failed );