Test for restricted prefetch (now passing again after previous commit)
[dbsrgits/DBIx-Class.git] / t / 51threadtxn.t
1 # README: If you set the env var to a number greater than 10,
2 #   we will use that many children
3
4 use Config;
5 BEGIN {
6   unless ($Config{useithreads}) {
7     print "1..0 # SKIP your perl does not support ithreads\n";
8     exit 0;
9   }
10 }
11 use threads;
12
13 use strict;
14 use warnings;
15
16 use Test::More;
17
18 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
19   if $] < '5.008005';
20
21 use DBIx::Class::Optional::Dependencies ();
22 use Scalar::Util 'weaken';
23 use lib qw(t/lib);
24 use DBICTest;
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 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
31   unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
32
33 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
34 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
35    $num_children = 10;
36 }
37
38 use_ok('DBICTest::Schema');
39
40 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
41
42 my $parent_rs;
43
44 eval {
45     my $dbh = $schema->storage->dbh;
46
47     {
48         local $SIG{__WARN__} = sub {};
49         eval { $dbh->do("DROP TABLE cd") };
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);");
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 });
57     $parent_rs->next;
58 };
59 ok(!$@) or diag "Creation eval failed: $@";
60
61 my @children;
62 while(@children < $num_children) {
63
64     my $newthread = async {
65         my $tid = threads->tid;
66         weaken(my $weak_schema = $schema);
67         weaken(my $weak_parent_rs = $parent_rs);
68         $schema->txn_do(sub {
69             my $child_rs = $weak_schema->resultset('CD')->search({ year => 1901 });
70             my $row = $weak_parent_rs->next;
71             if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
72                 $weak_schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
73             }
74         });
75         sleep(1);  # tasty crashes without this
76     };
77     die "Thread creation failed: $! $@" if !defined $newthread;
78     push(@children, $newthread);
79 }
80
81 ok(1, "past spawning");
82
83 {
84     $_->join for(@children);
85 }
86
87 ok(1, "past joining");
88
89 while(@children) {
90     my $child = pop(@children);
91     my $tid = $child->tid;
92     my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
93     is($rs->next->get_column('artist'), $tid, "Child $tid successful");
94 }
95
96 ok(1, "Made it to the end");
97
98 $schema->storage->dbh->do("DROP TABLE cd");
99
100 done_testing;