10 plan skip_all => 'Disabled on windows, pending resolution of DBD::SQLite SIGSEGVs'
14 my ($artist, @cd_titles) = @_;
16 $artist->create_related('cds', {
19 }) foreach (@cd_titles);
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/must be a CODE reference/, '$coderef parameter check ok');
37 # Test successful txn_do() - scalar/list context
39 my $schema = DBICTest->init_schema;
41 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
43 my @titles = map {'txn_do test CD ' . $_} (1..5);
44 my $artist = $schema->resultset('Artist')->find(1);
45 my $count_before = $artist->cds->count;
49 @res = $schema->txn_do($code, $artist, @titles);
50 is(scalar @res, $count_before+5, 'successful txn added 5 cds');
53 $res[0] = $schema->txn_do($code, $artist, @titles);
54 is($res[0], $count_before+5, 'successful txn added 5 cds');
58 title => "txn_do test CD $_",
59 })->first->year, 2006, "new CD $_ year correct") for (1..5);
61 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
64 # Test txn_do() @_ aliasing support
66 my $schema = DBICTest->init_schema;
69 $schema->storage->txn_do (sub { $_[0] = 'changed' }, $res);
70 is ($res, 'changed', "Arguments properly aliased for txn_do");
73 # Test nested successful txn_do()
75 my $schema = DBICTest->init_schema;
77 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
79 my $nested_code = sub {
80 my ($schema, $artist, $code) = @_;
82 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
83 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
85 $schema->txn_do($code, $artist, @titles1);
86 $schema->txn_do($code, $artist, @titles2);
89 my $artist = $schema->resultset('Artist')->find(2);
90 my $count_before = $artist->cds->count;
93 $schema->txn_do($nested_code, $schema, $artist, $code);
94 }, 'nested txn_do succeeded');
97 title => 'nested txn_do test CD '.$_,
98 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
99 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
101 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
104 # test nested txn_begin on fresh connection
106 my $schema = DBICTest->init_schema(sqlite_use_file => 1, no_deploy => 1);
107 $schema->storage->ensure_connected;
109 is ($schema->storage->transaction_depth, 0, 'Start outside txn');
115 die "$$ starts in txn!" if $s->storage->transaction_depth != 0;
117 die "$$ not in txn!" if $s->storage->transaction_depth == 0;
118 $s->storage->dbh->do('SELECT 1') }
120 die "$$ did not finish txn!" if $s->storage->transaction_depth != 0;
124 $_[0]->storage->dbh->do('SELECT 1');
128 my $guard = $_[0]->txn_scope_guard;
129 $_[0]->storage->dbh->do('SELECT 1');
134 die "Unable to fork: $!\n"
135 if ! defined $pids[-1];
145 is ($schema->storage->transaction_depth, 0, 'Parent still outside txn');
147 for my $pid (@pids) {
149 ok (! $?, "Child $pid exit ok");
153 # Test txn_do/scope_guard with forking: outer txn_do
155 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
157 for my $pass (1..2) {
159 # do something trying to destabilize the depth count
162 my $guard = $schema->txn_scope_guard;
163 $schema->txn_do( sub { die } );
165 $schema->txn_do( sub {
166 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
170 for my $pid ( $schema->txn_do ( sub { _forking_action ($schema) } ) ) {
172 ok (! $?, "Child $pid exit ok (pass $pass)");
173 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
178 # same test with outer guard
180 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
182 for my $pass (1..2) {
184 # do something trying to destabilize the depth count
187 my $guard = $schema->txn_scope_guard;
188 $schema->txn_do( sub { die } );
190 $schema->txn_do( sub {
191 ok ($schema->storage->_dbh->do ('SELECT 1'), "Query after exceptions ok ($_)");
196 my $guard = $schema->txn_scope_guard;
197 _forking_action ($schema);
200 for my $pid (@pids) {
202 ok (! $?, "Child $pid exit ok (pass $pass)");
203 isa_ok ($schema->resultset ('Artist')->find ({ name => "forking action $pid" }), 'DBIx::Class::Row');
208 sub _forking_action {
215 die "Unable to fork: $!\n"
216 if ! defined $pids[-1];
223 $schema->txn_do (sub {
224 my $depth = $schema->storage->transaction_depth;
225 die "$$(txn_do)unexpected txn depth $depth!" if $depth != 1;
226 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
230 my $guard = $schema->txn_scope_guard;
231 my $depth = $schema->storage->transaction_depth;
232 die "$$(scope_guard) unexpected txn depth $depth!" if $depth != 1;
233 $schema->resultset ('Artist')->create ({ name => "forking action $$"});
243 my $fail_code = sub {
245 $artist->create_related('cds', {
246 title => 'this should not exist',
249 die "the sky is falling";
253 my $schema = DBICTest->init_schema;
255 # Test failed txn_do()
258 is( $schema->storage->{transaction_depth}, 0, "txn depth starts at 0 (pass $pass)");
260 my $artist = $schema->resultset('Artist')->find(3);
263 $schema->txn_do($fail_code, $artist);
264 }, qr/the sky is falling/, "failed txn_do threw an exception (pass $pass)");
266 my $cd = $artist->cds({
267 title => 'this should not exist',
270 ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
272 is( $schema->storage->{transaction_depth}, 0, "txn depth has been reset (pass $pass)");
276 # Test failed txn_do() with failed rollback
278 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
280 my $artist = $schema->resultset('Artist')->find(3);
282 # Force txn_rollback() to throw an exception
283 no warnings 'redefine';
287 local *{"DBIx::Class::Storage::DBI::SQLite::txn_rollback"} = sub{
294 $schema->txn_do($fail_code, $artist);
296 qr/the sky is falling.+Rollback failed/s,
297 'txn_rollback threw a rollback exception (and included the original exception'
300 my $cd = $artist->cds({
301 title => 'this should not exist',
304 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
305 q{changed the cds table});
306 $cd->delete; # Rollback failed
308 title => 'this should not exist',
311 ok(!defined($cd), q{deleted the failed txn's cd});
312 $schema->storage->_dbh->rollback;
316 # Test nested failed txn_do()
318 my $schema = DBICTest->init_schema();
320 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
322 my $nested_fail_code = sub {
323 my ($schema, $artist, $code1, $code2) = @_;
325 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
327 $schema->txn_do($code1, $artist, @titles); # successful txn
328 $schema->txn_do($code2, $artist); # failed txn
331 my $artist = $schema->resultset('Artist')->find(3);
334 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
335 }, qr/the sky is falling/, 'nested failed txn_do threw exception');
337 ok(!defined($artist->cds({
338 title => 'nested txn_do test CD '.$_,
340 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
341 my $cd = $artist->cds({
342 title => 'this should not exist',
345 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
348 # Grab a new schema to test txn before connect
350 my $schema = DBICTest->init_schema(no_deploy => 1);
352 $schema->txn_begin();
353 $schema->txn_begin();
354 }, 'Pre-connection nested transactions.');
356 # although not connected DBI would still warn about rolling back at disconnect
357 $schema->txn_rollback;
358 $schema->txn_rollback;
361 # make sure AutoCommit => 0 on external handles behaves correctly with scope_guard
363 my $factory = DBICTest->init_schema (AutoCommit => 0);
364 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
365 my $dbh = $factory->storage->dbh;
367 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
368 my $schema = DBICTest::Schema->connect (sub { $dbh });
371 my $guard = $schema->txn_scope_guard;
372 $schema->resultset('CD')->delete;
374 }, 'No attempt to start a transaction with scope guard');
376 is ($schema->resultset('CD')->count, 0, 'Deletion successful in txn');
378 # this will commit the implicitly started txn
381 } [], 'No warnings on AutoCommit => 0 with txn_guard';
383 # make sure AutoCommit => 0 on external handles behaves correctly with txn_do
385 my $factory = DBICTest->init_schema (AutoCommit => 0);
386 cmp_ok ($factory->resultset('CD')->count, '>', 0, 'Something to delete');
387 my $dbh = $factory->storage->dbh;
389 ok (!$dbh->{AutoCommit}, 'AutoCommit is off on $dbh');
390 my $schema = DBICTest::Schema->connect (sub { $dbh });
394 $schema->txn_do (sub { $schema->resultset ('CD')->delete });
395 }, 'No attempt to start a atransaction with txn_do');
397 is ($schema->resultset('CD')->count, 0, 'Deletion successful');
399 # this will commit the implicitly started txn
402 } [], 'No warnings on AutoCommit => 0 with txn_do';