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