Skip to content

Commit 967d539

Browse files
committed
backoff test [nfc]: Pull out checkEmpirically
1 parent 7bdc6da commit 967d539

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

test/api/backoff_test.dart

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,30 @@ Future<Duration> measureWait(Future<void> future) async {
1515
}
1616

1717
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}) {
1933
// This is a randomized test. [numTrials] is chosen so that the failure
2034
// probability < 1e-9. There are 2 * 11 assertions, and each one has a
2135
// failure probability < 1e-12; see below.
2236
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);
2642

2743
final trialResults = List.generate(numTrials, (_) {
2844
return awaitFakeAsync((async) async {
@@ -52,6 +68,21 @@ void main() {
5268
check(minFromAllTrials).isLessThan( expectedMax * 0.25);
5369
check(maxFromAllTrials).isGreaterThan(expectedMax * 0.75);
5470
}
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+
]);
5586
});
5687

5788
test('BackoffMachine timeouts are always positive', () {

0 commit comments

Comments
 (0)