1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
14 my ($artist, @cd_titles) = @_;
16 $artist->create_related('cds', {
19 }) foreach (@cd_titles);
21 return $artist->cds->all;
24 # Test checking of parameters
26 my $schema = DBICTest->init_schema;
29 (ref $schema)->txn_do(sub{});
30 }, qr/storage/, "can't call txn_do without storage");
34 } qr/\Qrun() requires a coderef to execute as its first argument/,
35 '$coderef parameter check ok';
38 # Test successful txn_do() - scalar/list context
40 my $schema = DBICTest->init_schema;
42 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
44 my @titles = map {'txn_do test CD ' . $_} (1..5);
45 my $artist = $schema->resultset('Artist')->find(1);
46 my $count_before = $artist->cds->count;
50 @res = $schema->txn_do($code, $artist, @titles);
51 is(scalar @res, $count_before+5, 'successful txn added 5 cds');
54 $res[0] = $schema->txn_do($code, $artist, @titles);
55 is($res[0], $count_before+5, 'successful txn added 5 cds');
59 title => "txn_do test CD $_",
60 })->first->year, 2006, "new CD $_ year correct") for (1..5);
62 is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
65 # Test txn_do() @_ aliasing support
67 my $schema = DBICTest->init_schema;
70 $schema->storage->txn_do (sub { $_[0] = 'changed' }, $res);
71 is ($res, 'changed', "Arguments properly aliased for txn_do");
74 # Test nested successful txn_do()
76 my $schema = DBICTest->init_schema;
78 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
80 my $nested_code = sub {
81 my ($schema, $artist, $code) = @_;
83 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
84 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
86 $schema->txn_do($code, $artist, @titles1);
87 $schema->txn_do($code, $artist, @titles2);
90 my $artist = $schema->resultset('Artist')->find(2);
91 my $count_before = $artist->cds->count;
94 $schema->txn_do($nested_code, $schema, $artist, $code);
95 }, 'nested txn_do succeeded');
98 title => 'nested txn_do test CD '.$_,
99 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
100 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
102 is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
105 # test nested txn_begin on fresh connection
107 my $schema = DBICTest->init_schema(sqlite_use_file => 1, no_deploy => 1);
108 $schema->storage->ensure_connected;
110 is ($schema->storage->transaction_depth, 0, 'Start outside txn');
117 die "$$ starts in txn!" if $s->storage->transaction_depth != 0;
119 die "$$ not in txn!" if $s->storage->transaction_depth == 0;
120 $s->storage->dbh->do('SELECT 1') }
122 die "$$ did not finish txn!" if $s->storage->transaction_depth != 0;
126 $_[0]->storage->dbh->do('SELECT 1');
130 my $guard = $_[0]->txn_scope_guard;
131 $_[0]->storage->dbh->do('SELECT 1');
137 if( ! defined $pid ) {
138 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
139 if $! == Errno::EAGAIN();
141 die "Unable to fork: $!"
153 is ($schema->storage->transaction_depth, 0, 'Parent still outside txn');
155 for my $pid (@pids) {
157 ok (! $?, "Child $pid exit ok");
161 # Test txn_do/scope_guard with forking: outer txn_do
163 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
165 for my $pass (1..2) {
167 # do something trying to destabilize the depth count
170 my $guard = $schema->txn_scope_guard;
171 $schema->txn_do( sub { die } );
173 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
174 $schema->txn_do( sub {
175 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
179 $schema->txn_do ( sub { _test_forking_action ($schema, $pass) } );
183 # same test with outer guard
185 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
187 for my $pass (1..2) {
189 # do something trying to destabilize the depth count
192 my $guard = $schema->txn_scope_guard;
193 $schema->txn_do( sub { die } );
195 is( $schema->storage->transaction_depth, 0, 'Transaction successfully aborted' );
196 $schema->txn_do( sub {
197 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
201 my $guard = $schema->txn_scope_guard;
202 my @pids = _test_forking_action ($schema, $pass);
207 sub _test_forking_action {
208 my ($schema, $pass) = @_;
212 SKIP: for my $count (1 .. 5) {
214 skip 'FIXME: Weird DBI General Protection Faults, skip forking tests (RT#63104)', 5
218 if( ! defined $pid ) {
219 skip "EAGAIN encountered, your system is likely bogged down: skipping forking test", 1
220 if $! == Errno::EAGAIN();
222 die "Unable to fork: $!"
231 $schema->txn_do (sub {
232 my $depth = $schema->storage->transaction_depth;
233 die "$$(txn_do)unexpected txn depth $depth!" if $depth != 1;
234 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
238 my $guard = $schema->txn_scope_guard;
239 my $depth = $schema->storage->transaction_depth;
240 die "$$(scope_guard) unexpected txn depth $depth!" if $depth != 1;
241 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
248 for my $pid (@pids) {
250 ok (! $?, "Child $pid exit ok (pass $pass)");
253 # it is important to reap all children before checking the final db-state
254 # otherwise a deadlock may occur between the transactions running in the
255 # children and the query of the parent
256 for my $pid (@pids) {
257 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
261 my $fail_code = sub {
263 $artist->create_related('cds', {
264 title => 'this should not exist',
267 die "the sky is falling";
271 my $schema = DBICTest->init_schema;
273 # Test failed txn_do()
276 is( $schema->storage->transaction_depth, 0, "txn depth starts at 0 (pass $pass)");
278 my $artist = $schema->resultset('Artist')->find(3);
281 $schema->txn_do($fail_code, $artist);
282 }, qr/the sky is falling/, "failed txn_do threw an exception (pass $pass)");
284 my $cd = $artist->cds({
285 title => 'this should not exist',
288 ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
290 is( $schema->storage->transaction_depth, 0, "txn depth has been reset (pass $pass)");
294 # Test failed txn_do() with failed rollback
296 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
298 my $artist = $schema->resultset('Artist')->find(3);
300 # Force txn_rollback() to throw an exception
301 no warnings qw/once redefine/;
303 # this should logically work just fine - but it does not,
304 # only direct override of the existing method dtrt
305 #local *DBIx::Class::Storage::DBI::SQLite::txn_rollback = sub { die 'FAILED' };
307 local *DBIx::Class::Storage::DBI::txn_rollback = sub { die 'FAILED' };
308 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
312 $schema->txn_do($fail_code, $artist);
314 qr/the sky is falling.+Rollback failed/s,
315 'txn_rollback threw a rollback exception (and included the original exception'
318 my $cd = $artist->cds({
319 title => 'this should not exist',
322 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
323 q{changed the cds table});
324 $cd->delete; # Rollback failed
326 title => 'this should not exist',
329 ok(!defined($cd), q{deleted the failed txn's cd});
330 $schema->storage->_dbh->rollback;
334 # Test nested failed txn_do()
336 my $schema = DBICTest->init_schema();
338 is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
340 my $nested_fail_code = sub {
341 my ($schema, $artist, $code1, $code2) = @_;
343 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
345 $schema->txn_do($code1, $artist, @titles); # successful txn
346 $schema->txn_do($code2, $artist); # failed txn
349 my $artist = $schema->resultset('Artist')->find(3);
352 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
353 }, qr/the sky is falling/, 'nested failed txn_do threw exception');
355 ok(!defined($artist->cds({
356 title => 'nested txn_do test CD '.$_,
358 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
359 my $cd = $artist->cds({
360 title => 'this should not exist',
363 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
367 # Grab a new schema to test txn before connect
368 # also test nested txn exception
370 my $schema = DBICTest->init_schema(no_deploy => 1);
372 $schema->txn_begin();
373 $schema->txn_begin();
374 }, 'Pre-connection nested transactions.');
376 throws_ok( sub { $schema->txn_rollback }, 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION', 'got proper nested rollback exception' );
379 # make sure AutoCommit => 0 on external handles behaves correctly with scope_guard
381 my $factory = DBICTest->init_schema;
382 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
383 my $dbh = $factory->storage->dbh;
384 $dbh->{AutoCommit} = 0;
386 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
387 my $schema = DBICTest::Schema->connect (sub { $dbh });
390 my $guard = $schema->txn_scope_guard;
391 $schema->resultset('CD')->delete;
393 }, 'No attempt to start a transaction with scope guard');
395 is ($schema->resultset('CD')->count, 0, 'Deletion successful in txn');
397 # this will commit the implicitly started txn
400 } [], 'No warnings on AutoCommit => 0 with txn_guard';
402 # make sure AutoCommit => 0 on external handles behaves correctly with txn_do
404 my $factory = DBICTest->init_schema;
405 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
406 my $dbh = $factory->storage->dbh;
407 $dbh->{AutoCommit} = 0;
409 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
410 my $schema = DBICTest::Schema->connect (sub { $dbh });
413 $schema->txn_do (sub { $schema->resultset ('CD')->delete });
414 }, 'No attempt to start a atransaction with txn_do');
416 is ($schema->resultset('CD')->count, 0, 'Deletion successful');
418 # this will commit the implicitly started txn
421 } [], 'No warnings on AutoCommit => 0 with txn_do';
424 # make sure we are not fucking up the stacktrace on broken overloads
426 package DBICTest::BrokenOverload;
428 use overload '""' => sub { $_[0] };
433 local $SIG{__WARN__} = sub {
434 $_[0] =~ /\QExternal exception class DBICTest::BrokenOverload implements partial (broken) overloading preventing its instances from being used in simple (\E\$x eq \$y\Q) comparisons/
439 my $s = DBICTest->init_schema(no_deploy => 1);
441 my $g = $s->storage->txn_scope_guard;
442 my $broken_exception = bless {}, 'DBICTest::BrokenOverload';
444 # FIXME - investigate what confuses the regex engine below
446 # do not reformat - line-num part of the test
447 my $ln = __LINE__ + 6;
451 $s->storage->_dbh->disconnect;
452 die $broken_exception
455 } 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
457 is @w, 1, 'One matching warning only';
459 # try the same broken exception object, but have exception_action inject it
460 $s->exception_action(sub { die $broken_exception });
463 die "some string masked away";
466 isa_ok $@, 'DBICTest::BrokenOverload', 'Deficient exception properly propagated';
468 is @w, 2, 'The warning was emitted a second time';