Fix thinko from 10dd5c05 - make sure we actually sleep
[dbsrgits/DBIx-Class.git] / xt / extra / internals / ithread_stress.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
24fbd7fb 3use warnings;
4use strict;
5
a4367b26 6use Config;
7BEGIN {
24fbd7fb 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";
a4367b26 33 }
24fbd7fb 34 else {
35 require threads;
36 threads->import();
f15baf68 37
24fbd7fb 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";
f15baf68 49 exit 0;
50 }
a4367b26 51}
a4367b26 52
a4367b26 53use Test::More;
e48635f7 54use Errno ();
c5915b45 55use DBIx::Class::_Util 'sigwarn_silencer';
10dd5c05 56use Time::HiRes qw(time sleep);
ef25a429 57use List::Util 'max';
a4367b26 58
10dd5c05 59# README: If you set the env var to a number greater than 5,
a4367b26 60# we will use that many children
61my $num_children = $ENV{DBICTEST_THREAD_STRESS} || 1;
10dd5c05 62if($num_children !~ /^[0-9]+$/ || $num_children < 5) {
63 $num_children = 5;
a4367b26 64}
65
66my $schema = DBICTest->init_schema(no_deploy => 1);
67isa_ok ($schema, 'DBICTest::Schema');
68
10dd5c05 69# sleep until this spot so everything starts simultaneously
70# add "until turn of second" for prettier display
71my $t = int( time() ) + 4;
72
a4367b26 73my @threads;
c5915b45 74SKIP: {
75
76 local $SIG{__WARN__} = sigwarn_silencer( qr/Thread creation failed/i );
77
78 for (1.. $num_children) {
79 push @threads, threads->create(sub {
10dd5c05 80 my $tid = threads->tid;
81
ef25a429 82 sleep( max( 0.1, $t - time ) );
10dd5c05 83 note ("Thread $tid starting work at " . time() );
84
c5915b45 85 my $rsrc = $schema->source('Artist');
86 undef $schema;
87 isa_ok ($rsrc->schema, 'DBICTest::Schema');
88 my $s2 = $rsrc->schema->clone;
89
10dd5c05 90 sleep (0.2); # without this many tasty crashes even on latest perls
c5915b45 91 }) || do {
92 skip "EAGAIN encountered, your system is likely bogged down: skipping rest of test", 1
93 if $! == Errno::EAGAIN();
94
95 die "Unable to start thread: $!";
96 };
97 }
98}
99
a4367b26 100ok(1, "past spawning");
101
102$_->join for @threads;
24fbd7fb 103
a4367b26 104ok(1, "past joining");
105
10dd5c05 106# Too many threading bugs on exit, none of which have anything to do with
107# the actual stuff we test
108$ENV{DBICTEST_DIRTY_EXIT} = 1
109 if "$]"< 5.012;
110
a4367b26 111done_testing;