| 1 |
use Test2::V0 -target => 'Test2::Harness::Collector'; |
| 2 |
use File::Spec; |
| 3 |
use File::Temp qw/tempdir/; |
| 4 |
use Time::HiRes qw/time/; |
| 5 |
|
| 6 |
use Test2::Harness::Run; |
| 7 |
|
| 8 |
my $tmp = tempdir(CLEANUP => 1); |
| 9 |
my $run_id = 'test-run'; |
| 10 |
mkdir(File::Spec->catdir($tmp, $run_id)) or die "Could not create run dir: $!"; |
| 11 |
|
| 12 |
sub new_collector { |
| 13 |
return $CLASS->new( |
| 14 |
run => Test2::Harness::Run->new(run_id => $run_id), |
| 15 |
workdir => $tmp, |
| 16 |
run_id => $run_id, |
| 17 |
action => sub { }, |
| 18 |
@_, |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
subtest defaults => sub { |
| 23 |
my $one = new_collector(); |
| 24 |
|
| 25 |
is($one->wait_time, 0.02, "wait_time defaults to 0.02"); |
| 26 |
is($one->idle_wait, $CLASS->MIN_WAIT, "backoff starts at the floor"); |
| 27 |
}; |
| 28 |
|
| 29 |
subtest wait_time_is_settable => sub { |
| 30 |
my $one = new_collector(wait_time => 0.5); |
| 31 |
|
| 32 |
is($one->wait_time, 0.5, "wait_time can be set by the constructor"); |
| 33 |
}; |
| 34 |
|
| 35 |
subtest backoff_doubles_and_caps => sub { |
| 36 |
my $one = new_collector(wait_time => 0.008); |
| 37 |
|
| 38 |
my @seen; |
| 39 |
for (1 .. 5) { |
| 40 |
push @seen => $one->idle_wait; |
| 41 |
$one->idle_sleep; |
| 42 |
} |
| 43 |
|
| 44 |
is( |
| 45 |
\@seen, |
| 46 |
[0.001, 0.002, 0.004, 0.008, 0.008], |
| 47 |
"each sleep doubles the wait until it reaches the wait_time ceiling, then holds" |
| 48 |
); |
| 49 |
|
| 50 |
is($one->idle_wait, 0.008, "wait stays at the ceiling"); |
| 51 |
}; |
| 52 |
|
| 53 |
subtest reset_returns_to_the_floor => sub { |
| 54 |
my $one = new_collector(wait_time => 0.008); |
| 55 |
|
| 56 |
$one->idle_sleep for 1 .. 4; |
| 57 |
is($one->idle_wait, 0.008, "backed off to the ceiling"); |
| 58 |
|
| 59 |
$one->reset_idle_wait; |
| 60 |
is($one->idle_wait, $CLASS->MIN_WAIT, "reset returns to the floor"); |
| 61 |
}; |
| 62 |
|
| 63 |
subtest ceiling_below_the_floor_is_honored => sub { |
| 64 |
my $one = new_collector(wait_time => 0.0005); |
| 65 |
|
| 66 |
$one->idle_sleep; |
| 67 |
is($one->idle_wait, 0.0005, "a wait_time under the floor caps the very first backoff"); |
| 68 |
}; |
| 69 |
|
| 70 |
subtest idle_sleep_actually_sleeps => sub { |
| 71 |
my $one = new_collector(); |
| 72 |
|
| 73 |
my $start = time; |
| 74 |
$one->idle_sleep; |
| 75 |
my $slept = time - $start; |
| 76 |
|
| 77 |
# Deliberately loose: this asserts that a sleep happens at all, not how |
| 78 |
# precisely the scheduler honors the requested duration. |
| 79 |
ok($slept >= 0.0005, "idle_sleep waited", "slept ${slept}s"); |
| 80 |
}; |
| 81 |
|
| 82 |
done_testing; |