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