1 # README: If you set the env var to a number greater than 10,
2 # we will use that many children
6 unless ($Config{useithreads}) {
7 print "1..0 # SKIP your perl does not support ithreads\n";
11 if ($INC{'Devel/Cover.pm'}) {
12 print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
23 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
26 use DBIx::Class::Optional::Dependencies ();
27 use Scalar::Util 'weaken';
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 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
36 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
38 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
39 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
43 use_ok('DBICTest::Schema');
45 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
50 my $dbh = $schema->storage->dbh;
53 local $SIG{__WARN__} = sub {};
54 eval { $dbh->do("DROP TABLE cd") };
55 $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);");
58 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
59 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
61 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
62 is ($parent_rs->count, 2);
64 ok(!$@) or diag "Creation eval failed: $@";
67 while(@children < $num_children) {
69 my $newthread = async {
70 my $tid = threads->tid;
71 weaken(my $weak_schema = $schema);
72 weaken(my $weak_parent_rs = $parent_rs);
74 my $child_rs = $weak_schema->resultset('CD')->search({ year => 1901 });
75 my $row = $weak_parent_rs->next;
76 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
77 $weak_schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
80 sleep(1); # tasty crashes without this
82 die "Thread creation failed: $! $@" if !defined $newthread;
83 push(@children, $newthread);
86 ok(1, "past spawning");
89 $_->join for(@children);
92 ok(1, "past joining");
95 my $child = pop(@children);
96 my $tid = $child->tid;
97 my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
98 is($rs->next->get_column('artist'), $tid, "Child $tid successful");
101 ok(1, "Made it to the end");
103 $schema->storage->dbh->do("DROP TABLE cd");