Add explicit notes wrt DBI/DBD::SQLite dep versions
[dbsrgits/DBIx-Class.git] / t / 50fork.t
CommitLineData
474ab91c 1use strict;
474ab91c 2use warnings;
1346e22d 3use Test::More;
a2f22854 4use Test::Exception;
8d6b1478 5
6use lib qw(t/lib);
7use DBICTest;
199fbc45 8use DBIx::Class::Optional::Dependencies ();
9
8d6b1478 10my $main_pid = $$;
11
199fbc45 12plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
13 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
474ab91c 14
474ab91c 15my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
474ab91c 16
17plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
18 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
19
be21f2eb 20# README: If you set the env var to a number greater than 10,
21# we will use that many children
22my $num_children = $ENV{DBICTEST_FORK_STRESS} || 1;
1346e22d 23if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
24 $num_children = 10;
25}
26
6892eb09 27my $schema = DBICTest::Schema->connect($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 });
a2f22854 44 is ($parent_rs->count, 2);
474ab91c 45};
46ok(!$@) or diag "Creation eval failed: $@";
47
a2f22854 48# basic tests
649bfb8c 49{
a2f22854 50 ok ($schema->storage->connected(), 'Parent is connected');
51 is ($parent_rs->next->id, 1, 'Cursor advanced');
52
53 my ($parent_in, $child_out);
54 pipe( $parent_in, $child_out ) or die "Pipe open failed: $!";
55
56 my $pid = fork;
57 if(!defined $pid) {
58 die "fork failed: $!";
59 }
60
61 if (!$pid) {
62 close $parent_in;
63
64 #simulate a subtest to not confuse the parent TAP emission
65 my $tb = Test::More->builder;
66 $tb->reset;
67 for (qw/output failure_output todo_output/) {
68 close $tb->$_;
69 open ($tb->$_, '>&', $child_out);
649bfb8c 70 }
71
a2f22854 72 ok(!$schema->storage->connected, "storage->connected() false in child");
73 for (1,2) {
74 throws_ok { $parent_rs->next } qr/\QMulti-process access attempted while cursor in progress (position 1)/;
649bfb8c 75 }
76
a2f22854 77 $parent_rs->reset;
78 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
79
80 done_testing;
81 exit 0;
82 }
83
84 close $child_out;
85 while (my $ln = <$parent_in>) {
86 print " $ln";
87 }
88 waitpid( $pid, 0 );
89 ok(!$?, 'Child subtests passed');
90
91 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
92 is ($parent_rs->next, undef, 'Cursor exhausted');
649bfb8c 93}
94
a2f22854 95$parent_rs->reset;
474ab91c 96my @pids;
97while(@pids < $num_children) {
98
99 my $pid = fork;
100 if(!defined $pid) {
101 die "fork failed: $!";
102 }
103 elsif($pid) {
104 push(@pids, $pid);
1346e22d 105 next;
474ab91c 106 }
107
108 $pid = $$;
474ab91c 109
7d216b10 110 my $work = sub {
111 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
112 my $row = $parent_rs->next;
113 $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) })
114 if($row && $row->get_column('artist') =~ /^(?:123|456)$/);
115 };
116
117 # try with and without transactions
118 if ((@pids % 3) == 1) {
119 my $guard = $schema->txn_scope_guard;
120 $work->();
121 $guard->commit;
474ab91c 122 }
7d216b10 123 elsif ((@pids % 3) == 2) {
124 $schema->txn_do ($work);
125 }
126 else {
127 $work->();
128 }
129
474ab91c 130 sleep(3);
7d216b10 131 exit 0;
474ab91c 132}
133
134ok(1, "past forking");
135
7d216b10 136for (@pids) {
137 waitpid($_,0);
138 ok (! $?, "Child $_ exitted cleanly");
139};
474ab91c 140
141ok(1, "past waiting");
142
143while(@pids) {
144 my $pid = pop(@pids);
1346e22d 145 my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
474ab91c 146 is($rs->next->get_column('artist'), $pid, "Child $pid successful");
147}
148
149ok(1, "Made it to the end");
150
8d6b1478 151done_testing;
152
153END {
154 $schema->storage->dbh->do("DROP TABLE cd") if ($schema and $main_pid == $$);
155 undef $schema;
156}