Start known issues changelog section - place it on top for clarity
[dbsrgits/DBIx-Class.git] / t / 50fork.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
cb551b07 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
3
474ab91c 4use strict;
474ab91c 5use warnings;
1346e22d 6use Test::More;
a2f22854 7use Test::Exception;
10dd5c05 8use Time::HiRes qw(time sleep);
ef25a429 9use List::Util 'max';
c0329273 10
8d6b1478 11use DBICTest;
199fbc45 12
8d6b1478 13my $main_pid = $$;
14
10dd5c05 15# README: If you set the env var to a number greater than 5,
be21f2eb 16# we will use that many children
17my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
10dd5c05 18if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
19 $num_children = 5;
1346e22d 20}
21
cb551b07 22my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6892eb09 23my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
474ab91c 24
1346e22d 25my $parent_rs;
474ab91c 26
27eval {
1346e22d 28 my $dbh = $schema->storage->dbh;
474ab91c 29
1346e22d 30 {
31 local $SIG{__WARN__} = sub {};
32 eval { $dbh->do("DROP TABLE cd") };
a1cb5921 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);");
1346e22d 34 }
474ab91c 35
1346e22d 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 });
474ab91c 38
1346e22d 39 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
a2f22854 40 is ($parent_rs->count, 2);
474ab91c 41};
42ok(!$@) or diag "Creation eval failed: $@";
43
a2f22854 44# basic tests
649bfb8c 45{
a2f22854 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);
649bfb8c 66 }
67
a2f22854 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)/;
649bfb8c 71 }
72
a2f22854 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');
649bfb8c 89}
90
a2f22854 91$parent_rs->reset;
10dd5c05 92
93# sleep until this spot so everything starts simultaneously
94# add "until turn of second" for prettier display
95my $t = int( time() ) + 4;
96
474ab91c 97my @pids;
98while(@pids < $num_children) {
99
100 my $pid = fork;
101 if(!defined $pid) {
102 die "fork failed: $!";
103 }
104 elsif($pid) {
105 push(@pids, $pid);
1346e22d 106 next;
474ab91c 107 }
108
109 $pid = $$;
474ab91c 110
ef25a429 111 sleep( max( 0.1, $t - time ) );
10dd5c05 112 note ("Child process $pid starting work at " . time() );
113
7d216b10 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;
474ab91c 126 }
7d216b10 127 elsif ((@pids % 3) == 2) {
128 $schema->txn_do ($work);
129 }
130 else {
131 $work->();
132 }
133
10dd5c05 134 sleep(2);
7d216b10 135 exit 0;
474ab91c 136}
137
138ok(1, "past forking");
139
7d216b10 140for (@pids) {
141 waitpid($_,0);
142 ok (! $?, "Child $_ exitted cleanly");
143};
474ab91c 144
145ok(1, "past waiting");
146
147while(@pids) {
148 my $pid = pop(@pids);
1346e22d 149 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
474ab91c 150 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
151}
152
153ok(1, "Made it to the end");
154
8d6b1478 155done_testing;
156
157END {
158 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);
159 undef $schema;
160}