Abstract our internal capture_stderr test routine
[dbsrgits/DBIx-Class.git] / t / 35exception_inaction.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
7cb35852 3use strict;
4use warnings;
5
c0329273 6
7cb35852 7use DBICTest::RunMode;
8BEGIN {
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
20c0d57b 15use DBICTest::Util 'capture_stderr';
7cb35852 16use DBIx::Class::Schema;
17
18# Do not use T::B - the test is hard enough not to segfault as it is
19my $test_count = 0;
20
21# start with one failure, and decrement it at the end
22my $failed = 1;
23
24sub 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
7cb35852 42
43# this is incredibly horrible...
44# demonstrate utter breakage of the reconnection/retry logic
45#
20c0d57b 46my $output = capture_stderr {
7cb35852 47ESCAPE:
48{
20c0d57b 49 # yes, make it even dirtier
50 my $schema = 'DBIx::Class::Schema';
7cb35852 51
20c0d57b 52 $schema->connection('dbi:SQLite::memory:');
7cb35852 53 $schema->storage->ensure_connected;
54 $schema->storage->_dbh->disconnect;
55
c40b5744 56 # silences "exitting sub via last"
7cb35852 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");
20c0d57b 72}};
7cb35852 73
74ok(1, "Post-escape reached");
75
76ok(
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
81print "1..$test_count\n";
82
83# this is our "done_testing"
84$failed--;
85
86# avoid tasty segfaults on 5.8.x
87exit( $failed );