7 use DBIx::Class::Optional::Dependencies ();
11 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
16 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
17 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
19 # README: If you set the env var to a number greater than 10,
20 # we will use that many children
21 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
22 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
26 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
31 my $dbh = $schema->storage->dbh;
34 local $SIG{__WARN__} = sub {};
35 eval { $dbh->do("DROP TABLE cd") };
36 $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);");
39 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
40 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
42 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
45 ok(!$@) or diag "Creation eval failed: $@";
50 die "fork failed: $!";
54 exit $schema->storage->connected ? 1 : 0;
57 if (waitpid($pid, 0) == $pid) {
59 ok($ex == 0, "storage->connected() returns false in child");
60 exit $ex if $ex; # skip remaining tests
65 while(@pids < $num_children) {
69 die "fork failed: $!";
79 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
80 my $row = $parent_rs->next;
81 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
82 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
85 # try with and without transactions
86 if ((@pids % 3) == 1) {
87 my $guard = $schema->txn_scope_guard;
91 elsif ((@pids % 3) == 2) {
92 $schema->txn_do ($work);
102 ok(1, "past forking");
106 ok (! $?, "Child $_ exitted cleanly");
109 ok(1, "past waiting");
112 my $pid = pop(@pids);
113 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
114 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
117 ok(1, "Made it to the end");
122 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);