Massive cleanup of transaction handlers
[dbsrgits/DBIx-Class.git] / t / 50fork.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 # README: If you set the env var to a number greater than 10,
6 #   we will use that many children
7
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
9 my $num_children = $ENV{DBICTEST_FORK_STRESS};
10
11 plan skip_all => 'Set $ENV{DBICTEST_FORK_STRESS} to run this test'
12     unless $num_children;
13
14 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
15       . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
16
17 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
18    $num_children = 10;
19 }
20
21 plan tests => ($num_children*2) + 6;
22
23 use lib qw(t/lib);
24
25 use_ok('DBICTest::Schema');
26
27 my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
28
29 my $parent_rs;
30
31 eval {
32     my $dbh = $schema->storage->dbh;
33
34     {
35         local $SIG{__WARN__} = sub {};
36         eval { $dbh->do("DROP TABLE cd") };
37         $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER, single_track INTEGER);");
38     }
39
40     $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
41     $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
42
43     $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
44     $parent_rs->next;
45 };
46 ok(!$@) or diag "Creation eval failed: $@";
47
48 {
49     my $pid = fork;
50     if(!defined $pid) {
51         die "fork failed: $!";
52     }
53
54     if (!$pid) {
55         exit $schema->storage->connected ? 1 : 0;
56     }
57
58     if (waitpid($pid, 0) == $pid) {
59         my $ex = $? >> 8;
60         ok($ex == 0, "storage->connected() returns false in child");
61         exit $ex if $ex; # skip remaining tests
62     }
63 }
64
65 my @pids;
66 while(@pids < $num_children) {
67
68     my $pid = fork;
69     if(!defined $pid) {
70         die "fork failed: $!";
71     }
72     elsif($pid) {
73         push(@pids, $pid);
74         next;
75     }
76
77     $pid = $$;
78
79     my $work = sub {
80       my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
81       my $row = $parent_rs->next;
82       $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
83         if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
84     };
85
86     # try with and without transactions
87     if ((@pids % 3) == 1) {
88       my $guard = $schema->txn_scope_guard;
89       $work->();
90       $guard->commit;
91     }
92     elsif ((@pids % 3) == 2) {
93       $schema->txn_do ($work);
94     }
95     else {
96       $work->();
97     }
98
99     sleep(3);
100     exit 0;
101 }
102
103 ok(1, "past forking");
104
105 for (@pids) {
106   waitpid($_,0);
107   ok (! $?, "Child $_ exitted cleanly");
108 };
109
110 ok(1, "past waiting");
111
112 while(@pids) {
113     my $pid = pop(@pids);
114     my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
115     is($rs->next->get_column('artist'), $pid, "Child $pid successful");
116 }
117
118 ok(1, "Made it to the end");
119
120 $schema->storage->dbh->do("DROP TABLE cd");