3 unless ($Config{useithreads}) {
4 print "1..0 # SKIP your perl does not support ithreads\n";
8 if ($INC{'Devel/Cover.pm'}) {
9 print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
21 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
24 use DBIx::Class::Optional::Dependencies ();
28 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
29 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
31 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
32 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
33 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
35 # README: If you set the env var to a number greater than 10,
36 # we will use that many children
37 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
38 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
42 use_ok('DBICTest::Schema');
44 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
49 my $dbh = $schema->storage->dbh;
52 local $SIG{__WARN__} = sub {};
53 eval { $dbh->do("DROP TABLE cd") };
54 $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);");
57 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
58 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
60 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
61 is ($parent_rs->count, 2);
62 }, 'populate successfull');
66 ok ($schema->storage->connected(), 'Parent is connected');
67 is ($parent_rs->next->id, 1, 'Cursor advanced');
68 my $ct_num = Test::More->builder->current_test;
70 my $newthread = async {
73 #simulate a subtest to not confuse the parent TAP emission
74 my $tb = Test::More->builder;
76 for (qw/output failure_output todo_output/) {
78 open ($tb->$_, '>', \$out);
81 ok(!$schema->storage->connected, "storage->connected() false in child");
83 throws_ok { $parent_rs->next } qr/\QMulti-thread access attempted while cursor in progress (position 1)/;
87 is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment');
91 close $tb->$_ for (qw/output failure_output todo_output/);
92 sleep(1); # tasty crashes without this
96 die "Thread creation failed: $! $@" if !defined $newthread;
98 my $out = $newthread->join;
102 # workaround for older Test::More confusing the plan under threads
103 Test::More->builder->current_test($ct_num);
105 is ($parent_rs->next->id, 2, 'Cursor still intact in parent');
106 is ($parent_rs->next, undef, 'Cursor exhausted');
111 while(@children < $num_children) {
113 my $newthread = async {
114 my $tid = threads->tid;
116 my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
117 my $row = $parent_rs->next;
118 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
119 $schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
121 sleep(1); # tasty crashes without this
123 die "Thread creation failed: $! $@" if !defined $newthread;
124 push(@children, $newthread);
127 ok(1, "past spawning");
130 $_->join for(@children);
133 ok(1, "past joining");
136 my $child = pop(@children);
137 my $tid = $child->tid;
138 my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
139 is($rs->next->get_column('artist'), $tid, "Child $tid successful");
142 ok(1, "Made it to the end");
145 $schema->storage->dbh->do("DROP TABLE cd");