1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
5 unless ($Config{useithreads}) {
6 print "1..0 # SKIP your perl does not support ithreads\n";
10 if ($INC{'Devel/Cover.pm'}) {
11 print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
17 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
24 use Time::HiRes qw(time sleep);
27 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
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) {
40 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
42 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
47 my $dbh = $schema->storage->dbh;
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);");
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 });
58 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
59 is ($parent_rs->count, 2);
60 }, 'populate successfull');
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;
68 my $newthread = async {
71 #simulate a subtest to not confuse the parent TAP emission
72 my $tb = Test::More->builder;
74 for (qw/output failure_output todo_output/) {
76 open ($tb->$_, '>', \$out);
79 ok(!$schema->storage->connected, "storage->connected() false in child");
81 throws_ok { $parent_rs->next } qr/\QMulti-thread access attempted while cursor in progress (position 1)/;
85 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
89 close $tb->$_ for (qw/output failure_output todo_output/);
90 sleep (0.2); # tasty crashes without this
94 die "Thread creation failed: $! $@" if !defined $newthread;
96 my $out = $newthread->join;
100 # workaround for older Test::More confusing the plan under threads
101 Test::More->builder->current_test($ct_num);
103 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
104 is ($parent_rs->next, undef, 'Cursor exhausted');
109 # sleep until this spot so everything starts simultaneously
110 # add "until turn of second" for prettier display
111 my $t = int( time() ) + 4;
114 while(@children < $num_children) {
116 my $newthread = async {
117 my $tid = threads->tid;
119 sleep( max( 0.1, $t - time ) );
121 # FIXME if we do not stagger the threads, sparks fly due to CXSA
122 sleep ( $tid / 10 ) if "$]" < 5.012;
124 note ("Thread $tid starting work at " . time() );
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) });
132 sleep (0.2); # without this many tasty crashes even on latest perls
134 die "Thread creation failed: $! $@" if !defined $newthread;
135 push(@children, $newthread);
138 ok(1, "past spawning");
146 ok(1, "past joining");
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");
154 ok(1, "Made it to the end");
157 $schema->storage->dbh->do("DROP TABLE cd");
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