(travis) Strangely `cpan .` does not work in certain configs
[dbsrgits/DBIx-Class.git] / xt / extra / internals / ithread_stress.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use warnings;
4 use strict;
5
6 use Config;
7 BEGIN {
8   my $skipall;
9
10   # FIXME: this discrepancy is crazy, need to investigate
11   my $mem_needed = ($Config{ptrsize} == 4)
12     ? 200
13     : 750
14   ;
15
16   if( ! $Config{useithreads} ) {
17     $skipall = 'your perl does not support ithreads';
18   }
19   elsif( "$]" < 5.008005 ) {
20     $skipall = 'DBIC does not actively support threads before perl 5.8.5';
21   }
22   elsif( $INC{'Devel/Cover.pm'} ) {
23     $skipall = 'Devel::Cover does not work with ithreads yet';
24   }
25   elsif(
26     ! $ENV{DBICTEST_RUN_ALL_TESTS}
27       and
28     require DBICTest::RunMode
29       and
30     ! DBICTest::RunMode->is_smoker
31   ) {
32     $skipall = "Test is too expensive (may use up to ${mem_needed}MB of memory), skipping on non-smoker";
33   }
34   else {
35     require threads;
36     threads->import();
37
38     require DBICTest;
39     # without this the can_alloc may very well shoot half of the CI down
40     DBICTest->import(':GlobalLock');
41
42     unless ( DBICTest::Util::can_alloc_MB($mem_needed) ) {
43       $skipall = "Your system does not have the necessary amount of memory (${mem_needed}MB) for this ridiculous test";
44     }
45   }
46
47   if( $skipall ) {
48     print "1..0 # SKIP $skipall\n";
49     exit 0;
50   }
51 }
52
53 use Test::More;
54 use Errno ();
55 use DBIx::Class::_Util 'sigwarn_silencer';
56 use Time::HiRes qw(time sleep);
57
58 # README: If you set the env var to a number greater than 5,
59 #   we will use that many children
60 my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
61 if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
62    $num_children = 5;
63 }
64
65 my $schema = DBICTest->init_schema(no_deploy => 1);
66 isa_ok ($schema, 'DBICTest::Schema');
67
68 # sleep until this spot so everything starts simultaneously
69 # add "until turn of second" for prettier display
70 my $t = int( time() ) + 4;
71
72 my @threads;
73 SKIP: {
74
75   local $SIG{__WARN__} = sigwarn_silencer( qr/Thread creation failed/i );
76
77   for (1.. $num_children) {
78     push @threads, threads->create(sub {
79       my $tid = threads->tid;
80
81       sleep ($t - time);
82       note ("Thread $tid starting work at " . time() );
83
84       my $rsrc = $schema->source('Artist');
85       undef $schema;
86       isa_ok ($rsrc->schema, 'DBICTest::Schema');
87       my $s2 = $rsrc->schema->clone;
88
89       sleep (0.2); # without this many tasty crashes even on latest perls
90     }) || do {
91       skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1
92         if $! == Errno::EAGAIN();
93
94       die "Unable to start thread: $!";
95     };
96   }
97 }
98
99 ok(1, "past spawning");
100
101 $_->join for @threads;
102
103 ok(1, "past joining");
104
105 # Too many threading bugs on exit, none of which have anything to do with
106 # the actual stuff we test
107 $ENV{DBICTEST_DIRTY_EXIT} = 1
108   if "$]"< 5.012;
109
110 done_testing;