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