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