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