Expand ASSERT_NO_SPURIOUS_EXCEPTION_ACTION to set a rogue $SIG{__DIE__}
[dbsrgits/DBIx-Class.git] / t / storage / txn_scope_guard.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
471a5fdd 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Warn;
8use Test::Exception;
37f94f8d 9
10use List::Util 'shuffle';
11use DBIx::Class::_Util 'sigwarn_silencer';
12
c0329273 13
471a5fdd 14use DBICTest;
15
16# Test txn_scope_guard
17{
18 my $schema = DBICTest->init_schema();
19
20 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
21 my $artist_rs = $schema->resultset('Artist');
22
23 my $fn = __FILE__;
24 throws_ok {
25 my $guard = $schema->txn_scope_guard;
26
27 $artist_rs->create({
28 name => 'Death Cab for Cutie',
29 made_up_column => 1,
30 });
31
32 $guard->commit;
e705f529 33 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
471a5fdd 34
35 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
36
37 my $inner_exception = ''; # set in inner() below
38 throws_ok (sub {
39 outer($schema, 1);
40 }, qr/$inner_exception/, "Nested exceptions propogated");
41
42 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
43
44 lives_ok (sub {
45
46 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
47 my $s = $schema;
48
49 warnings_exist ( sub {
50 # The 0 arg says don't die, just let the scope guard go out of scope
51 # forcing a txn_rollback to happen
52 outer($s, 0);
53 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
54
55 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
56
57 }, 'rollback successful withot exception');
58
59 sub outer {
60 my ($schema, $fatal) = @_;
61
62 my $guard = $schema->txn_scope_guard;
63 $schema->resultset('Artist')->create({
64 name => 'Death Cab for Cutie',
65 });
66 inner($schema, $fatal);
67 }
68
69 sub inner {
70 my ($schema, $fatal) = @_;
71
72 my $inner_guard = $schema->txn_scope_guard;
73 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
74
75 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
76
77 eval {
78 $artist->cds->create({
79 title => 'Plans',
80 year => 2005,
81 $fatal ? ( foo => 'bar' ) : ()
82 });
83 };
84 if ($@) {
85 # Record what got thrown so we can test it propgates out properly.
86 $inner_exception = $@;
87 die $@;
88 }
89
90 # inner guard should commit without consequences
91 $inner_guard->commit;
92 }
93}
94
95# make sure the guard does not eat exceptions
96{
97 my $schema = DBICTest->init_schema;
98
471a5fdd 99 no warnings 'redefine';
0d2033c3 100 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
87bf71d5 101 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
471a5fdd 102
103 throws_ok (sub {
104 my $guard = $schema->txn_scope_guard;
105 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
106
107 # this should freak out the guard rollback
108 # but it won't work because DBD::SQLite is buggy
109 # instead just install a toxic rollback above
110 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
111
112 die 'Deliberate exception';
750a4ad2 113 }, ( "$]" >= 5.013008 )
e69b5335 114 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
115 : qr/Deliberate exception.+Rollback failed/s
116 );
471a5fdd 117
118 # just to mask off warning since we could not disconnect above
119 $schema->storage->_dbh->disconnect;
120}
121
122# make sure it warns *big* on failed rollbacks
f62c5724 123# test with and without a poisoned $@
37f94f8d 124require DBICTest::AntiPattern::TrueZeroLen;
125require DBICTest::AntiPattern::NullObject;
126{
471a5fdd 127 my @want = (
128 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
129 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
130 );
131
132 my @w;
133 local $SIG{__WARN__} = sub {
134 if (grep {$_[0] =~ $_} (@want)) {
135 push @w, $_[0];
136 }
137 else {
138 warn $_[0];
139 }
140 };
6e102c8f 141
5c33c8be 142
143 # we are driving manually here, do not allow interference
144 local $SIG{__DIE__} if $SIG{__DIE__};
145
146
37f94f8d 147 no warnings 'redefine';
148 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
149 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
471a5fdd 150
37f94f8d 151 my @poisons = shuffle (
152 undef,
153 DBICTest::AntiPattern::TrueZeroLen->new,
154 DBICTest::AntiPattern::NullObject->new,
155 'GIFT!',
156 );
6e102c8f 157
37f94f8d 158 for my $pre_poison (@poisons) {
159 for my $post_poison (@poisons) {
471a5fdd 160
37f94f8d 161 @w = ();
162
163 my $schema = DBICTest->init_schema(no_populate => 1);
164
165 # the actual scope where the guard is created/freed
166 {
167 # in this particular case these are not the warnings we are looking for
168 local $SIG{__WARN__} = sigwarn_silencer qr/implementing the so called null-object-pattern/;
169
170 # if is inside the eval, to clear $@ in the undef case
171 eval { die $pre_poison if defined $pre_poison };
172
173 my $guard = $schema->txn_scope_guard;
174
175 eval { die $post_poison if defined $post_poison };
176
177 $schema->resultset ('Artist')->create ({ name => "bohhoo, too bad we'll roll you back"});
178 }
179
180 local $TODO = 'Do not know how to deal with trapped exceptions occuring after guard instantiation...'
181 if ( defined $post_poison and (
182 # take no chances on installation
183 DBICTest::RunMode->is_plain
184 or
185 # I do not understand why but on <= 5.8.8 and on 5.10.0
186 # "$pre_poison == $post_poison == string" passes...
187 # so todoify 5.8.9 and 5.10.1+, and deal with the rest below
188 ( ( "$]" > 5.008008 and "$]" < 5.010000 ) or "$]" > 5.010000 )
189 or
190 ! defined $pre_poison
191 or
192 length ref $pre_poison
193 or
194 length ref $post_poison
195 ));
196
197 is (@w, 2, sprintf 'Both expected warnings found - $@ poisonstate: pre-poison:%s post-poison:%s',
198 map {
199 ! defined $_ ? 'UNDEF'
200 : ! length ref $_ ? $_
201 : ref $_
202
203 } ($pre_poison, $post_poison)
204 );
205
206 # just to mask off warning since we could not disconnect above
207 $schema->storage->_dbh->disconnect;
208 }
209 }
210}
471a5fdd 211
153a6b38 212# add a TODO to catch when Text::Balanced is finally fixed
213# https://rt.cpan.org/Public/Bug/Display.html?id=74994
214#
215# while it doesn't matter much for DBIC itself, this particular bug
216# is a *BANE*, and DBIC is to bump its dep as soon as possible
217{
218
219 require Text::Balanced;
220
841efcb3 221 my @w;
222 local $SIG{__WARN__} = sub {
9bea2000 223 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
841efcb3 224 ? push @w, @_
225 : warn @_
226 };
153a6b38 227
841efcb3 228 lives_ok {
229 # this is what poisons $@
230 Text::Balanced::extract_bracketed( '(foo', '()' );
35cf7d1a 231 DBIx::Class::_Util::is_exception($@);
153a6b38 232
841efcb3 233 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
234 my $g = $s->txn_scope_guard;
235 $g->commit;
236 } 'Broken Text::Balanced is not screwing up txn_guard';
153a6b38 237
841efcb3 238 local $TODO = 'RT#74994 *STILL* not fixed';
239 is(scalar @w, 0, 'no warnings \o/');
153a6b38 240}
241
3d56e026 242# ensure Devel::StackTrace-refcapture-like effects are countered
243{
244 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
245 my $g = $s->txn_scope_guard;
246
247 my @arg_capture;
248 {
249 local $SIG{__WARN__} = sub {
250 package DB;
251 my $frnum;
821edc09 252 while (my @f = CORE::caller(++$frnum) ) {
3d56e026 253 push @arg_capture, @DB::args;
254 }
255 };
256
257 undef $g;
258 1;
259 }
260
261 warnings_exist
262 { @arg_capture = () }
263 qr/\QPreventing *MULTIPLE* DESTROY() invocations on DBIx::Class::Storage::TxnScopeGuard/
264 ;
265}
266
471a5fdd 267done_testing;