fix subtle bug with Sybase database type determination
[dbsrgits/DBIx-Class.git] / t / 50fork.t
CommitLineData
474ab91c 1use strict;
474ab91c 2use warnings;
1346e22d 3use Test::More;
474ab91c 4
1346e22d 5# README: If you set the env var to a number greater than 10,
6# we will use that many children
474ab91c 7
8my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
1346e22d 9my $num_children = $ENV{DBICTEST_FORK_STRESS};
474ab91c 10
11plan skip_all => 'Set $ENV{DBICTEST_FORK_STRESS} to run this test'
1346e22d 12 unless $num_children;
474ab91c 13
14plan 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
1346e22d 17if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
18 $num_children = 10;
19}
20
649bfb8c 21plan tests => $num_children + 6;
474ab91c 22
23use lib qw(t/lib);
24
25use_ok('DBICTest::Schema');
26
1346e22d 27my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
474ab91c 28
1346e22d 29my $parent_rs;
474ab91c 30
31eval {
1346e22d 32 my $dbh = $schema->storage->dbh;
474ab91c 33
1346e22d 34 {
35 local $SIG{__WARN__} = sub {};
36 eval { $dbh->do("DROP TABLE cd") };
a1cb5921 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);");
1346e22d 38 }
474ab91c 39
1346e22d 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 });
474ab91c 42
1346e22d 43 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
44 $parent_rs->next;
474ab91c 45};
46ok(!$@) or diag "Creation eval failed: $@";
47
649bfb8c 48{
49 my $pid = fork;
50 if(!defined $pid) {
51 die "fork failed: $!";
52 }
53
54 if (!$pid) {
55 exit $schema->storage->connected ? 1 : 0;
56 }
57
58 if (waitpid($pid, 0) == $pid) {
59 my $ex = $? >> 8;
60 ok($ex == 0, "storage->connected() returns false in child");
61 exit $ex if $ex; # skip remaining tests
62 }
63}
64
474ab91c 65my @pids;
66while(@pids < $num_children) {
67
68 my $pid = fork;
69 if(!defined $pid) {
70 die "fork failed: $!";
71 }
72 elsif($pid) {
73 push(@pids, $pid);
1346e22d 74 next;
474ab91c 75 }
76
77 $pid = $$;
474ab91c 78
1346e22d 79 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
80 my $row = $parent_rs->next;
81 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
82 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
474ab91c 83 }
84 sleep(3);
85 exit;
86}
87
88ok(1, "past forking");
89
90waitpid($_,0) for(@pids);
91
92ok(1, "past waiting");
93
94while(@pids) {
95 my $pid = pop(@pids);
1346e22d 96 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
474ab91c 97 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
98}
99
100ok(1, "Made it to the end");
101
1346e22d 102$schema->storage->dbh->do("DROP TABLE cd");