1 # Test is sufficiently involved to *want* to run with "maximum paranoia"
2 BEGIN { $ENV{DBICTEST_OLD_MRO_SANITY_CHECK_ASSERTIONS} = 1 }
4 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
17 my ($artist, @cd_titles) = @_;
19 $artist->create_related('cds', {
22 }) foreach (@cd_titles);
24 return $artist->cds->all;
27 # Test checking of parameters
29 my $schema = DBICTest->init_schema;
32 (ref $schema)->txn_do(sub{});
33 }, qr/storage/, "can't call txn_do without storage");
37 } qr/\Qrun() requires a coderef to execute as its first argument/,
38 '$coderef parameter check ok';
41 # Test successful txn_do() - scalar/list context
43 my $schema = DBICTest->init_schema;
45 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
47 my @titles = map {'txn_do test CD ' . $_} (1..5);
48 my $artist = $schema->resultset('Artist')->find(1);
49 my $count_before = $artist->cds->count;
53 @res = $schema->txn_do($code, $artist, @titles);
54 is(scalar @res, $count_before+5, 'successful txn added 5 cds');
57 $res[0] = $schema->txn_do($code, $artist, @titles);
58 is($res[0], $count_before+5, 'successful txn added 5 cds');
62 title => "txn_do test CD $_",
63 })->first->year, 2006, "new CD $_ year correct") for (1..5);
65 is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
68 # Test txn_do() @_ aliasing support
70 my $schema = DBICTest->init_schema;
73 $schema->storage->txn_do (sub { $_[0] = 'changed' }, $res);
74 is ($res, 'changed', "Arguments properly aliased for txn_do");
77 # Test nested successful txn_do()
79 my $schema = DBICTest->init_schema;
81 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
83 my $nested_code = sub {
84 my ($schema, $artist, $code) = @_;
86 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
87 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
89 $schema->txn_do($code, $artist, @titles1);
90 $schema->txn_do($code, $artist, @titles2);
93 my $artist = $schema->resultset('Artist')->find(2);
94 my $count_before = $artist->cds->count;
97 $schema->txn_do($nested_code, $schema, $artist, $code);
98 }, 'nested txn_do succeeded');
101 title => 'nested txn_do test CD '.$_,
102 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
103 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
105 is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
108 # test nested txn_begin on fresh connection
110 my $schema = DBICTest->init_schema(sqlite_use_file => 1, no_deploy => 1);
111 $schema->storage->ensure_connected;
113 is ($schema->storage->transaction_depth, 0, 'Start outside txn');
120 die "$$ starts in txn!" if $s->storage->transaction_depth != 0;
122 die "$$ not in txn!" if $s->storage->transaction_depth == 0;
123 $s->storage->dbh->do('SELECT 1') }
125 die "$$ did not finish txn!" if $s->storage->transaction_depth != 0;
129 $_[0]->storage->dbh->do('SELECT 1');
133 my $guard = $_[0]->txn_scope_guard;
134 $_[0]->storage->dbh->do('SELECT 1');
140 if( ! defined $pid ) {
141 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
142 if $! == Errno::EAGAIN();
144 die "Unable to fork: $!"
156 is ($schema->storage->transaction_depth, 0, 'Parent still outside txn');
158 for my $pid (@pids) {
160 ok (! $?, "Child $pid exit ok");
164 # Test txn_do/scope_guard with forking: outer txn_do
166 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
168 for my $pass (1..2) {
170 # do something trying to destabilize the depth count
173 my $guard = $schema->txn_scope_guard;
174 $schema->txn_do( sub { die } );
176 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
177 $schema->txn_do( sub {
178 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
182 $schema->txn_do ( sub { _test_forking_action ($schema, $pass) } );
186 # same test with outer guard
188 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
190 for my $pass (1..2) {
192 # do something trying to destabilize the depth count
195 my $guard = $schema->txn_scope_guard;
196 $schema->txn_do( sub { die } );
198 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
199 $schema->txn_do( sub {
200 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
204 my $guard = $schema->txn_scope_guard;
205 my @pids = _test_forking_action ($schema, $pass);
210 sub _test_forking_action {
211 my ($schema, $pass) = @_;
215 SKIP: for my $count (1 .. 5) {
217 skip 'FIXME: Weird DBI General Protection Faults, skip forking tests (RT#63104)', 5
221 if( ! defined $pid ) {
222 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
223 if $! == Errno::EAGAIN();
225 die "Unable to fork: $!"
234 $schema->txn_do (sub {
235 my $depth = $schema->storage->transaction_depth;
236 die "$$(txn_do)unexpected txn depth $depth!" if $depth != 1;
237 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
241 my $guard = $schema->txn_scope_guard;
242 my $depth = $schema->storage->transaction_depth;
243 die "$$(scope_guard) unexpected txn depth $depth!" if $depth != 1;
244 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
251 for my $pid (@pids) {
253 ok (! $?, "Child $pid exit ok (pass $pass)");
256 # it is important to reap all children before checking the final db-state
257 # otherwise a deadlock may occur between the transactions running in the
258 # children and the query of the parent
259 for my $pid (@pids) {
260 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
264 my $fail_code = sub {
266 $artist->create_related('cds', {
267 title => 'this should not exist',
270 die "the sky is falling";
274 my $schema = DBICTest->init_schema;
276 # Test failed txn_do()
279 is( $schema->storage->transaction_depth, 0, "txn depth starts at 0 (pass $pass)");
281 my $artist = $schema->resultset('Artist')->find(3);
284 $schema->txn_do($fail_code, $artist);
285 }, qr/the sky is falling/, "failed txn_do threw an exception (pass $pass)");
287 my $cd = $artist->cds({
288 title => 'this should not exist',
291 ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
293 is( $schema->storage->transaction_depth, 0, "txn depth has been reset (pass $pass)");
297 # Test failed txn_do() with failed rollback
299 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
301 my $artist = $schema->resultset('Artist')->find(3);
303 # Force txn_rollback() to throw an exception
304 no warnings qw/once redefine/;
306 # this should logically work just fine - but it does not,
307 # only direct override of the existing method dtrt
308 #local *DBIx::Class::Storage::DBI::SQLite::txn_rollback = sub { die 'FAILED' };
310 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'FAILED' };
311 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
315 $schema->txn_do($fail_code, $artist);
317 qr/the sky is falling.+Rollback failed/s,
318 'txn_rollback threw a rollback exception (and included the original exception'
321 my $cd = $artist->cds({
322 title => 'this should not exist',
325 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
326 q{changed the cds table});
327 $cd->delete; # Rollback failed
329 title => 'this should not exist',
332 ok(!defined($cd), q{deleted the failed txn's cd});
333 $schema->storage->_dbh->rollback;
337 # Test nested failed txn_do()
339 my $schema = DBICTest->init_schema();
341 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
343 my $nested_fail_code = sub {
344 my ($schema, $artist, $code1, $code2) = @_;
346 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
348 $schema->txn_do($code1, $artist, @titles); # successful txn
349 $schema->txn_do($code2, $artist); # failed txn
352 my $artist = $schema->resultset('Artist')->find(3);
355 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
356 }, qr/the sky is falling/, 'nested failed txn_do threw exception');
358 ok(!defined($artist->cds({
359 title => 'nested txn_do test CD '.$_,
361 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
362 my $cd = $artist->cds({
363 title => 'this should not exist',
366 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
370 # Grab a new schema to test txn before connect
371 # also test nested txn exception
373 my $schema = DBICTest->init_schema(no_deploy => 1);
375 $schema->txn_begin();
376 $schema->txn_begin();
377 }, 'Pre-connection nested transactions.');
379 throws_ok( sub { $schema->txn_rollback }, 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION', 'got proper nested rollback exception' );
382 # make sure AutoCommit => 0 on external handles behaves correctly with scope_guard
384 my $factory = DBICTest->init_schema;
385 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
386 my $dbh = $factory->storage->dbh;
387 $dbh->{AutoCommit} = 0;
389 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
390 my $schema = DBICTest::Schema->connect (sub { $dbh });
393 my $guard = $schema->txn_scope_guard;
394 $schema->resultset('CD')->delete;
396 }, 'No attempt to start a transaction with scope guard');
398 is ($schema->resultset('CD')->count, 0, 'Deletion successful in txn');
400 # this will commit the implicitly started txn
403 } [], 'No warnings on AutoCommit => 0 with txn_guard';
405 # make sure AutoCommit => 0 on external handles behaves correctly with txn_do
407 my $factory = DBICTest->init_schema;
408 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
409 my $dbh = $factory->storage->dbh;
410 $dbh->{AutoCommit} = 0;
412 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
413 my $schema = DBICTest::Schema->connect (sub { $dbh });
416 $schema->txn_do (sub { $schema->resultset ('CD')->delete });
417 }, 'No attempt to start a atransaction with txn_do');
419 is ($schema->resultset('CD')->count, 0, 'Deletion successful');
421 # this will commit the implicitly started txn
424 } [], 'No warnings on AutoCommit => 0 with txn_do';
427 # make sure we are not fucking up the stacktrace on broken overloads
429 package DBICTest::BrokenOverload;
431 use overload '""' => sub { $_[0] };
436 local $SIG{__WARN__} = sub {
437 $_[0] =~ /\QExternal exception class DBICTest::BrokenOverload implements partial (broken) overloading preventing its instances from being used in simple (\E\$x eq \$y\Q) comparisons/
442 my $s = DBICTest->init_schema(no_deploy => 1);
444 my $g = $s->storage->txn_scope_guard;
445 my $broken_exception = bless {}, 'DBICTest::BrokenOverload';
447 # FIXME - investigate what confuses the regex engine below
449 # do not reformat - line-num part of the test
450 my $ln = __LINE__ + 6;
454 $s->storage->_dbh->disconnect;
455 die $broken_exception
458 } 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
460 is @w, 1, 'One matching warning only';
462 # try the same broken exception object, but have exception_action inject it
463 $s->exception_action(sub { die $broken_exception });
466 die "some string masked away";
469 isa_ok $@, 'DBICTest::BrokenOverload', 'Deficient exception properly propagated';
471 is @w, 2, 'The warning was emitted a second time';