Fix annoying warnings on innocent looking MSSQL code
[dbsrgits/DBIx-Class.git] / t / 51threads.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
1346e22d 3use Config;
1346e22d 4BEGIN {
9798dffd 5 unless ($Config{useithreads}) {
6 print "1..0 # SKIP your perl does not support ithreads\n";
7 exit 0;
8 }
f15baf68 9
10 if ($INC{'Devel/Cover.pm'}) {
11 print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
12 exit 0;
13 }
1346e22d 14}
9798dffd 15use threads;
1346e22d 16
cb551b07 17use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
18
9798dffd 19use strict;
20use warnings;
c76e5262 21
9798dffd 22use Test::More;
8ec03a3a 23use Test::Exception;
10dd5c05 24use Time::HiRes qw(time sleep);
9798dffd 25
a4367b26 26plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
750a4ad2 27 if "$]" < 5.008005;
9798dffd 28
c0329273 29
8d6b1478 30use DBICTest;
1346e22d 31
10dd5c05 32# README: If you set the env var to a number greater than 5,
8ec03a3a 33# we will use that many children
be21f2eb 34my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
10dd5c05 35if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
36 $num_children = 5;
1346e22d 37}
38
cb551b07 39my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
ec6415a9 40
6892eb09 41my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
1346e22d 42
43my $parent_rs;
44
ca507a2f 45lives_ok (sub {
1346e22d 46 my $dbh = $schema->storage->dbh;
47
48 {
49 local $SIG{__WARN__} = sub {};
50 eval { $dbh->do("DROP TABLE cd") };
a1cb5921 51 $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 52 }
53
54 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
55 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
56
57 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
a2f22854 58 is ($parent_rs->count, 2);
ca507a2f 59}, 'populate successfull');
1346e22d 60
a2f22854 61# basic tests
62{
63 ok ($schema->storage->connected(), 'Parent is connected');
64 is ($parent_rs->next->id, 1, 'Cursor advanced');
65 my $ct_num = Test::More->builder->current_test;
66
67 my $newthread = async {
68 my $out = '';
69
70 #simulate a subtest to not confuse the parent TAP emission
71 my $tb = Test::More->builder;
72 $tb->reset;
73 for (qw/output failure_output todo_output/) {
74 close $tb->$_;
75 open ($tb->$_, '>', \$out);
76 }
77
78 ok(!$schema->storage->connected, "storage->connected() false in child");
79 for (1,2) {
80 throws_ok { $parent_rs->next } qr/\QMulti-thread access attempted while cursor in progress (position 1)/;
81 }
82
83 $parent_rs->reset;
84 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
85
86 done_testing;
87
88 close $tb->$_ for (qw/output failure_output todo_output/);
10dd5c05 89 sleep (0.2); # tasty crashes without this
a2f22854 90
91 $out;
92 };
93 die "Thread creation failed: $! $@" if !defined $newthread;
94
95 my $out = $newthread->join;
96 $out =~ s/^/ /gm;
97 print $out;
98
99 # workaround for older Test::More confusing the plan under threads
100 Test::More->builder->current_test($ct_num);
101
102 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
103 is ($parent_rs->next, undef, 'Cursor exhausted');
104}
105
106$parent_rs->reset;
10dd5c05 107
108# sleep until this spot so everything starts simultaneously
109# add "until turn of second" for prettier display
110my $t = int( time() ) + 4;
111
1346e22d 112my @children;
113while(@children < $num_children) {
114
115 my $newthread = async {
116 my $tid = threads->tid;
1346e22d 117
10dd5c05 118 sleep ($t - time);
119
120 # FIXME if we do not stagger the threads, sparks fly due to CXSA
121 sleep ( $tid / 10 ) if "$]" < 5.012;
122
123 note ("Thread $tid starting work at " . time() );
124
1346e22d 125 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
126 my $row = $parent_rs->next;
127 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
128 $schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
129 }
10dd5c05 130
131 sleep (0.2); # without this many tasty crashes even on latest perls
1346e22d 132 };
133 die "Thread creation failed: $! $@" if !defined $newthread;
134 push(@children, $newthread);
135}
136
137ok(1, "past spawning");
138
10dd5c05 139my @tids;
140for (@children) {
141 push @tids, $_->tid;
142 $_->join;
1346e22d 143}
144
145ok(1, "past joining");
146
10dd5c05 147while (@tids) {
148 my $tid = pop @tids;
149 my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@tids) });
1346e22d 150 is($rs->next->get_column('artist'), $tid, "Child $tid successful");
151}
152
153ok(1, "Made it to the end");
a2f22854 154undef $parent_rs;
1346e22d 155
156$schema->storage->dbh->do("DROP TABLE cd");
8ec03a3a 157
10dd5c05 158# Too many threading bugs on exit, none of which have anything to do with
159# the actual stuff we test
160$ENV{DBICTEST_DIRTY_EXIT} = 1
161 if "$]" < 5.012;
162
8ec03a3a 163done_testing;