5 # README: If you set the env var to a number greater than 10,
6 # we will use that many children
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
9 my $num_children = $ENV{DBICTEST_FORK_STRESS};
11 plan skip_all => 'Set $ENV{DBICTEST_FORK_STRESS} to run this test'
14 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
15 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
17 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
21 plan tests => $num_children + 6;
25 use_ok('DBICTest::Schema');
27 my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
32 my $dbh = $schema->storage->dbh;
35 local $SIG{__WARN__} = sub {};
36 eval { $dbh->do("DROP TABLE cd") };
37 $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(255) NOT NULL UNIQUE, year VARCHAR(255));");
40 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
41 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
43 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
46 ok(!$@) or diag "Creation eval failed: $@";
51 die "fork failed: $!";
55 exit $schema->storage->connected ? 1 : 0;
58 if (waitpid($pid, 0) == $pid) {
60 ok($ex == 0, "storage->connected() returns false in child");
61 exit $ex if $ex; # skip remaining tests
66 while(@pids < $num_children) {
70 die "fork failed: $!";
79 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
80 my $row = $parent_rs->next;
81 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
82 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
88 ok(1, "past forking");
90 waitpid($_,0) for(@pids);
92 ok(1, "past waiting");
96 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
97 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
100 ok(1, "Made it to the end");
102 $schema->storage->dbh->do("DROP TABLE cd");