8 use List::Util 'shuffle';
9 use DBIx::Class::_Util 'sigwarn_silencer';
14 # Test txn_scope_guard
16 my $schema = DBICTest->init_schema();
18 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
19 my $artist_rs = $schema->resultset('Artist');
23 my $guard = $schema->txn_scope_guard;
26 name => 'Death Cab for Cutie',
31 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
33 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
35 my $inner_exception = ''; # set in inner() below
38 }, qr/$inner_exception/, "Nested exceptions propogated");
40 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
44 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
47 warnings_exist ( sub {
48 # The 0 arg says don't die, just let the scope guard go out of scope
49 # forcing a txn_rollback to happen
51 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
53 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
55 }, 'rollback successful withot exception');
58 my ($schema, $fatal) = @_;
60 my $guard = $schema->txn_scope_guard;
61 $schema->resultset('Artist')->create({
62 name => 'Death Cab for Cutie',
64 inner($schema, $fatal);
68 my ($schema, $fatal) = @_;
70 my $inner_guard = $schema->txn_scope_guard;
71 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
73 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
76 $artist->cds->create({
79 $fatal ? ( foo => 'bar' ) : ()
83 # Record what got thrown so we can test it propgates out properly.
84 $inner_exception = $@;
88 # inner guard should commit without consequences
93 # make sure the guard does not eat exceptions
95 my $schema = DBICTest->init_schema;
97 no warnings 'redefine';
98 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
99 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
102 my $guard = $schema->txn_scope_guard;
103 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
105 # this should freak out the guard rollback
106 # but it won't work because DBD::SQLite is buggy
107 # instead just install a toxic rollback above
108 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
110 die 'Deliberate exception';
111 }, ( "$]" >= 5.013008 )
112 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
113 : qr/Deliberate exception.+Rollback failed/s
116 # just to mask off warning since we could not disconnect above
117 $schema->storage->_dbh->disconnect;
120 # make sure it warns *big* on failed rollbacks
121 # test with and without a poisoned $@
122 require DBICTest::AntiPattern::TrueZeroLen;
123 require DBICTest::AntiPattern::NullObject;
126 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
127 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
131 local $SIG{__WARN__} = sub {
132 if (grep {$_[0] =~ $_} (@want)) {
140 no warnings 'redefine';
141 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
142 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
144 my @poisons = shuffle (
146 DBICTest::AntiPattern::TrueZeroLen->new,
147 DBICTest::AntiPattern::NullObject->new,
151 for my $pre_poison (@poisons) {
152 for my $post_poison (@poisons) {
156 my $schema = DBICTest->init_schema(no_populate => 1);
158 # the actual scope where the guard is created/freed
160 # in this particular case these are not the warnings we are looking for
161 local $SIG{__WARN__} = sigwarn_silencer qr/implementing the so called null-object-pattern/;
163 # if is inside the eval, to clear $@ in the undef case
164 eval { die $pre_poison if defined $pre_poison };
166 my $guard = $schema->txn_scope_guard;
168 eval { die $post_poison if defined $post_poison };
170 $schema->resultset ('Artist')->create ({ name => "bohhoo, too bad we'll roll you back"});
173 local $TODO = 'Do not know how to deal with trapped exceptions occuring after guard instantiation...'
174 if ( defined $post_poison and (
175 # take no chances on installation
176 DBICTest::RunMode->is_plain
178 # I do not understand why but on <= 5.8.8 and on 5.10.0
179 # "$pre_poison == $post_poison == string" passes...
180 # so todoify 5.8.9 and 5.10.1+, and deal with the rest below
181 ( ( "$]" > 5.008008 and "$]" < 5.010000 ) or "$]" > 5.010000 )
183 ! defined $pre_poison
185 length ref $pre_poison
187 length ref $post_poison
190 is (@w, 2, sprintf 'Both expected warnings found - $@ poisonstate: pre-poison:%s post-poison:%s',
192 ! defined $_ ? 'UNDEF'
193 : ! length ref $_ ? $_
196 } ($pre_poison, $post_poison)
199 # just to mask off warning since we could not disconnect above
200 $schema->storage->_dbh->disconnect;
205 # add a TODO to catch when Text::Balanced is finally fixed
206 # https://rt.cpan.org/Public/Bug/Display.html?id=74994
208 # while it doesn't matter much for DBIC itself, this particular bug
209 # is a *BANE*, and DBIC is to bump its dep as soon as possible
212 require Text::Balanced;
215 local $SIG{__WARN__} = sub {
216 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
222 # this is what poisons $@
223 Text::Balanced::extract_bracketed( '(foo', '()' );
224 DBIx::Class::_Util::is_exception($@);
226 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
227 my $g = $s->txn_scope_guard;
229 } 'Broken Text::Balanced is not screwing up txn_guard';
231 local $TODO = 'RT#74994 *STILL* not fixed';
232 is(scalar @w, 0, 'no warnings \o/');
235 # ensure Devel::StackTrace-refcapture-like effects are countered
237 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
238 my $g = $s->txn_scope_guard;
242 local $SIG{__WARN__} = sub {
245 while (my @f = CORE::caller(++$frnum) ) {
246 push @arg_capture, @DB::args;
255 { @arg_capture = () }
256 qr/\QPreventing *MULTIPLE* DESTROY() invocations on DBIx::Class::Storage::TxnScopeGuard/