Skip to content

Commit 34f1a4d

Browse files
committed
backoff: add reset method to reset to minimal wait
1 parent 9044a9a commit 34f1a4d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/api/backoff.dart

+6
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ class BackoffMachine {
4949

5050
_waitsCompleted++;
5151
}
52+
53+
/// Reset the machine to its initial state with minimal wait.
54+
void reset() {
55+
_startTime = null;
56+
_waitsCompleted = 0;
57+
}
5258
}

test/api/backoff_test.dart

+13
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,17 @@ void main() {
5151
check(maxFromAllTrials).isGreaterThan(expectedMax * 0.75);
5252
}
5353
});
54+
55+
test('BackoffMachine resets duration', () async {
56+
final backoffMachine = BackoffMachine();
57+
await measureWait(backoffMachine.wait());
58+
final duration2 = await measureWait(backoffMachine.wait());
59+
check(backoffMachine.waitsCompleted).equals(2);
60+
61+
backoffMachine.reset();
62+
check(backoffMachine.waitsCompleted).equals(0);
63+
final durationReset1 = await measureWait(backoffMachine.wait());
64+
check(durationReset1).isLessThan(duration2);
65+
check(backoffMachine.waitsCompleted).equals(1);
66+
});
5467
}

0 commit comments

Comments
 (0)