deno.land / std@0.224.0 / async / retry_test.ts

retry_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { retry, RetryError } from "./retry.ts";import { assertEquals, assertRejects } from "../assert/mod.ts";import { FakeTime } from "../testing/time.ts";
function generateErroringFunction(errorsBeforeSucceeds: number) { let errorCount = 0;
return () => { if (errorCount >= errorsBeforeSucceeds) { return errorCount; } errorCount++; throw `Only errored ${errorCount} times`; };}
Deno.test("retry()", async () => { const threeErrors = generateErroringFunction(3); const result = await retry(threeErrors, { minTimeout: 100, }); assertEquals(result, 3);});
Deno.test("retry() fails after max errors is passed", async () => { const fiveErrors = generateErroringFunction(5); await assertRejects(() => retry(fiveErrors, { minTimeout: 100, }) );});
Deno.test("retry() waits four times by default", async () => { let callCount = 0; const onlyErrors = function () { callCount++; throw new Error("Failure"); }; using time = new FakeTime(); const callCounts: Array<number> = []; const promise = retry(onlyErrors); queueMicrotask(() => callCounts.push(callCount)); await time.next(); queueMicrotask(() => callCounts.push(callCount)); await time.next(); queueMicrotask(() => callCounts.push(callCount)); await time.next(); queueMicrotask(() => callCounts.push(callCount)); await time.next(); queueMicrotask(() => callCounts.push(callCount)); await assertRejects(() => promise, RetryError); assertEquals(callCounts, [1, 2, 3, 4, 5]);});
Deno.test( "retry() throws if minTimeout is less than maxTimeout", async () => { await assertRejects(() => retry(() => {}, { minTimeout: 1000, maxTimeout: 100, }) ); },);
Deno.test( "retry() throws if maxTimeout is less than 0", async () => { await assertRejects(() => retry(() => {}, { maxTimeout: -1, }) ); },);
Deno.test( "retry() throws if jitter is bigger than 1", async () => { await assertRejects(() => retry(() => {}, { jitter: 2, }) ); },);
Deno.test("retry() checks backoff function timings", async (t) => { const originalMathRandom = Math.random;
await t.step("wait fixed times without jitter", async () => { using time = new FakeTime(); let resolved = false; const checkResolved = async () => { try { await retry(() => { throw new Error("Failure"); }, { jitter: 0 }); } catch { resolved = true; } };
const promise = checkResolved(); const startTime = time.now;
await time.nextAsync(); assertEquals(time.now - startTime, 1000);
await time.nextAsync(); assertEquals(time.now - startTime, 3000);
await time.nextAsync(); assertEquals(time.now - startTime, 7000);
await time.nextAsync(); assertEquals(time.now - startTime, 15000); assertEquals(resolved, false);
await time.runMicrotasks(); assertEquals(time.now - startTime, 15000); assertEquals(resolved, true);
await time.runAllAsync(); assertEquals(time.now - startTime, 15000); await promise; });
Math.random = originalMathRandom;});
std

Version Info

Tagged at
6 months ago