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