Commit | Line | Data |
1346e22d |
1 | use Config; |
1346e22d |
2 | BEGIN { |
9798dffd |
3 | unless ($Config{useithreads}) { |
4 | print "1..0 # SKIP your perl does not support ithreads\n"; |
5 | exit 0; |
6 | } |
f15baf68 |
7 | |
8 | if ($INC{'Devel/Cover.pm'}) { |
9 | print "1..0 # SKIP Devel::Cover does not work with threads yet\n"; |
10 | exit 0; |
11 | } |
1346e22d |
12 | } |
9798dffd |
13 | use threads; |
1346e22d |
14 | |
cb551b07 |
15 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg'; |
16 | |
9798dffd |
17 | use strict; |
18 | use warnings; |
c76e5262 |
19 | |
9798dffd |
20 | use Test::More; |
8ec03a3a |
21 | use Test::Exception; |
9798dffd |
22 | |
a4367b26 |
23 | plan skip_all => 'DBIC does not actively support threads before perl 5.8.5' |
9798dffd |
24 | if $] < '5.008005'; |
25 | |
8ec03a3a |
26 | use lib qw(t/lib); |
8d6b1478 |
27 | use DBICTest; |
1346e22d |
28 | |
8ec03a3a |
29 | # README: If you set the env var to a number greater than 10, |
30 | # we will use that many children |
be21f2eb |
31 | my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1; |
1346e22d |
32 | if($num_children !~ /^[0-9]+$/ || $num_children < 10) { |
33 | $num_children = 10; |
34 | } |
35 | |
cb551b07 |
36 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; |
ec6415a9 |
37 | |
6892eb09 |
38 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 }); |
1346e22d |
39 | |
40 | my $parent_rs; |
41 | |
ca507a2f |
42 | lives_ok (sub { |
1346e22d |
43 | my $dbh = $schema->storage->dbh; |
44 | |
45 | { |
46 | local $SIG{__WARN__} = sub {}; |
47 | eval { $dbh->do("DROP TABLE cd") }; |
a1cb5921 |
48 | $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);"); |
1346e22d |
49 | } |
50 | |
51 | $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 }); |
52 | $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 }); |
53 | |
54 | $parent_rs = $schema->resultset('CD')->search({ year => 1901 }); |
a2f22854 |
55 | is ($parent_rs->count, 2); |
ca507a2f |
56 | }, 'populate successfull'); |
1346e22d |
57 | |
a2f22854 |
58 | # basic tests |
59 | { |
60 | ok ($schema->storage->connected(), 'Parent is connected'); |
61 | is ($parent_rs->next->id, 1, 'Cursor advanced'); |
62 | my $ct_num = Test::More->builder->current_test; |
63 | |
64 | my $newthread = async { |
65 | my $out = ''; |
66 | |
67 | #simulate a subtest to not confuse the parent TAP emission |
68 | my $tb = Test::More->builder; |
69 | $tb->reset; |
70 | for (qw/output failure_output todo_output/) { |
71 | close $tb->$_; |
72 | open ($tb->$_, '>', \$out); |
73 | } |
74 | |
75 | ok(!$schema->storage->connected, "storage->connected() false in child"); |
76 | for (1,2) { |
77 | throws_ok { $parent_rs->next } qr/\QMulti-thread access attempted while cursor in progress (position 1)/; |
78 | } |
79 | |
80 | $parent_rs->reset; |
81 | is($parent_rs->next->id, 1, 'Resetting cursor reprepares it within child environment'); |
82 | |
83 | done_testing; |
84 | |
85 | close $tb->$_ for (qw/output failure_output todo_output/); |
86 | sleep(1); # tasty crashes without this |
87 | |
88 | $out; |
89 | }; |
90 | die "Thread creation failed: $! $@" if !defined $newthread; |
91 | |
92 | my $out = $newthread->join; |
93 | $out =~ s/^/ /gm; |
94 | print $out; |
95 | |
96 | # workaround for older Test::More confusing the plan under threads |
97 | Test::More->builder->current_test($ct_num); |
98 | |
99 | is ($parent_rs->next->id, 2, 'Cursor still intact in parent'); |
100 | is ($parent_rs->next, undef, 'Cursor exhausted'); |
101 | } |
102 | |
103 | $parent_rs->reset; |
1346e22d |
104 | my @children; |
105 | while(@children < $num_children) { |
106 | |
107 | my $newthread = async { |
108 | my $tid = threads->tid; |
1346e22d |
109 | |
110 | my $child_rs = $schema->resultset('CD')->search({ year => 1901 }); |
111 | my $row = $parent_rs->next; |
112 | if($row && $row->get_column('artist') =~ /^(?:123|456)$/) { |
113 | $schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) }); |
114 | } |
9798dffd |
115 | sleep(1); # tasty crashes without this |
1346e22d |
116 | }; |
117 | die "Thread creation failed: $! $@" if !defined $newthread; |
118 | push(@children, $newthread); |
119 | } |
120 | |
121 | ok(1, "past spawning"); |
122 | |
123 | { |
124 | $_->join for(@children); |
125 | } |
126 | |
127 | ok(1, "past joining"); |
128 | |
129 | while(@children) { |
130 | my $child = pop(@children); |
131 | my $tid = $child->tid; |
132 | my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) }); |
133 | is($rs->next->get_column('artist'), $tid, "Child $tid successful"); |
134 | } |
135 | |
136 | ok(1, "Made it to the end"); |
a2f22854 |
137 | undef $parent_rs; |
1346e22d |
138 | |
139 | $schema->storage->dbh->do("DROP TABLE cd"); |
8ec03a3a |
140 | |
141 | done_testing; |