5 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
7 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
8 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
10 # README: If you set the env var to a number greater than 10,
11 # we will use that many children
12 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
13 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
17 plan tests => ($num_children*2) + 6;
21 use_ok('DBICTest::Schema');
23 my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
28 my $dbh = $schema->storage->dbh;
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);");
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 });
39 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
42 ok(!$@) or diag "Creation eval failed: $@";
47 die "fork failed: $!";
51 exit $schema->storage->connected ? 1 : 0;
54 if (waitpid($pid, 0) == $pid) {
56 ok($ex == 0, "storage->connected() returns false in child");
57 exit $ex if $ex; # skip remaining tests
62 while(@pids < $num_children) {
66 die "fork failed: $!";
76 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
77 my $row = $parent_rs->next;
78 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
79 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
82 # try with and without transactions
83 if ((@pids % 3) == 1) {
84 my $guard = $schema->txn_scope_guard;
88 elsif ((@pids % 3) == 2) {
89 $schema->txn_do ($work);
99 ok(1, "past forking");
103 ok (! $?, "Child $_ exitted cleanly");
106 ok(1, "past waiting");
109 my $pid = pop(@pids);
110 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
111 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
114 ok(1, "Made it to the end");
116 $schema->storage->dbh->do("DROP TABLE cd");