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