fork tests/code improved, ithreads tests/code added, version bumped
[dbsrgits/DBIx-Class.git] / t / 50fork.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 # README: If you set the env var to a number greater than 10,
6 #   we will use that many children
7
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
9 my $num_children = $ENV{DBICTEST_FORK_STRESS};
10
11 plan skip_all => 'Set $ENV{DBICTEST_FORK_STRESS} to run this test'
12     unless $num_children;
13
14 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
15       . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
16
17 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
18    $num_children = 10;
19 }
20
21 plan tests => $num_children + 5;
22
23 use lib qw(t/lib);
24
25 use_ok('DBICTest::Schema');
26
27 my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
28
29 my $parent_rs;
30
31 eval {
32     my $dbh = $schema->storage->dbh;
33
34     {
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(255) NOT NULL UNIQUE, year VARCHAR(255));");
38     }
39
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 });
42
43     $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
44     $parent_rs->next;
45 };
46 ok(!$@) or diag "Creation eval failed: $@";
47
48 my @pids;
49 while(@pids < $num_children) {
50
51     my $pid = fork;
52     if(!defined $pid) {
53         die "fork failed: $!";
54     }
55     elsif($pid) {
56         push(@pids, $pid);
57         next;
58     }
59
60     $pid = $$;
61
62     my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
63     my $row = $parent_rs->next;
64     if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
65         $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
66     }
67     sleep(3);
68     exit;
69 }
70
71 ok(1, "past forking");
72
73 waitpid($_,0) for(@pids);
74
75 ok(1, "past waiting");
76
77 while(@pids) {
78     my $pid = pop(@pids);
79     my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
80     is($rs->next->get_column('artist'), $pid, "Child $pid successful");
81 }
82
83 ok(1, "Made it to the end");
84
85 $schema->storage->dbh->do("DROP TABLE cd");