10 # Test txn_scope_guard
12 my $schema = DBICTest->init_schema();
14 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
15 my $artist_rs = $schema->resultset('Artist');
19 my $guard = $schema->txn_scope_guard;
22 name => 'Death Cab for Cutie',
27 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
29 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
31 my $inner_exception = ''; # set in inner() below
34 }, qr/$inner_exception/, "Nested exceptions propogated");
36 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
40 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
43 warnings_exist ( sub {
44 # The 0 arg says don't die, just let the scope guard go out of scope
45 # forcing a txn_rollback to happen
47 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
49 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
51 }, 'rollback successful withot exception');
54 my ($schema, $fatal) = @_;
56 my $guard = $schema->txn_scope_guard;
57 $schema->resultset('Artist')->create({
58 name => 'Death Cab for Cutie',
60 inner($schema, $fatal);
64 my ($schema, $fatal) = @_;
66 my $inner_guard = $schema->txn_scope_guard;
67 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
69 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
72 $artist->cds->create({
75 $fatal ? ( foo => 'bar' ) : ()
79 # Record what got thrown so we can test it propgates out properly.
80 $inner_exception = $@;
84 # inner guard should commit without consequences
89 # make sure the guard does not eat exceptions
91 my $schema = DBICTest->init_schema;
93 no warnings 'redefine';
94 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
95 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
98 my $guard = $schema->txn_scope_guard;
99 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
101 # this should freak out the guard rollback
102 # but it won't work because DBD::SQLite is buggy
103 # instead just install a toxic rollback above
104 #$schema->storage->_dbh( $schema->storage->_dbh->clone );
106 die 'Deliberate exception';
107 }, ( "$]" >= 5.013008 )
108 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
109 : qr/Deliberate exception.+Rollback failed/s
112 # just to mask off warning since we could not disconnect above
113 $schema->storage->_dbh->disconnect;
116 # make sure it warns *big* on failed rollbacks
117 # test with and without a poisoned $@
118 for my $pre_poison (0,1) {
119 for my $post_poison (0,1) {
121 my $schema = DBICTest->init_schema(no_populate => 1);
123 no warnings 'redefine';
124 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
125 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
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 eval { die 'pre-GIFT!' if $pre_poison };
144 my $guard = $schema->txn_scope_guard;
145 eval { die 'post-GIFT!' if $post_poison };
146 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
149 local $TODO = 'Do not know how to deal with trapped exceptions occuring after guard instantiation...'
150 if ( $post_poison and (
151 # take no chances on installation
152 DBICTest::RunMode->is_plain
157 # I do not understand why but on <= 5.8.8 and on 5.10.0 "$pre_poison && $post_poison" passes...
158 ( "$]" > 5.008008 and "$]" < 5.010000 ) or "$]" > 5.010000
161 is (@w, 2, "Both expected warnings found - \$\@ pre-poison: $pre_poison, post-poison: $post_poison" );
163 # just to mask off warning since we could not disconnect above
164 $schema->storage->_dbh->disconnect;
167 # add a TODO to catch when Text::Balanced is finally fixed
168 # https://rt.cpan.org/Public/Bug/Display.html?id=74994
170 # while it doesn't matter much for DBIC itself, this particular bug
171 # is a *BANE*, and DBIC is to bump its dep as soon as possible
174 require Text::Balanced;
177 local $SIG{__WARN__} = sub {
178 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
184 # this is what poisons $@
185 Text::Balanced::extract_bracketed( '(foo', '()' );
186 DBIx::Class::_Util::is_exception($@);
188 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
189 my $g = $s->txn_scope_guard;
191 } 'Broken Text::Balanced is not screwing up txn_guard';
193 local $TODO = 'RT#74994 *STILL* not fixed';
194 is(scalar @w, 0, 'no warnings \o/');
197 # ensure Devel::StackTrace-refcapture-like effects are countered
199 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
200 my $g = $s->txn_scope_guard;
204 local $SIG{__WARN__} = sub {
207 while (my @f = CORE::caller(++$frnum) ) {
208 push @arg_capture, @DB::args;
217 { @arg_capture = () }
218 qr/\QPreventing *MULTIPLE* DESTROY() invocations on DBIx::Class::Storage::TxnScopeGuard/