-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
129 lines (98 loc) · 2.79 KB
/
test.js
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
const test = require('ava')
const backoff = require('./index')
test('executes a synchronous function', async t => {
t.is(await backoff(() => 'result'), 'result')
})
test('executes an asynchronous function', async t => {
const work = backoff(async () => {
await sleep(10)
return 'result'
})
t.is(await work, 'result')
})
test('after maxAttempts an error is thrown', async t => {
let count = 0
const work = backoff(async () => {
count++
throw new Error('123')
}, { maxAttempts: 5 })
await t.throwsAsync(async () => {
await work
}, { instanceOf: backoff.OperationFailedError, message: 'exceeded maximum attempts, operation failed' })
t.is(count, 5)
})
test('if throwMaxAttemptsError flag is set to false then no error is thrown when maxAttempts is exceeded', async t => {
const work = backoff(async () => {
throw new Error('123')
}, { maxAttempts: 5, throwMaxAttemptsError: false })
await t.notThrowsAsync(async () => {
await work
})
})
test('execute a function multiple times if it throws an error', async t => {
const maxAttempts = 5
let actualRetryCount = 0
const work = backoff((attempt) => {
if (attempt === maxAttempts) {
return 'result'
}
actualRetryCount++
throw new Error()
})
const result = await work
t.is(result, 'result')
t.is(actualRetryCount, maxAttempts)
})
test('iteration API', async t => {
const maxAttempts = 5
let actualRetryCount = 0
const iterator = backoff.iterator((attempt) => {
if (attempt === maxAttempts) {
return 'result'
}
throw new Error('blabla')
})
for await (let attempt of iterator) {
actualRetryCount++
if (attempt === 3) break
}
t.is(actualRetryCount, 3)
for await (let attempt of iterator) {
actualRetryCount++
}
t.is(actualRetryCount, maxAttempts)
t.is(iterator.result, 'result')
})
test('during iteration lastError is provided', async t => {
const maxAttempts = 5
let actualRetryCount = 0
const iterator = backoff.iterator((attempt) => {
if (attempt === maxAttempts) {
return 'result'
}
throw new Error('blabla')
})
for await (let attempt of iterator) {
if (attempt === 0) {
t.is(iterator.lastError, undefined)
} else {
t.is(iterator.lastError.message, 'blabla')
}
}
})
test('iteration will stop and an error is thrown after maxAttempts', async t => {
const iterator = backoff.iterator((attempt) => {
throw new Error('blabla')
}, { maxAttempts: 5 })
await t.throwsAsync(async () => {
for await (let attempt of iterator) {}
}, { instanceOf: backoff.OperationFailedError, message: 'exceeded maximum attempts, operation failed' })
})
test('an error is thrown if work argument is not a function', async t => {
await t.throwsAsync(async () => {
await backoff('asd')
}, { instanceOf: TypeError })
})
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}