Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
90d24a26
Unverified
Commit
90d24a26
authored
Jul 11, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
retry tests
parent
7a3a5bd5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
1 deletion
+48
-1
retry.test.ts
src/utils/retry.test.ts
+47
-0
retry.ts
src/utils/retry.ts
+1
-1
No files found.
src/utils/retry.test.ts
0 → 100644
View file @
90d24a26
import
{
retry
}
from
'
./retry
'
describe
(
'
retry
'
,
()
=>
{
function
makeFn
<
T
>
(
fails
:
number
,
result
:
T
):
()
=>
Promise
<
T
>
{
return
async
()
=>
{
if
(
fails
>
0
)
{
fails
--
throw
new
Error
(
'
failure
'
)
}
return
result
}
}
it
(
'
works after one fail
'
,
async
()
=>
{
await
expect
(
retry
(
makeFn
(
1
,
'
abc
'
),
{
n
:
3
,
maxWait
:
0
,
minWait
:
0
})).
resolves
.
toEqual
(
'
abc
'
)
})
it
(
'
works after two fails
'
,
async
()
=>
{
await
expect
(
retry
(
makeFn
(
2
,
'
abc
'
),
{
n
:
3
,
maxWait
:
0
,
minWait
:
0
})).
resolves
.
toEqual
(
'
abc
'
)
})
it
(
'
throws if too many fails
'
,
async
()
=>
{
await
expect
(
retry
(
makeFn
(
4
,
'
abc
'
),
{
n
:
3
,
maxWait
:
0
,
minWait
:
0
})).
rejects
.
toThrow
(
'
failure
'
)
})
async
function
checkTime
(
fn
:
()
=>
Promise
<
any
>
,
min
:
number
,
max
:
number
)
{
const
time
=
new
Date
().
getTime
()
await
fn
()
const
diff
=
new
Date
().
getTime
()
-
time
expect
(
diff
).
toBeGreaterThanOrEqual
(
min
)
expect
(
diff
).
toBeLessThanOrEqual
(
max
)
}
it
(
'
waits random amount of time between min and max
'
,
async
()
=>
{
const
promises
=
[]
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
promises
.
push
(
checkTime
(
()
=>
expect
(
retry
(
makeFn
(
4
,
'
abc
'
),
{
n
:
3
,
maxWait
:
100
,
minWait
:
50
})).
rejects
.
toThrow
(
'
failure
'
),
150
,
305
)
)
}
await
Promise
.
all
(
promises
)
})
})
src/utils/retry.ts
View file @
90d24a26
...
...
@@ -3,7 +3,7 @@ function wait(ms: number): Promise<void> {
}
function
waitRandom
(
min
:
number
,
max
:
number
):
Promise
<
void
>
{
return
wait
(
min
+
Math
.
round
(
Math
.
random
()
*
(
max
-
min
)))
return
wait
(
min
+
Math
.
round
(
Math
.
random
()
*
Math
.
max
(
0
,
max
-
min
)))
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment