Resolve $rsrc instance duality on metadata traversal
[dbsrgits/DBIx-Class.git] / t / 51threads.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use Config;
4 BEGIN {
5   unless ($Config{useithreads}) {
6     print "1..0 # SKIP your perl does not support ithreads\n";
7     exit 0;
8   }
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   }
14 }
15 use threads;
16
17 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
18
19 use strict;
20 use warnings;
21
22 use Test::More;
23 use Test::Exception;
24 use Time::HiRes qw(time sleep);
25 use List::Util 'max';
26
27 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
28   if "$]" < 5.008005;
29
30
31 use DBICTest;
32
33 # README: If you set the env var to a number greater than 5,
34 #   we will use that many children
35 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
36 if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
37    $num_children = 5;
38 }
39
40 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
41
42 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
43
44 my $parent_rs;
45
46 lives_ok (sub {
47     my $dbh = $schema->storage->dbh;
48
49     {
50         local $SIG{__WARN__} = sub {};
51         eval { $dbh->do("DROP TABLE cd") };
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);");
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 });
59     is ($parent_rs->count, 2);
60 }, 'populate successfull');
61
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/);
90     sleep (0.2); # tasty crashes without this
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;
108
109 # sleep until this spot so everything starts simultaneously
110 # add "until turn of second" for prettier display
111 my $t = int( time() ) + 4;
112
113 my @children;
114 while(@children < $num_children) {
115
116     my $newthread = async {
117         my $tid = threads->tid;
118
119         sleep( max( 0.1, $t - time ) );
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
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         }
131
132         sleep (0.2); # without this many tasty crashes even on latest perls
133     };
134     die "Thread creation failed: $! $@" if !defined $newthread;
135     push(@children, $newthread);
136 }
137
138 ok(1, "past spawning");
139
140 my @tids;
141 for (@children) {
142   push @tids, $_->tid;
143   $_->join;
144 }
145
146 ok(1, "past joining");
147
148 while (@tids) {
149     my $tid = pop @tids;
150     my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@tids) });
151     is($rs->next->get_column('artist'), $tid, "Child $tid successful");
152 }
153
154 ok(1, "Made it to the end");
155 undef $parent_rs;
156
157 $schema->storage->dbh->do("DROP TABLE cd");
158
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
164 done_testing;