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