Add import-time action stub to OptDeps, switch distbuild checks to it
[dbsrgits/DBIx-Class.git] / t / 51threadtxn.t
CommitLineData
39fa10f5 1# README: If you set the env var to a number greater than 10,
2# we will use that many children
3
9798dffd 4use Config;
39fa10f5 5BEGIN {
9798dffd 6 unless ($Config{useithreads}) {
7 print "1..0 # SKIP your perl does not support ithreads\n";
8 exit 0;
9 }
f15baf68 10
11 if ($INC{'Devel/Cover.pm'}) {
12 print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
13 exit 0;
14 }
39fa10f5 15}
9798dffd 16use threads;
39fa10f5 17
9798dffd 18use strict;
19use warnings;
c76e5262 20
9798dffd 21use Test::More;
22
a4367b26 23plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
9798dffd 24 if $] < '5.008005';
c76e5262 25
199fbc45 26use DBIx::Class::Optional::Dependencies ();
6892eb09 27use Scalar::Util 'weaken';
8ec03a3a 28use lib qw(t/lib);
8d6b1478 29use DBICTest;
39fa10f5 30
31my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
39fa10f5 32plan 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);
34
49b3a264 35plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
36 unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
8ec03a3a 37
be21f2eb 38my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
39fa10f5 39if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
40 $num_children = 10;
41}
42
39fa10f5 43use_ok('DBICTest::Schema');
44
6892eb09 45my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
39fa10f5 46
47my $parent_rs;
48
49eval {
50 my $dbh = $schema->storage->dbh;
51
52 {
53 local $SIG{__WARN__} = sub {};
54 eval { $dbh->do("DROP TABLE cd") };
a1cb5921 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);");
39fa10f5 56 }
57
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 });
60
61 $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
a2f22854 62 is ($parent_rs->count, 2);
39fa10f5 63};
64ok(!$@) or diag "Creation eval failed: $@";
65
66my @children;
67while(@children < $num_children) {
68
69 my $newthread = async {
70 my $tid = threads->tid;
6892eb09 71 weaken(my $weak_schema = $schema);
72 weaken(my $weak_parent_rs = $parent_rs);
39fa10f5 73 $schema->txn_do(sub {
6892eb09 74 my $child_rs = $weak_schema->resultset('CD')->search({ year => 1901 });
75 my $row = $weak_parent_rs->next;
39fa10f5 76 if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
6892eb09 77 $weak_schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
39fa10f5 78 }
79 });
9798dffd 80 sleep(1); # tasty crashes without this
39fa10f5 81 };
82 die "Thread creation failed: $! $@" if !defined $newthread;
83 push(@children, $newthread);
84}
85
86ok(1, "past spawning");
87
88{
89 $_->join for(@children);
90}
91
92ok(1, "past joining");
93
94while(@children) {
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");
99}
100
101ok(1, "Made it to the end");
102
103$schema->storage->dbh->do("DROP TABLE cd");
8ec03a3a 104
105done_testing;