8 use DBIx::Class::Optional::Dependencies ();
12 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
13 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
15 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
17 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
18 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
20 # README: If you set the env var to a number greater than 10,
21 # we will use that many children
22 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
23 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
27 my $schema = DBICTest::Schema->connect($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(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER, single_track INTEGER);");
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 });
44 is ($parent_rs->count, 2);
46 ok(!$@) or diag "Creation eval failed: $@";
50 ok ($schema->storage->connected(), 'Parent is connected');
51 is ($parent_rs->next->id, 1, 'Cursor advanced');
53 my ($parent_in, $child_out);
54 pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
58 die "fork failed: $!";
64 #simulate a subtest to not confuse the parent TAP emission
65 my $tb = Test::More->builder;
67 for (qw/output failure_output todo_output/) {
69 open ($tb->$_, '>&', $child_out);
72 ok(!$schema->storage->connected, "storage->connected() false in child");
74 throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
78 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
85 while (my $ln = <$parent_in>) {
89 ok(!$?, 'Child subtests passed');
91 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
92 is ($parent_rs->next, undef, 'Cursor exhausted');
97 while(@pids < $num_children) {
101 die "fork failed: $!";
111 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
112 my $row = $parent_rs->next;
113 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
114 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
117 # try with and without transactions
118 if ((@pids % 3) == 1) {
119 my $guard = $schema->txn_scope_guard;
123 elsif ((@pids % 3) == 2) {
124 $schema->txn_do ($work);
134 ok(1, "past forking");
138 ok (! $?, "Child $_ exitted cleanly");
141 ok(1, "past waiting");
144 my $pid = pop(@pids);
145 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
146 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
149 ok(1, "Made it to the end");
154 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);