8b22c8d5ce410f5a2bc787ac5f27db7f30f096db
[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 - investigation pending'
13   if $^O eq 'MSWin32' && DBICTest::RunMode->is_plain;
14
15 for my $type (qw/PG MYSQL SQLite/) {
16
17   SKIP: {
18     my @dsn = $type eq 'SQLite'
19       ? DBICTest->_database(sqlite_use_file => 1)
20       : do {
21         skip "Skipping $type tests without DBICTEST_${type}_DSN", 1
22           unless $ENV{"DBICTEST_${type}_DSN"};
23         @ENV{map { "DBICTEST_${type}_${_}" } qw/DSN USER PASS/}
24       }
25     ;
26
27     if ($type eq 'PG') {
28       skip "skipping Pg tests without dependencies installed", 1
29         unless DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_pg');
30     }
31     elsif ($type eq 'MYSQL') {
32       skip "skipping MySQL tests without dependencies installed", 1
33         unless DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_mysql');
34     }
35
36     my $schema = DBICTest::Schema->connect (@dsn);
37
38     # emulate a singleton-factory, just cache the object *somewhere in a different package*
39     # to induce out-of-order destruction
40     $DBICTest::FakeSchemaFactory::schema = $schema;
41
42     # so we can see the retry exceptions (if any)
43     $ENV{DBIC_DBIRETRY_DEBUG} = 1;
44
45     ok (!$schema->storage->connected, "$type: start disconnected");
46
47     lives_ok (sub {
48       $schema->txn_do (sub {
49
50         ok ($schema->storage->connected, "$type: transaction starts connected");
51
52         my $pid = fork();
53         SKIP: {
54           skip "Fork failed: $!", 1 if (! defined $pid);
55
56           if ($pid) {
57             note "Parent $$ sleeping...";
58             wait();
59             note "Parent $$ woken up after child $pid exit";
60           }
61           else {
62             note "Child $$ terminating";
63             undef $DBICTest::FakeSchemaFactory::schema;
64             exit 0;
65           }
66
67           ok ($schema->storage->connected, "$type: parent still connected (in txn_do)");
68         }
69       });
70     });
71
72     ok ($schema->storage->connected, "$type: parent still connected (outside of txn_do)");
73
74     undef $DBICTest::FakeSchemaFactory::schema;
75   }
76 }
77
78 done_testing;