1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
13 # README: If you set the env var to a number greater than 10,
14 # we will use that many children
15 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
16 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
20 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
21 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
26 my $dbh = $schema->storage->dbh;
29 local $SIG{__WARN__} = sub {};
30 eval { $dbh->do("DROP TABLE cd") };
31 $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);");
34 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
35 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
37 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
38 is ($parent_rs->count, 2);
40 ok(!$@) or diag "Creation eval failed: $@";
44 ok ($schema->storage->connected(), 'Parent is connected');
45 is ($parent_rs->next->id, 1, 'Cursor advanced');
47 my ($parent_in, $child_out);
48 pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
52 die "fork failed: $!";
58 #simulate a subtest to not confuse the parent TAP emission
59 my $tb = Test::More->builder;
61 for (qw/output failure_output todo_output/) {
63 open ($tb->$_, '>&', $child_out);
66 ok(!$schema->storage->connected, "storage->connected() false in child");
68 throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
72 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
79 while (my $ln = <$parent_in>) {
83 ok(!$?, 'Child subtests passed');
85 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
86 is ($parent_rs->next, undef, 'Cursor exhausted');
91 while(@pids < $num_children) {
95 die "fork failed: $!";
105 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
106 my $row = $parent_rs->next;
107 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
108 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
111 # try with and without transactions
112 if ((@pids % 3) == 1) {
113 my $guard = $schema->txn_scope_guard;
117 elsif ((@pids % 3) == 2) {
118 $schema->txn_do ($work);
128 ok(1, "past forking");
132 ok (! $?, "Child $_ exitted cleanly");
135 ok(1, "past waiting");
138 my $pid = pop(@pids);
139 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
140 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
143 ok(1, "Made it to the end");
148 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);