Simplify debugfh rebuilding after clearing
[dbsrgits/DBIx-Class.git] / t / 50fork.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my $main_pid = $$;
12
13 # README: If you set the env var to a number greater than 10,
14 #   we will use that many children
15 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
16 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
17    $num_children = 10;
18 }
19
20 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
21 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
22
23 my $parent_rs;
24
25 eval {
26     my $dbh = $schema->storage->dbh;
27
28     {
29         local $SIG{__WARN__} = sub {};
30         eval { $dbh->do("DROP TABLE cd") };
31         $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);");
32     }
33
34     $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
35     $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
36
37     $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
38     is ($parent_rs->count, 2);
39 };
40 ok(!$@) or diag "Creation eval failed: $@";
41
42 # basic tests
43 {
44   ok ($schema->storage->connected(), 'Parent is connected');
45   is ($parent_rs->next->id, 1, 'Cursor advanced');
46
47   my ($parent_in, $child_out);
48   pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
49
50   my $pid = fork;
51   if(!defined $pid) {
52     die "fork failed: $!";
53   }
54
55   if (!$pid) {
56     close $parent_in;
57
58     #simulate a  subtest to not confuse the parent TAP emission
59     my $tb = Test::More->builder;
60     $tb->reset;
61     for (qw/output failure_output todo_output/) {
62       close $tb->$_;
63       open ($tb->$_, '>&', $child_out);
64     }
65
66     ok(!$schema->storage->connected, "storage->connected() false in child");
67     for (1,2) {
68       throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
69     }
70
71     $parent_rs->reset;
72     is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
73
74     done_testing;
75     exit 0;
76   }
77
78   close $child_out;
79   while (my $ln = <$parent_in>) {
80     print "   $ln";
81   }
82   waitpid( $pid, 0 );
83   ok(!$?, 'Child subtests passed');
84
85   is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
86   is ($parent_rs->next, undef, 'Cursor exhausted');
87 }
88
89 $parent_rs->reset;
90 my @pids;
91 while(@pids < $num_children) {
92
93     my $pid = fork;
94     if(!defined $pid) {
95         die "fork failed: $!";
96     }
97     elsif($pid) {
98         push(@pids, $pid);
99         next;
100     }
101
102     $pid = $$;
103
104     my $work = sub {
105       my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
106       my $row = $parent_rs->next;
107       $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
108         if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
109     };
110
111     # try with and without transactions
112     if ((@pids % 3) == 1) {
113       my $guard = $schema->txn_scope_guard;
114       $work->();
115       $guard->commit;
116     }
117     elsif ((@pids % 3) == 2) {
118       $schema->txn_do ($work);
119     }
120     else {
121       $work->();
122     }
123
124     sleep(3);
125     exit 0;
126 }
127
128 ok(1, "past forking");
129
130 for (@pids) {
131   waitpid($_,0);
132   ok (! $?, "Child $_ exitted cleanly");
133 };
134
135 ok(1, "past waiting");
136
137 while(@pids) {
138     my $pid = pop(@pids);
139     my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
140     is($rs->next->get_column('artist'), $pid, "Child $pid successful");
141 }
142
143 ok(1, "Made it to the end");
144
145 done_testing;
146
147 END {
148   $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);
149   undef $schema;
150 }