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');
114 die "$$ starts in txn!" if $s->storage->transaction_depth != 0;
116 die "$$ not in txn!" if $s->storage->transaction_depth == 0;
117 $s->storage->dbh->do('SELECT 1') }
119 die "$$ did not finish txn!" if $s->storage->transaction_depth != 0;
123 $_[0]->storage->dbh->do('SELECT 1');
127 my $guard = $_[0]->txn_scope_guard;
128 $_[0]->storage->dbh->do('SELECT 1');
134 if( ! defined $pid ) {
135 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
136 if $! == Errno::EAGAIN();
138 die "Unable to fork: $!"
150 is ($schema->storage->transaction_depth, 0, 'Parent still outside txn');
152 for my $pid (@pids) {
154 ok (! $?, "Child $pid exit ok");
158 # Test txn_do/scope_guard with forking: outer txn_do
160 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
162 for my $pass (1..2) {
164 # do something trying to destabilize the depth count
167 my $guard = $schema->txn_scope_guard;
168 $schema->txn_do( sub { die } );
170 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
171 $schema->txn_do( sub {
172 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
176 $schema->txn_do ( sub { _test_forking_action ($schema, $pass) } );
180 # same test with outer guard
182 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
184 for my $pass (1..2) {
186 # do something trying to destabilize the depth count
189 my $guard = $schema->txn_scope_guard;
190 $schema->txn_do( sub { die } );
192 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
193 $schema->txn_do( sub {
194 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
198 my $guard = $schema->txn_scope_guard;
199 my @pids = _test_forking_action ($schema, $pass);
204 sub _test_forking_action {
205 my ($schema, $pass) = @_;
209 SKIP: for my $count (1 .. 5) {
211 skip 'Weird DBI General Protection Faults, skip forking tests (RT#63104)', 5
215 if( ! defined $pid ) {
217 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
218 if $! == Errno::EAGAIN();
220 die "Unable to fork: $!"
229 $schema->txn_do (sub {
230 my $depth = $schema->storage->transaction_depth;
231 die "$$(txn_do)unexpected txn depth $depth!" if $depth != 1;
232 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
236 my $guard = $schema->txn_scope_guard;
237 my $depth = $schema->storage->transaction_depth;
238 die "$$(scope_guard) unexpected txn depth $depth!" if $depth != 1;
239 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
246 for my $pid (@pids) {
248 ok (! $?, "Child $pid exit ok (pass $pass)");
251 # it is important to reap all children before checking the final db-state
252 # otherwise a deadlock may occur between the transactions running in the
253 # children and the query of the parent
254 for my $pid (@pids) {
255 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
259 my $fail_code = sub {
261 $artist->create_related('cds', {
262 title => 'this should not exist',
265 die "the sky is falling";
269 my $schema = DBICTest->init_schema;
271 # Test failed txn_do()
274 is( $schema->storage->transaction_depth, 0, "txn depth starts at 0 (pass $pass)");
276 my $artist = $schema->resultset('Artist')->find(3);
279 $schema->txn_do($fail_code, $artist);
280 }, qr/the sky is falling/, "failed txn_do threw an exception (pass $pass)");
282 my $cd = $artist->cds({
283 title => 'this should not exist',
286 ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
288 is( $schema->storage->transaction_depth, 0, "txn depth has been reset (pass $pass)");
292 # Test failed txn_do() with failed rollback
294 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
296 my $artist = $schema->resultset('Artist')->find(3);
298 # Force txn_rollback() to throw an exception
299 no warnings qw/once redefine/;
301 # this should logically work just fine - but it does not,
302 # only direct override of the existing method dtrt
303 #local *DBIx::Class::Storage::DBI::SQLite::txn_rollback = sub { die 'FAILED' };
305 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'FAILED' };
306 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
310 $schema->txn_do($fail_code, $artist);
312 qr/the sky is falling.+Rollback failed/s,
313 'txn_rollback threw a rollback exception (and included the original exception'
316 my $cd = $artist->cds({
317 title => 'this should not exist',
320 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
321 q{changed the cds table});
322 $cd->delete; # Rollback failed
324 title => 'this should not exist',
327 ok(!defined($cd), q{deleted the failed txn's cd});
328 $schema->storage->_dbh->rollback;
332 # Test nested failed txn_do()
334 my $schema = DBICTest->init_schema();
336 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
338 my $nested_fail_code = sub {
339 my ($schema, $artist, $code1, $code2) = @_;
341 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
343 $schema->txn_do($code1, $artist, @titles); # successful txn
344 $schema->txn_do($code2, $artist); # failed txn
347 my $artist = $schema->resultset('Artist')->find(3);
350 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
351 }, qr/the sky is falling/, 'nested failed txn_do threw exception');
353 ok(!defined($artist->cds({
354 title => 'nested txn_do test CD '.$_,
356 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
357 my $cd = $artist->cds({
358 title => 'this should not exist',
361 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
365 # Grab a new schema to test txn before connect
366 # also test nested txn exception
368 my $schema = DBICTest->init_schema(no_deploy => 1);
370 $schema->txn_begin();
371 $schema->txn_begin();
372 }, 'Pre-connection nested transactions.');
374 throws_ok( sub { $schema->txn_rollback }, 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION', 'got proper nested rollback exception' );
377 # make sure AutoCommit => 0 on external handles behaves correctly with scope_guard
379 my $factory = DBICTest->init_schema;
380 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
381 my $dbh = $factory->storage->dbh;
382 $dbh->{AutoCommit} = 0;
384 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
385 my $schema = DBICTest::Schema->connect (sub { $dbh });
388 my $guard = $schema->txn_scope_guard;
389 $schema->resultset('CD')->delete;
391 }, 'No attempt to start a transaction with scope guard');
393 is ($schema->resultset('CD')->count, 0, 'Deletion successful in txn');
395 # this will commit the implicitly started txn
398 } [], 'No warnings on AutoCommit => 0 with txn_guard';
400 # make sure AutoCommit => 0 on external handles behaves correctly with txn_do
402 my $factory = DBICTest->init_schema;
403 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
404 my $dbh = $factory->storage->dbh;
405 $dbh->{AutoCommit} = 0;
407 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
408 my $schema = DBICTest::Schema->connect (sub { $dbh });
411 $schema->txn_do (sub { $schema->resultset ('CD')->delete });
412 }, 'No attempt to start a atransaction with txn_do');
414 is ($schema->resultset('CD')->count, 0, 'Deletion successful');
416 # this will commit the implicitly started txn
419 } [], 'No warnings on AutoCommit => 0 with txn_do';
422 # make sure we are not fucking up the stacktrace on broken overloads
424 package DBICTest::BrokenOverload;
426 use overload '""' => sub { $_[0] };
431 local $SIG{__WARN__} = sub {
432 $_[0] =~ /\QExternal exception class DBICTest::BrokenOverload implements partial (broken) overloading preventing its instances from being used in simple (\E\$x eq \$y\Q) comparisons/
437 my $s = DBICTest->init_schema(no_deploy => 1);
439 my $g = $s->storage->txn_scope_guard;
440 my $broken_exception = bless {}, 'DBICTest::BrokenOverload';
442 # FIXME - investigate what confuses the regex engine below
444 # do not reformat - line-num part of the test
445 my $ln = __LINE__ + 6;
449 $s->storage->_dbh->disconnect;
450 die $broken_exception
453 } qr/\QTransaction aborted: $broken_exception. Rollback failed: DBIx::Class::Storage::DBI::txn_rollback(): lost connection to storage at @{[__FILE__]} line $ln\E\n/; # FIXME wtf - ...\E$/m doesn't work here
455 is @w, 1, 'One matching warning only';
457 # try the same broken exception object, but have exception_action inject it
458 $s->exception_action(sub { die $broken_exception });
461 die "some string masked away";
464 isa_ok $@, 'DBICTest::BrokenOverload', 'Deficient exception properly propagated';
466 is @w, 2, 'The warning was emitted a second time';