Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / t / storage / global_destruction.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use DBIx::Class::Optional::Dependencies ();
8
9 use lib qw(t/lib);
10 use DBICTest;
11
12 plan skip_all => 'Test segfaults on Win32' if $^O eq 'MSWin32';
13
14 for my $type (qw/PG MYSQL/) {
15
16   SKIP: {
17     skip "Skipping $type tests without DBICTEST_${type}_DSN", 1
18       unless $ENV{"DBICTEST_${type}_DSN"};
19
20     if ($type eq 'PG') {
21       skip "skipping Pg tests without dependencies installed", 1
22         unless DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_pg');
23     }
24     elsif ($type eq 'MYSQL') {
25       skip "skipping MySQL tests without dependencies installed", 1
26         unless DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_mysql');
27     }
28
29     my $schema = DBICTest::Schema->connect (@ENV{map { "DBICTEST_${type}_${_}" } qw/DSN USER PASS/});
30
31     # emulate a singleton-factory, just cache the object *somewhere in a different package*
32     # to induce out-of-order destruction
33     $DBICTest::FakeSchemaFactory::schema = $schema;
34
35     # so we can see the retry exceptions (if any)
36     $ENV{DBIC_DBIRETRY_DEBUG} = 1;
37
38     ok (!$schema->storage->connected, "$type: start disconnected");
39
40     lives_ok (sub {
41       $schema->txn_do (sub {
42
43         ok ($schema->storage->connected, "$type: transaction starts connected");
44
45         my $pid = fork();
46         SKIP: {
47           skip "Fork failed: $!", 1 if (! defined $pid);
48
49           if ($pid) {
50             note "Parent $$ sleeping...";
51             wait();
52             note "Parent $$ woken up after child $pid exit";
53           }
54           else {
55             note "Child $$ terminating";
56             undef $DBICTest::FakeSchemaFactory::schema;
57             exit 0;
58           }
59
60           ok ($schema->storage->connected, "$type: parent still connected (in txn_do)");
61         }
62       });
63     });
64
65     ok ($schema->storage->connected, "$type: parent still connected (outside of txn_do)");
66
67     undef $DBICTest::FakeSchemaFactory::schema;
68   }
69 }
70
71 done_testing;