-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.test.ts
153 lines (139 loc) · 4.2 KB
/
index.test.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as assert from 'assert';
import test from 'ava';
import * as sinon from 'sinon';
import RetryOp from './index';
function promiseOperation(a: any) {
return new Promise((resolve: any, reject: any) => {
resolve({a: a});
});
}
function sleep(ms: number, fail?: boolean) {
return new Promise((resolve: any, reject: any) => {
if (!fail) {
setTimeout(() => {
resolve(ms);
}, ms);
} else {
setTimeout(() => {
reject(ms);
}, ms);
}
});
}
async function asyncOperation(ms: number, fail?: boolean) {
await sleep(ms, fail);
return ms;
}
test('build operation with promise function', async t => {
let myPromiseOp = RetryOp.buildOperation(promiseOperation);
t.is('function', typeof myPromiseOp);
let closureF = myPromiseOp(123456);
t.is('function', typeof closureF);
let r = await closureF();
t.deepEqual(r, {a: 123456});
t.pass();
});
test('build operation with async function', async t => {
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
t.is('function', typeof myAsyncOp);
let closureF = myAsyncOp(1000);
t.is('function', typeof closureF);
let r = await closureF();
t.deepEqual(1000, r);
t.pass();
});
test('resolve events', async t => {
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let spy1 = sinon.spy();
let spy2 = sinon.spy();
let spy3 = sinon.spy();
let spy5 = sinon.spy();
let myRetry = new RetryOp(myAsyncOp(500), (retryOptions) => {
return;
});
myRetry.on('op_resolve', spy1);
myRetry.on('op_reject', spy2);
myRetry.on('retry', spy3);
myRetry.on('finish', spy5);
await sleep(2000);
assert(spy1.calledOnce);
assert(spy2.notCalled);
assert(spy3.notCalled);
assert(spy5.calledOnce);
t.pass();
});
test('reject events', async t => {
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let spy1 = sinon.spy();
let spy2 = sinon.spy();
let spy3 = sinon.spy();
let spy5 = sinon.spy();
let myRetry = new RetryOp(myAsyncOp(500, true), (retryOptions) => {
return;
});
myRetry.on('op_resolve', spy1);
myRetry.on('op_reject', spy2);
myRetry.on('retry', spy3);
myRetry.on('finish', spy5);
await sleep(2000);
assert(spy1.notCalled);
assert(spy2.calledOnce);
assert(spy3.notCalled);
assert(spy5.calledOnce);
t.pass();
});
test('resolve returns', async t => {
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let myRetry = new RetryOp(myAsyncOp(500), (retryOptions) => {
t.is(500, retryOptions.returns.data);
t.is(null, retryOptions.returns.error);
return;
});
await sleep(2000);
});
test('reject returns', async t => {
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let myRetry = new RetryOp(myAsyncOp(500, true), (retryOptions) => {
t.is(500, retryOptions.returns.error);
t.is(null, retryOptions.returns.data);
return;
});
await sleep(2000);
});
test('retryOptions limit by count', async t => {
let spy1 = sinon.spy();
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let myRetry = new RetryOp(myAsyncOp(500), (retryOptions) => {
if (retryOptions.totalRetryCount < 3) {
return 200;
}
t.is(3, retryOptions.totalRetryCount);
return;
});
myRetry.on('retry', spy1);
await sleep(3000);
assert(spy1.calledThrice);
});
test('retryOptions limit by time', async t => {
let spy1 = sinon.spy();
let myAsyncOp = RetryOp.buildOperation(asyncOperation);
let myRetry = new RetryOp(myAsyncOp(500), (retryOptions) => {
let now = Date.now();
if (now - retryOptions.startTime < 2700) {
return 500;
}
t.is(3, retryOptions.totalRetryCount);
return;
});
myRetry.on('retry', spy1);
await sleep(4500);
assert(spy1.calledThrice);
});
test('op should be function', async t => {
const error = t.throws(() => {
let myRetry = new RetryOp(('not function') as any, (retryOptions) => {
return;
});
}, Error);
t.is(error.message, 'Operation must be a function');
});