1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
8 use Time::HiRes qw(time sleep);
14 # README: If you set the env var to a number greater than 5,
15 # we will use that many children
16 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
17 if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
21 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
22 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1 });
27 my $dbh = $schema->storage->dbh;
30 local $SIG{__WARN__} = sub {};
31 eval { $dbh->do("DROP TABLE cd") };
32 $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);");
35 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
36 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
38 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
39 is ($parent_rs->count, 2);
41 ok(!$@) or diag "Creation eval failed: $@";
45 ok ($schema->storage->connected(), 'Parent is connected');
46 is ($parent_rs->next->id, 1, 'Cursor advanced');
48 my ($parent_in, $child_out);
49 pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
53 die "fork failed: $!";
59 #simulate a subtest to not confuse the parent TAP emission
60 my $tb = Test::More->builder;
62 for (qw/output failure_output todo_output/) {
64 open ($tb->$_, '>&', $child_out);
67 ok(!$schema->storage->connected, "storage->connected() false in child");
69 throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
73 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
80 while (my $ln = <$parent_in>) {
84 ok(!$?, 'Child subtests passed');
86 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
87 is ($parent_rs->next, undef, 'Cursor exhausted');
92 # sleep until this spot so everything starts simultaneously
93 # add "until turn of second" for prettier display
94 my $t = int( time() ) + 4;
97 while(@pids < $num_children) {
101 die "fork failed: $!";
111 note ("Child process $pid starting work at " . time() );
114 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
115 my $row = $parent_rs->next;
116 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
117 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
120 # try with and without transactions
121 if ((@pids % 3) == 1) {
122 my $guard = $schema->txn_scope_guard;
126 elsif ((@pids % 3) == 2) {
127 $schema->txn_do ($work);
137 ok(1, "past forking");
141 ok (! $?, "Child $_ exitted cleanly");
144 ok(1, "past waiting");
147 my $pid = pop(@pids);
148 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
149 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
152 ok(1, "Made it to the end");
157 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);