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