| 1 |
package TestBarrier; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use Time::HiRes qw/sleep time/; |
| 6 |
|
| 7 |
use Exporter qw/import/; |
| 8 |
|
| 9 |
our @EXPORT_OK = qw/barrier_wait/; |
| 10 |
|
| 11 |
# Fixtures that have to be running at the same time as their siblings used to |
| 12 |
# sleep and hope the overlap landed inside the sleep. That makes the fixture |
| 13 |
# slow, and it makes the assertion a coin flip on a machine slow enough to |
| 14 |
# spend the whole sleep starting the next job. |
| 15 |
# |
| 16 |
# This makes the overlap a fact instead: every participant records that it |
| 17 |
# arrived, then waits until as many participants have arrived as the run |
| 18 |
# expects. |
| 19 |
# |
| 20 |
# Markers are never removed. A participant that starts after enough others |
| 21 |
# have already arrived therefore does not wait at all, which is what keeps a |
| 22 |
# run with more tests than slots from stalling on its last, unpaired test. |
| 23 |
sub barrier_wait { |
| 24 |
my %params = @_; |
| 25 |
|
| 26 |
my $dir = $params{dir} // $ENV{TEST_BARRIER_DIR} |
| 27 |
or die "No barrier directory (pass one, or set TEST_BARRIER_DIR)"; |
| 28 |
|
| 29 |
my $want = $params{count} // $ENV{TEST_BARRIER_COUNT} // 2; |
| 30 |
|
| 31 |
# A ceiling, not a wait. It only comes into play when something is wrong |
| 32 |
# and the other participants are never going to arrive; reaching it fails |
| 33 |
# the assertion the barrier exists to make, which is the point. |
| 34 |
my $ceiling = $params{ceiling} // $ENV{TEST_BARRIER_CEILING} // 30; |
| 35 |
|
| 36 |
open(my $fh, '>', "$dir/$$") or die "Could not write barrier marker: $!"; |
| 37 |
close($fh); |
| 38 |
|
| 39 |
my $start = time; |
| 40 |
while (1) { |
| 41 |
opendir(my $dh, $dir) or die "Could not open barrier dir '$dir': $!"; |
| 42 |
my $have = grep { !m/^\.\.?$/ } readdir($dh); |
| 43 |
closedir($dh); |
| 44 |
|
| 45 |
return $have if $have >= $want; |
| 46 |
return $have if time - $start > $ceiling; |
| 47 |
|
| 48 |
sleep 0.05; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
1; |