Throw away remnant commented out in 36099e8c
[dbsrgits/DBIx-Class.git] / t / storage / txn_scope_guard.t
CommitLineData
471a5fdd 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Warn;
6use Test::Exception;
7use lib qw(t/lib);
8use DBICTest;
9
10# Test txn_scope_guard
11{
12 my $schema = DBICTest->init_schema();
13
14 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
15 my $artist_rs = $schema->resultset('Artist');
16
17 my $fn = __FILE__;
18 throws_ok {
19 my $guard = $schema->txn_scope_guard;
20
21 $artist_rs->create({
22 name => 'Death Cab for Cutie',
23 made_up_column => 1,
24 });
25
26 $guard->commit;
e705f529 27 } qr/No such column 'made_up_column' .*? at .*?\Q$fn\E line \d+/s, "Error propogated okay";
471a5fdd 28
29 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
30
31 my $inner_exception = ''; # set in inner() below
32 throws_ok (sub {
33 outer($schema, 1);
34 }, qr/$inner_exception/, "Nested exceptions propogated");
35
36 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
37
38 lives_ok (sub {
39
40 # this weird assignment is to stop perl <= 5.8.9 leaking $schema on nested sub{}s
41 my $s = $schema;
42
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
46 outer($s, 0);
47 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
48
49 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
50
51 }, 'rollback successful withot exception');
52
53 sub outer {
54 my ($schema, $fatal) = @_;
55
56 my $guard = $schema->txn_scope_guard;
57 $schema->resultset('Artist')->create({
58 name => 'Death Cab for Cutie',
59 });
60 inner($schema, $fatal);
61 }
62
63 sub inner {
64 my ($schema, $fatal) = @_;
65
66 my $inner_guard = $schema->txn_scope_guard;
67 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
68
69 my $artist = $schema->resultset('Artist')->find({ name => 'Death Cab for Cutie' });
70
71 eval {
72 $artist->cds->create({
73 title => 'Plans',
74 year => 2005,
75 $fatal ? ( foo => 'bar' ) : ()
76 });
77 };
78 if ($@) {
79 # Record what got thrown so we can test it propgates out properly.
80 $inner_exception = $@;
81 die $@;
82 }
83
84 # inner guard should commit without consequences
85 $inner_guard->commit;
86 }
87}
88
89# make sure the guard does not eat exceptions
90{
91 my $schema = DBICTest->init_schema;
92
471a5fdd 93 no warnings 'redefine';
0d2033c3 94 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
87bf71d5 95 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
471a5fdd 96
97 throws_ok (sub {
98 my $guard = $schema->txn_scope_guard;
99 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
100
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 );
105
106 die 'Deliberate exception';
750a4ad2 107 }, ( "$]" >= 5.013008 )
e69b5335 108 ? qr/Deliberate exception/s # temporary until we get the generic exception wrapper rolling
109 : qr/Deliberate exception.+Rollback failed/s
110 );
471a5fdd 111
112 # just to mask off warning since we could not disconnect above
113 $schema->storage->_dbh->disconnect;
114}
115
116# make sure it warns *big* on failed rollbacks
f62c5724 117# test with and without a poisoned $@
6e102c8f 118for my $pre_poison (0,1) {
119for my $post_poison (0,1) {
f62c5724 120
6e102c8f 121 my $schema = DBICTest->init_schema(no_populate => 1);
471a5fdd 122
471a5fdd 123 no warnings 'redefine';
0d2033c3 124 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'die die my darling' };
87bf71d5 125 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
471a5fdd 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
471a5fdd 142 {
6e102c8f 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'});
471a5fdd 147 }
148
6e102c8f 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
6853e2c3 152 DBICTest::RunMode->is_plain
6e102c8f 153 or
154 # this always fails
155 ! $pre_poison
156 or
f2f65c95 157 # I do not understand why but on <= 5.8.8 and on 5.10.0 "$pre_poison && $post_poison" passes...
750a4ad2 158 ( "$]" > 5.008008 and "$]" < 5.010000 ) or "$]" > 5.010000
6e102c8f 159 ));
160
161 is (@w, 2, "Both expected warnings found - \$\@ pre-poison: $pre_poison, post-poison: $post_poison" );
471a5fdd 162
163 # just to mask off warning since we could not disconnect above
164 $schema->storage->_dbh->disconnect;
6e102c8f 165}}
471a5fdd 166
153a6b38 167# add a TODO to catch when Text::Balanced is finally fixed
168# https://rt.cpan.org/Public/Bug/Display.html?id=74994
169#
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
172{
173
174 require Text::Balanced;
175
841efcb3 176 my @w;
177 local $SIG{__WARN__} = sub {
9bea2000 178 $_[0] =~ /External exception class .+? \Qimplements partial (broken) overloading/
841efcb3 179 ? push @w, @_
180 : warn @_
181 };
153a6b38 182
841efcb3 183 lives_ok {
184 # this is what poisons $@
185 Text::Balanced::extract_bracketed( '(foo', '()' );
35cf7d1a 186 DBIx::Class::_Util::is_exception($@);
153a6b38 187
841efcb3 188 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
189 my $g = $s->txn_scope_guard;
190 $g->commit;
191 } 'Broken Text::Balanced is not screwing up txn_guard';
153a6b38 192
841efcb3 193 local $TODO = 'RT#74994 *STILL* not fixed';
194 is(scalar @w, 0, 'no warnings \o/');
153a6b38 195}
196
3d56e026 197# ensure Devel::StackTrace-refcapture-like effects are countered
198{
199 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
200 my $g = $s->txn_scope_guard;
201
202 my @arg_capture;
203 {
204 local $SIG{__WARN__} = sub {
205 package DB;
206 my $frnum;
821edc09 207 while (my @f = CORE::caller(++$frnum) ) {
3d56e026 208 push @arg_capture, @DB::args;
209 }
210 };
211
212 undef $g;
213 1;
214 }
215
216 warnings_exist
217 { @arg_capture = () }
218 qr/\QPreventing *MULTIPLE* DESTROY() invocations on DBIx::Class::Storage::TxnScopeGuard/
219 ;
220}
221
471a5fdd 222done_testing;