1 # Test is sufficiently involved to *want* to run with "maximum paranoia"
2 BEGIN { $ENV{DBICTEST_OLD_MRO_SANITY_CHECK_ASSERTIONS} = 1 }
4 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
13 use List::Util 'shuffle';
14 use DBIx::Class::_Util 'sigwarn_silencer';
19 # Test txn_scope_guard
21 my $schema = DBICTest->init_schema();
23 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
24 my $artist_rs = $schema->resultset('Artist');
28 my $guard = $schema->txn_scope_guard;
31 name => 'Death Cab for Cutie',
36 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
38 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
40 my $inner_exception = ''; # set in inner() below
43 }, qr/$inner_exception/, "Nested exceptions propogated");
45 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
49 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
52 warnings_exist ( sub {
53 # The 0 arg says don't die, just let the scope guard go out of scope
54 # forcing a txn_rollback to happen
56 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
58 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
60 }, 'rollback successful withot exception');
63 my ($schema, $fatal) = @_;
65 my $guard = $schema->txn_scope_guard;
66 $schema->resultset('Artist')->create({
67 name => 'Death Cab for Cutie',
69 inner($schema, $fatal);
73 my ($schema, $fatal) = @_;
75 my $inner_guard = $schema->txn_scope_guard;
76 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
78 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
81 $artist->cds->create({
84 $fatal ? ( foo => 'bar' ) : ()
88 # Record what got thrown so we can test it propgates out properly.
89 $inner_exception = $@;
93 # inner guard should commit without consequences
98 # make sure the guard does not eat exceptions
100 my $schema = DBICTest->init_schema;
102 no warnings 'redefine';
103 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
104 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
107 my $guard = $schema->txn_scope_guard;
108 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
110 # this should freak out the guard rollback
111 # but it won't work because DBD::SQLite is buggy
112 # instead just install a toxic rollback above
113 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
115 die 'Deliberate exception';
116 }, ( "$]" >= 5.013008 )
117 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
118 : qr/Deliberate exception.+Rollback failed/s
121 # just to mask off warning since we could not disconnect above
122 $schema->storage->_dbh->disconnect;
125 # make sure it warns *big* on failed rollbacks
126 # test with and without a poisoned $@
127 require DBICTest::AntiPattern::TrueZeroLen;
128 require DBICTest::AntiPattern::NullObject;
131 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
132 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
136 local $SIG{__WARN__} = sub {
137 if (grep {$_[0] =~ $_} (@want)) {
146 # we are driving manually here, do not allow interference
147 local $SIG{__DIE__} if $SIG{__DIE__};
150 no warnings 'redefine';
151 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
152 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
154 my @poisons = shuffle (
156 DBICTest::AntiPattern::TrueZeroLen->new,
157 DBICTest::AntiPattern::NullObject->new,
161 for my $pre_poison (@poisons) {
162 for my $post_poison (@poisons) {
166 my $schema = DBICTest->init_schema(no_populate => 1);
168 # the actual scope where the guard is created/freed
170 # in this particular case these are not the warnings we are looking for
171 local $SIG{__WARN__} = sigwarn_silencer qr/implementing the so called null-object-pattern/;
173 # if is inside the eval, to clear $@ in the undef case
174 eval { die $pre_poison if defined $pre_poison };
176 my $guard = $schema->txn_scope_guard;
178 eval { die $post_poison if defined $post_poison };
180 $schema->resultset ('Artist')->create ({ name => "bohhoo, too bad we'll roll you back"});
183 local $TODO = 'Do not know how to deal with trapped exceptions occuring after guard instantiation...'
184 if ( defined $post_poison and (
185 # take no chances on installation
186 DBICTest::RunMode->is_plain
188 # I do not understand why but on <= 5.8.8 and on 5.10.0
189 # "$pre_poison == $post_poison == string" passes...
190 # so todoify 5.8.9 and 5.10.1+, and deal with the rest below
191 ( ( "$]" > 5.008008 and "$]" < 5.010000 ) or "$]" > 5.010000 )
193 ! defined $pre_poison
195 length ref $pre_poison
197 length ref $post_poison
200 is (@w, 2, sprintf 'Both expected warnings found - $@ poisonstate: pre-poison:%s post-poison:%s',
202 ! defined $_ ? 'UNDEF'
203 : ! length ref $_ ? $_
206 } ($pre_poison, $post_poison)
209 # just to mask off warning since we could not disconnect above
210 $schema->storage->_dbh->disconnect;
215 # add a TODO to catch when Text::Balanced is finally fixed
216 # https://rt.cpan.org/Public/Bug/Display.html?id=74994
218 # while it doesn't matter much for DBIC itself, this particular bug
219 # is a *BANE*, and DBIC is to bump its dep as soon as possible
222 require Text::Balanced;
225 local $SIG{__WARN__} = sub {
226 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
232 # this is what poisons $@
233 Text::Balanced::extract_bracketed( '(foo', '()' );
234 DBIx::Class::_Util::is_exception($@);
236 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
237 my $g = $s->txn_scope_guard;
239 } 'Broken Text::Balanced is not screwing up txn_guard';
241 local $TODO = 'RT#74994 *STILL* not fixed';
242 is(scalar @w, 0, 'no warnings \o/');