1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#!perl
use strict;
use warnings;
use Test::Exception;
use Test::More 0.98;
#use Test::RandomResults;
use Algorithm::Backoff::Constant;
# XXX also test max_attempts for each strategy
subtest "attr: max_attempts" => sub {
my $ar;
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
max_attempts => 0,
);
isnt($ar->failure(1), -1);
isnt($ar->failure(2), -1);
isnt($ar->failure(3), -1);
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
max_attempts => 1,
);
is($ar->failure(1), -1);
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
max_attempts => 2,
);
isnt($ar->failure(1), -1);
is ($ar->failure(1), -1);
$ar->success(1);
isnt($ar->failure(1), -1);
is ($ar->failure(1), -1);
};
# XXX also test max_actual_duration for each strategy
subtest "attr: max_actual_duration" => sub {
my $ar;
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
max_actual_duration => 4,
max_attempts => 3,
_start_timestamp => 0,
);
isnt($ar->failure(0), -1);
isnt($ar->failure(2), -1);
is ($ar->failure(4), -1);
};
# XXX also test consider_actual_delay for each strategy
subtest "attr: consider_actual_delay" => sub {
my $ar;
$ar = Algorithm::Backoff::Constant->new(
consider_actual_delay => 1,
delay => 2,
max_attempts => 0,
);
is($ar->failure(1), 2);
# we didn't wait, so the delay is now 2+2 = 4
is($ar->failure(1), 4);
# we now waited for 5 seconds, so delay is now 2-1 = 1
is($ar->failure(6), 1);
# we now waited for 2 seconds, so delay is now 2-1 = 1
is($ar->failure(8), 1);
# we now waited for 3 seconds, so delay is now 2-2 = 0
is($ar->failure(11), 0);
};
# This tests that consider_actual_delay uses the post-processed _prev_delay
# value correctly.
subtest "attr: consider_actual_delay + post-processing" => sub {
my $ar;
$ar = Algorithm::Backoff::Constant->new(
consider_actual_delay => 1,
delay => 3, # "pre-processor" delay
max_delay => 2, # real delay
max_attempts => 0,
);
# first failure after 1 second
is($ar->failure(1), 2);
# 2s delay + instant failure, so the delay is now 3 -> max 2
is($ar->failure(1+2+0), 2);
# 2s delay + 2s failure, so the delay is now 3-2 = 1
is($ar->failure(3+2+2), 1);
# 1s delay + 5s failure, so delay is now 3-5 = -2 -> min 0
is($ar->failure(7+1+5), 0);
# 0s delay + instant failure, so delay is now 3 -> max 2
is($ar->failure(13+0+0), 2);
};
# XXX also test jitter_factor for each strategy
subtest "attr: jitter_factor" => sub {
my $ar = Algorithm::Backoff::Constant->new(
delay => 2,
delay_on_success => 3,
jitter_factor => 0.1,
);
rand_between_ok(sub { $ar->failure(1) }, 2*(1-0.1), 2*(1+0.1));
rand_between_ok(sub { $ar->success(1) }, 3*(1-0.1), 3*(1+0.1));
# jittered delay still doesn't violate min_delay and max_delay
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
delay_on_success => 2,
min_delay => 1.8,
max_delay => 2.2,
jitter_factor => 0.5,
);
rand_between_ok(sub { $ar->failure(1) }, 1.8, 2.2);
$ar = Algorithm::Backoff::Constant->new(
delay => 2,
delay_on_success => 3,
min_delay => 2.8,
max_delay => 3.2,
jitter_factor => 0.5,
);
rand_between_ok(sub { $ar->success(1) }, 2.8, 3.2);
};
subtest "timestamp must not decrease" => sub {
my $ar = Algorithm::Backoff::Constant->new(
delay => 2,
);
$ar->success(2);
dies_ok { $ar->success(1) };
};
DONE_TESTING:
done_testing;
# XXX temporary function
sub rand_between_ok(&$$) {
my ($block, $min, $max, $name) = @_;
my @res;
my %res;
for (1..30) {
my $res = $block->();
do {
ok(0, "Result #$_ is not between $min and $max ($res)");
return;
} if $res < $min || $res > $max;
push @res, $res;
$res{ $res+0 }++;
}
note "Results: ", explain(\@res);
keys(%res) > 1 or
ok(0, "Results do not seem to be random, but constant $res[0]");
ok(1, "Results are random between $min and $max");
}
|