1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
10 use List::Util 'shuffle';
11 use DBIx::Class::_Util 'sigwarn_silencer';
16 # Test txn_scope_guard
18 my $schema = DBICTest->init_schema();
20 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
21 my $artist_rs = $schema->resultset('Artist');
25 my $guard = $schema->txn_scope_guard;
28 name => 'Death Cab for Cutie',
33 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
35 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
37 my $inner_exception = ''; # set in inner() below
40 }, qr/$inner_exception/, "Nested exceptions propogated");
42 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
46 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
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
53 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
55 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
57 }, 'rollback successful withot exception');
60 my ($schema, $fatal) = @_;
62 my $guard = $schema->txn_scope_guard;
63 $schema->resultset('Artist')->create({
64 name => 'Death Cab for Cutie',
66 inner($schema, $fatal);
70 my ($schema, $fatal) = @_;
72 my $inner_guard = $schema->txn_scope_guard;
73 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
75 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
78 $artist->cds->create({
81 $fatal ? ( foo => 'bar' ) : ()
85 # Record what got thrown so we can test it propgates out properly.
86 $inner_exception = $@;
90 # inner guard should commit without consequences
95 # make sure the guard does not eat exceptions
97 my $schema = DBICTest->init_schema;
99 no warnings 'redefine';
100 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
101 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
104 my $guard = $schema->txn_scope_guard;
105 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
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 );
112 die 'Deliberate exception';
113 }, ( "$]" >= 5.013008 )
114 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
115 : qr/Deliberate exception.+Rollback failed/s
118 # just to mask off warning since we could not disconnect above
119 $schema->storage->_dbh->disconnect;
122 # make sure it warns *big* on failed rollbacks
123 # test with and without a poisoned $@
124 require DBICTest::AntiPattern::TrueZeroLen;
125 require DBICTest::AntiPattern::NullObject;
128 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
129 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
133 local $SIG{__WARN__} = sub {
134 if (grep {$_[0] =~ $_} (@want)) {
143 # we are driving manually here, do not allow interference
144 local $SIG{__DIE__} if $SIG{__DIE__};
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;
151 my @poisons = shuffle (
153 DBICTest::AntiPattern::TrueZeroLen->new,
154 DBICTest::AntiPattern::NullObject->new,
158 for my $pre_poison (@poisons) {
159 for my $post_poison (@poisons) {
163 my $schema = DBICTest->init_schema(no_populate => 1);
165 # the actual scope where the guard is created/freed
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/;
170 # if is inside the eval, to clear $@ in the undef case
171 eval { die $pre_poison if defined $pre_poison };
173 my $guard = $schema->txn_scope_guard;
175 eval { die $post_poison if defined $post_poison };
177 $schema->resultset ('Artist')->create ({ name => "bohhoo, too bad we'll roll you back"});
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
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 )
190 ! defined $pre_poison
192 length ref $pre_poison
194 length ref $post_poison
197 is (@w, 2, sprintf 'Both expected warnings found - $@ poisonstate: pre-poison:%s post-poison:%s',
199 ! defined $_ ? 'UNDEF'
200 : ! length ref $_ ? $_
203 } ($pre_poison, $post_poison)
206 # just to mask off warning since we could not disconnect above
207 $schema->storage->_dbh->disconnect;
212 # add a TODO to catch when Text::Balanced is finally fixed
213 # https://rt.cpan.org/Public/Bug/Display.html?id=74994
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
219 require Text::Balanced;
222 local $SIG{__WARN__} = sub {
223 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
229 # this is what poisons $@
230 Text::Balanced::extract_bracketed( '(foo', '()' );
231 DBIx::Class::_Util::is_exception($@);
233 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
234 my $g = $s->txn_scope_guard;
236 } 'Broken Text::Balanced is not screwing up txn_guard';
238 local $TODO = 'RT#74994 *STILL* not fixed';
239 is(scalar @w, 0, 'no warnings \o/');
242 # ensure Devel::StackTrace-refcapture-like effects are countered
244 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
245 my $g = $s->txn_scope_guard;
249 local $SIG{__WARN__} = sub {
252 while (my @f = CORE::caller(++$frnum) ) {
253 push @arg_capture, @DB::args;
262 { @arg_capture = () }
263 qr/\QPreventing *MULTIPLE* DESTROY() invocations on DBIx::Class::Storage::TxnScopeGuard/