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