@@ -15,14 +15,30 @@ Future<Duration> measureWait(Future<void> future) async {
15
15
}
16
16
17
17
void main () {
18
- test ('BackoffMachine timeouts are random from zero to 100ms, 200ms, 400ms, ...' , () {
18
+ List <Duration > expectedBounds ({
19
+ required int length,
20
+ required Duration firstBound,
21
+ required Duration maxBound,
22
+ }) {
23
+ return List .generate (length, growable: false , (completed) {
24
+ return Duration (microseconds:
25
+ min (maxBound.inMicroseconds,
26
+ (firstBound.inMicroseconds
27
+ * pow (BackoffMachine .base , completed)).round ()));
28
+ });
29
+ }
30
+
31
+ void checkEmpirically ({
32
+ required Duration firstBound, required Duration maxBound}) {
19
33
// This is a randomized test. [numTrials] is chosen so that the failure
20
34
// probability < 1e-9. There are 2 * 11 assertions, and each one has a
21
35
// failure probability < 1e-12; see below.
22
36
const numTrials = 100 ;
23
- final expectedMaxDurations = [
24
- 100 , 200 , 400 , 800 , 1600 , 3200 , 6400 , 10000 , 10000 , 10000 , 10000 ,
25
- ].map ((ms) => Duration (milliseconds: ms)).toList ();
37
+ final expectedMaxDurations = expectedBounds (length: 11 ,
38
+ firstBound: firstBound, maxBound: maxBound);
39
+
40
+ // Check an assumption used in our failure-probability estimates.
41
+ assert (2 * expectedMaxDurations.length < 1000 );
26
42
27
43
final trialResults = List .generate (numTrials, (_) {
28
44
return awaitFakeAsync ((async ) async {
@@ -52,6 +68,21 @@ void main() {
52
68
check (minFromAllTrials).isLessThan ( expectedMax * 0.25 );
53
69
check (maxFromAllTrials).isGreaterThan (expectedMax * 0.75 );
54
70
}
71
+ }
72
+
73
+ test ('BackoffMachine timeouts are random from zero to the intended bounds' , () {
74
+ checkEmpirically (firstBound: const Duration (milliseconds: 100 ),
75
+ maxBound: const Duration (seconds: 10 ));
76
+ });
77
+
78
+ test ('BackoffMachine intended bounds, explicitly' , () {
79
+ // This check on expectedBounds acts as a cross-check on the
80
+ // other test case above, confirming what it is it's checking for.
81
+ final bounds = expectedBounds (length: 11 ,
82
+ firstBound: BackoffMachine .firstBound, maxBound: BackoffMachine .maxBound);
83
+ check (bounds.map ((d) => d.inMilliseconds)).deepEquals ([
84
+ 100 , 200 , 400 , 800 , 1600 , 3200 , 6400 , 10000 , 10000 , 10000 , 10000 ,
85
+ ]);
55
86
});
56
87
57
88
test ('BackoffMachine timeouts are always positive' , () {
0 commit comments