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);
15 # README: If you set the env var to a number greater than 5,
16 # we will use that many children
17 my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
18 if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
22 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
23 my $schema = DBICTest::Schema->connect($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 });
40 is ($parent_rs->count, 2);
42 ok(!$@) or diag "Creation eval failed: $@";
46 ok ($schema->storage->connected(), 'Parent is connected');
47 is ($parent_rs->next->id, 1, 'Cursor advanced');
49 my ($parent_in, $child_out);
50 pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
54 die "fork failed: $!";
60 #simulate a subtest to not confuse the parent TAP emission
61 my $tb = Test::More->builder;
63 for (qw/output failure_output todo_output/) {
65 open ($tb->$_, '>&', $child_out);
68 ok(!$schema->storage->connected, "storage->connected() false in child");
70 throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
74 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
81 while (my $ln = <$parent_in>) {
85 ok(!$?, 'Child subtests passed');
87 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
88 is ($parent_rs->next, undef, 'Cursor exhausted');
93 # sleep until this spot so everything starts simultaneously
94 # add "until turn of second" for prettier display
95 my $t = int( time() ) + 4;
98 while(@pids < $num_children) {
102 die "fork failed: $!";
111 sleep( max( 0.1, $t - time ) );
112 note ("Child process $pid starting work at " . time() );
115 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
116 my $row = $parent_rs->next;
117 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
118 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
121 # try with and without transactions
122 if ((@pids % 3) == 1) {
123 my $guard = $schema->txn_scope_guard;
127 elsif ((@pids % 3) == 2) {
128 $schema->txn_do ($work);
138 ok(1, "past forking");
142 ok (! $?, "Child $_ exitted cleanly");
145 ok(1, "past waiting");
148 my $pid = pop(@pids);
149 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
150 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
153 ok(1, "Made it to the end");
158 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);