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";
18 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
21 use DBIx::Class::Optional::Dependencies ();
22 use Scalar::Util 'weaken';
26 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
27 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
28 . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
30 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
31 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
33 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
34 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
38 use_ok('DBICTest::Schema');
40 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
45 my $dbh = $schema->storage->dbh;
48 local $SIG{__WARN__} = sub {};
49 eval { $dbh->do("DROP TABLE cd") };
50 $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 $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
54 $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
56 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
59 ok(!$@) or diag "Creation eval failed: $@";
62 while(@children < $num_children) {
64 my $newthread = async {
65 my $tid = threads->tid;
66 weaken(my $weak_schema = $schema);
67 weaken(my $weak_parent_rs = $parent_rs);
69 my $child_rs = $weak_schema->resultset('CD')->search({ year => 1901 });
70 my $row = $weak_parent_rs->next;
71 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
72 $weak_schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
75 sleep(1); # tasty crashes without this
77 die "Thread creation failed: $! $@" if !defined $newthread;
78 push(@children, $newthread);
81 ok(1, "past spawning");
84 $_->join for(@children);
87 ok(1, "past joining");
90 my $child = pop(@children);
91 my $tid = $child->tid;
92 my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
93 is($rs->next->get_column('artist'), $tid, "Child $tid successful");
96 ok(1, "Made it to the end");
98 $schema->storage->dbh->do("DROP TABLE cd");