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