Fix default insert on Oracle ( $rs->create({}) with empty hashref )
[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   if ($INC{'Devel/Cover.pm'}) {
12     print "1..0 # SKIP Devel::Cover does not work with threads yet\n";
13     exit 0;
14   }
15 }
16 use threads;
17
18 use strict;
19 use warnings;
20
21 use Test::More;
22
23 plan skip_all => 'DBIC does not actively support threads before perl 5.8.5'
24   if $] < '5.008005';
25
26 use DBIx::Class::Optional::Dependencies ();
27 use Scalar::Util 'weaken';
28 use lib qw(t/lib);
29 use DBICTest;
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 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
36   unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
37
38 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
39 if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
40    $num_children = 10;
41 }
42
43 use_ok('DBICTest::Schema');
44
45 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
46
47 my $parent_rs;
48
49 eval {
50     my $dbh = $schema->storage->dbh;
51
52     {
53         local $SIG{__WARN__} = sub {};
54         eval { $dbh->do("DROP TABLE cd") };
55         $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);");
56     }
57
58     $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
59     $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
60
61     $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
62     is ($parent_rs->count, 2);
63 };
64 ok(!$@) or diag "Creation eval failed: $@";
65
66 my @children;
67 while(@children < $num_children) {
68
69     my $newthread = async {
70         my $tid = threads->tid;
71         weaken(my $weak_schema = $schema);
72         weaken(my $weak_parent_rs = $parent_rs);
73         $schema->txn_do(sub {
74             my $child_rs = $weak_schema->resultset('CD')->search({ year => 1901 });
75             my $row = $weak_parent_rs->next;
76             if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
77                 $weak_schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
78             }
79         });
80         sleep(1);  # tasty crashes without this
81     };
82     die "Thread creation failed: $! $@" if !defined $newthread;
83     push(@children, $newthread);
84 }
85
86 ok(1, "past spawning");
87
88 {
89     $_->join for(@children);
90 }
91
92 ok(1, "past joining");
93
94 while(@children) {
95     my $child = pop(@children);
96     my $tid = $child->tid;
97     my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
98     is($rs->next->get_column('artist'), $tid, "Child $tid successful");
99 }
100
101 ok(1, "Made it to the end");
102
103 $schema->storage->dbh->do("DROP TABLE cd");
104
105 done_testing;