Move tmpdir() to DBICTest::Util where it belongs
[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;
8d6b1478 8
c0329273 9
8d6b1478 10use DBICTest;
199fbc45 11
8d6b1478 12my $main_pid = $$;
13
be21f2eb 14# README: If you set the env var to a number greater than 10,
15# we will use that many children
16my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
1346e22d 17if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
18 $num_children = 10;
19}
20
cb551b07 21my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6892eb09 22my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
474ab91c 23
1346e22d 24my $parent_rs;
474ab91c 25
26eval {
1346e22d 27 my $dbh = $schema->storage->dbh;
474ab91c 28
1346e22d 29 {
30 local $SIG{__WARN__} = sub {};
31 eval { $dbh->do("DROP TABLE cd") };
a1cb5921 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);");
1346e22d 33 }
474ab91c 34
1346e22d 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 });
474ab91c 37
1346e22d 38 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
a2f22854 39 is ($parent_rs->count, 2);
474ab91c 40};
41ok(!$@) or diag "Creation eval failed: $@";
42
a2f22854 43# basic tests
649bfb8c 44{
a2f22854 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);
649bfb8c 65 }
66
a2f22854 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)/;
649bfb8c 70 }
71
a2f22854 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');
649bfb8c 88}
89
a2f22854 90$parent_rs->reset;
474ab91c 91my @pids;
92while(@pids < $num_children) {
93
94 my $pid = fork;
95 if(!defined $pid) {
96 die "fork failed: $!";
97 }
98 elsif($pid) {
99 push(@pids, $pid);
1346e22d 100 next;
474ab91c 101 }
102
103 $pid = $$;
474ab91c 104
7d216b10 105 my $work = sub {
106 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
107 my $row = $parent_rs->next;
108 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
109 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
110 };
111
112 # try with and without transactions
113 if ((@pids % 3) == 1) {
114 my $guard = $schema->txn_scope_guard;
115 $work->();
116 $guard->commit;
474ab91c 117 }
7d216b10 118 elsif ((@pids % 3) == 2) {
119 $schema->txn_do ($work);
120 }
121 else {
122 $work->();
123 }
124
474ab91c 125 sleep(3);
7d216b10 126 exit 0;
474ab91c 127}
128
129ok(1, "past forking");
130
7d216b10 131for (@pids) {
132 waitpid($_,0);
133 ok (! $?, "Child $_ exitted cleanly");
134};
474ab91c 135
136ok(1, "past waiting");
137
138while(@pids) {
139 my $pid = pop(@pids);
1346e22d 140 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
474ab91c 141 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
142}
143
144ok(1, "Made it to the end");
145
8d6b1478 146done_testing;
147
148END {
149 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);
150 undef $schema;
151}