11 my ($artist, @cd_titles) = @_;
13 $artist->create_related('cds', {
16 }) foreach (@cd_titles);
18 return $artist->cds->all;
21 # Test checking of parameters
23 my $schema = DBICTest->init_schema;
26 (ref $schema)->txn_do(sub{});
27 }, qr/storage/, "can't call txn_do without storage");
31 } qr/\Qrun() requires a coderef to execute as its first argument/,
32 '$coderef parameter check ok';
35 # Test successful txn_do() - scalar/list context
37 my $schema = DBICTest->init_schema;
39 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
41 my @titles = map {'txn_do test CD ' . $_} (1..5);
42 my $artist = $schema->resultset('Artist')->find(1);
43 my $count_before = $artist->cds->count;
47 @res = $schema->txn_do($code, $artist, @titles);
48 is(scalar @res, $count_before+5, 'successful txn added 5 cds');
51 $res[0] = $schema->txn_do($code, $artist, @titles);
52 is($res[0], $count_before+5, 'successful txn added 5 cds');
56 title => "txn_do test CD $_",
57 })->first->year, 2006, "new CD $_ year correct") for (1..5);
59 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
62 # Test txn_do() @_ aliasing support
64 my $schema = DBICTest->init_schema;
67 $schema->storage->txn_do (sub { $_[0] = 'changed' }, $res);
68 is ($res, 'changed', "Arguments properly aliased for txn_do");
71 # Test nested successful txn_do()
73 my $schema = DBICTest->init_schema;
75 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
77 my $nested_code = sub {
78 my ($schema, $artist, $code) = @_;
80 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
81 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
83 $schema->txn_do($code, $artist, @titles1);
84 $schema->txn_do($code, $artist, @titles2);
87 my $artist = $schema->resultset('Artist')->find(2);
88 my $count_before = $artist->cds->count;
91 $schema->txn_do($nested_code, $schema, $artist, $code);
92 }, 'nested txn_do succeeded');
95 title => 'nested txn_do test CD '.$_,
96 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
97 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
99 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
102 # test nested txn_begin on fresh connection
104 my $schema = DBICTest->init_schema(sqlite_use_file => 1, no_deploy => 1);
105 $schema->storage->ensure_connected;
107 is ($schema->storage->transaction_depth, 0, 'Start outside txn');
113 die "$$ starts in txn!" if $s->storage->transaction_depth != 0;
115 die "$$ not in txn!" if $s->storage->transaction_depth == 0;
116 $s->storage->dbh->do('SELECT 1') }
118 die "$$ did not finish txn!" if $s->storage->transaction_depth != 0;
122 $_[0]->storage->dbh->do('SELECT 1');
126 my $guard = $_[0]->txn_scope_guard;
127 $_[0]->storage->dbh->do('SELECT 1');
132 die "Unable to fork: $!\n"
144 is ($schema->storage->transaction_depth, 0, 'Parent still outside txn');
146 for my $pid (@pids) {
148 ok (! $?, "Child $pid exit ok");
152 # Test txn_do/scope_guard with forking: outer txn_do
154 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
156 for my $pass (1..2) {
158 # do something trying to destabilize the depth count
161 my $guard = $schema->txn_scope_guard;
162 $schema->txn_do( sub { die } );
164 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
165 $schema->txn_do( sub {
166 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
170 $schema->txn_do ( sub { _test_forking_action ($schema, $pass) } );
174 # same test with outer guard
176 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
178 for my $pass (1..2) {
180 # do something trying to destabilize the depth count
183 my $guard = $schema->txn_scope_guard;
184 $schema->txn_do( sub { die } );
186 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
187 $schema->txn_do( sub {
188 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
192 my $guard = $schema->txn_scope_guard;
193 my @pids = _test_forking_action ($schema, $pass);
198 sub _test_forking_action {
199 my ($schema, $pass) = @_;
203 SKIP: for my $count (1 .. 5) {
205 skip 'Weird DBI General Protection Faults, skip forking tests (RT#63104)', 5
209 die "Unable to fork: $!\n"
218 $schema->txn_do (sub {
219 my $depth = $schema->storage->transaction_depth;
220 die "$$(txn_do)unexpected txn depth $depth!" if $depth != 1;
221 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
225 my $guard = $schema->txn_scope_guard;
226 my $depth = $schema->storage->transaction_depth;
227 die "$$(scope_guard) unexpected txn depth $depth!" if $depth != 1;
228 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
235 for my $pid (@pids) {
237 ok (! $?, "Child $pid exit ok (pass $pass)");
240 # it is important to reap all children before checking the final db-state
241 # otherwise a deadlock may occur between the transactions running in the
242 # children and the query of the parent
243 for my $pid (@pids) {
244 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
248 my $fail_code = sub {
250 $artist->create_related('cds', {
251 title => 'this should not exist',
254 die "the sky is falling";
258 my $schema = DBICTest->init_schema;
260 # Test failed txn_do()
263 is( $schema->storage->{transaction_depth}, 0, "txn depth starts at 0 (pass $pass)");
265 my $artist = $schema->resultset('Artist')->find(3);
268 $schema->txn_do($fail_code, $artist);
269 }, qr/the sky is falling/, "failed txn_do threw an exception (pass $pass)");
271 my $cd = $artist->cds({
272 title => 'this should not exist',
275 ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
277 is( $schema->storage->{transaction_depth}, 0, "txn depth has been reset (pass $pass)");
281 # Test failed txn_do() with failed rollback
283 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
285 my $artist = $schema->resultset('Artist')->find(3);
287 # Force txn_rollback() to throw an exception
288 no warnings qw/once redefine/;
290 # this should logically work just fine - but it does not,
291 # only direct override of the existing method dtrt
292 #local *DBIx::Class::Storage::DBI::SQLite::txn_rollback = sub { die 'FAILED' };
294 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'FAILED' };
295 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
299 $schema->txn_do($fail_code, $artist);
301 qr/the sky is falling.+Rollback failed/s,
302 'txn_rollback threw a rollback exception (and included the original exception'
305 my $cd = $artist->cds({
306 title => 'this should not exist',
309 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
310 q{changed the cds table});
311 $cd->delete; # Rollback failed
313 title => 'this should not exist',
316 ok(!defined($cd), q{deleted the failed txn's cd});
317 $schema->storage->_dbh->rollback;
321 # Test nested failed txn_do()
323 my $schema = DBICTest->init_schema();
325 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
327 my $nested_fail_code = sub {
328 my ($schema, $artist, $code1, $code2) = @_;
330 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
332 $schema->txn_do($code1, $artist, @titles); # successful txn
333 $schema->txn_do($code2, $artist); # failed txn
336 my $artist = $schema->resultset('Artist')->find(3);
339 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
340 }, qr/the sky is falling/, 'nested failed txn_do threw exception');
342 ok(!defined($artist->cds({
343 title => 'nested txn_do test CD '.$_,
345 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
346 my $cd = $artist->cds({
347 title => 'this should not exist',
350 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
354 # Grab a new schema to test txn before connect
355 # also test nested txn exception
357 my $schema = DBICTest->init_schema(no_deploy => 1);
359 $schema->txn_begin();
360 $schema->txn_begin();
361 }, 'Pre-connection nested transactions.');
363 throws_ok( sub { $schema->txn_rollback }, 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION', 'got proper nested rollback exception' );
366 # make sure AutoCommit => 0 on external handles behaves correctly with scope_guard
368 my $factory = DBICTest->init_schema;
369 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
370 my $dbh = $factory->storage->dbh;
371 $dbh->{AutoCommit} = 0;
373 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
374 my $schema = DBICTest::Schema->connect (sub { $dbh });
377 my $guard = $schema->txn_scope_guard;
378 $schema->resultset('CD')->delete;
380 }, 'No attempt to start a transaction with scope guard');
382 is ($schema->resultset('CD')->count, 0, 'Deletion successful in txn');
384 # this will commit the implicitly started txn
387 } [], 'No warnings on AutoCommit => 0 with txn_guard';
389 # make sure AutoCommit => 0 on external handles behaves correctly with txn_do
391 my $factory = DBICTest->init_schema;
392 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
393 my $dbh = $factory->storage->dbh;
394 $dbh->{AutoCommit} = 0;
396 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
397 my $schema = DBICTest::Schema->connect (sub { $dbh });
400 $schema->txn_do (sub { $schema->resultset ('CD')->delete });
401 }, 'No attempt to start a atransaction with txn_do');
403 is ($schema->resultset('CD')->count, 0, 'Deletion successful');
405 # this will commit the implicitly started txn
408 } [], 'No warnings on AutoCommit => 0 with txn_do';
411 # make sure we are not fucking up the stacktrace on broken overloads
413 package DBICTest::BrokenOverload;
415 use overload '""' => sub { $_[0] };
420 local $SIG{__WARN__} = sub {
421 $_[0] =~ /\QExternal exception class DBICTest::BrokenOverload implements partial (broken) overloading preventing its instances from being used in simple (\E\$x eq \$y\Q) comparisons/
426 my $s = DBICTest->init_schema(no_deploy => 1);
428 my $g = $s->storage->txn_scope_guard;
429 my $broken_exception = bless {}, 'DBICTest::BrokenOverload';
431 # FIXME - investigate what confuses the regex engine below
433 # do not reformat - line-num part of the test
434 my $ln = __LINE__ + 6;
438 $s->storage->_dbh->disconnect;
439 die $broken_exception
442 } qr/\QTransaction aborted: $broken_exception. Rollback failed: lost connection to storage at @{[__FILE__]} line $ln\E\n/; # FIXME wtf - ...\E$/m doesn't work here
444 is @w, 1, 'One matching warning only';