refactor code needing version
[dbsrgits/DBIx-Class.git] / t / 51threads.t
1 use Config;
2 BEGIN {
3   unless ($Config{useithreads}) {
4     print "1..0 # SKIP your perl does not support ithreads\n";
5     exit 0;
6   }
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   }
12 }
13 use threads;
14
15 use strict;
16 use warnings;
17
18 use Test::More;
19 use Test::Exception;
20
21 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
22   if $] < '5.008005';
23
24 use DBIx::Class::Optional::Dependencies ();
25 use lib qw(t/lib);
26 use DBICTest;
27
28 plan 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
31 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
32 plan 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
37 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
38 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
39    $num_children = 10;
40 }
41
42 use_ok('DBICTest::Schema');
43
44 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
45
46 my $parent_rs;
47
48 lives_ok (sub {
49     my $dbh = $schema->storage->dbh;
50
51     {
52         local $SIG{__WARN__} = sub {};
53         eval { $dbh->do("DROP TABLE cd") };
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);");
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 });
61     is ($parent_rs->count, 2);
62 }, 'populate successfull');
63
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;
110 my @children;
111 while(@children < $num_children) {
112
113     my $newthread = async {
114         my $tid = threads->tid;
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         }
121         sleep(1); # tasty crashes without this
122     };
123     die "Thread creation failed: $! $@" if !defined $newthread;
124     push(@children, $newthread);
125 }
126
127 ok(1, "past spawning");
128
129 {
130     $_->join for(@children);
131 }
132
133 ok(1, "past joining");
134
135 while(@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
142 ok(1, "Made it to the end");
143 undef $parent_rs;
144
145 $schema->storage->dbh->do("DROP TABLE cd");
146
147 done_testing;