/
pmThrottle.test.mjs
248 lines (226 loc) · 8.8 KB
/
pmThrottle.test.mjs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import assert from 'assert'
import pmThrottle from '../src/pmThrottle.mjs'
describe(`pmThrottle`, function() {
async function test1() {
return new Promise((resolve, reject) => {
let ms = []
let pmt = pmThrottle()
let fun = function (name, t) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
//console.log('resolve: ' + name)
resolve('resolve: ' + name + ', t: ' + t)
}, t)
})
}
pmt(fun, 't1', 150)
.then(function(msg) {
//console.log('t1 then', msg)
ms.push('t1 then: ' + msg)
})
.catch(function(msg) {
//console.log('t1 catch', msg)
ms.push('t1 catch: ' + 'reason ' + msg.reason)
})
pmt(fun, 't2', 100)
.then(function(msg) {
//console.log('t2 then', msg)
ms.push('t2 then: ' + msg)
})
.catch(function(msg) {
//console.log('t2 catch', msg)
ms.push('t2 catch: ' + 'reason ' + msg.reason)
})
pmt(fun, 't3', 50)
.then(function(msg) {
//console.log('t3 then', msg)
ms.push('t3 then: ' + msg)
})
.catch(function(msg) {
//console.log('t3 catch', msg)
ms.push('t3 catch: ' + 'reason ' + msg.reason)
})
setTimeout(() => {
pmt(fun, 't4', 50)
.then((msg) => {
//console.log('t4 then', msg)
ms.push('t4 then: ' + msg)
})
.catch((msg) => {
//console.log('t4 catch', msg)
ms.push('t4 catch: ' + 'reason ' + msg.reason)
})
.finally(() => {
resolve(ms)
})
}, 200)
})
}
//console.log('test1')
// test1
// t1 catch { reason: 'cancelled' }
// t2 catch { reason: 'cancelled' }
// resolve: t3, t: 50
// t3 then resolve: t3, t: 50
// resolve: t4, t: 50
// t4 then resolve: t4, t: 50
// ["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: resolve: t3, t: 50","t4 then: resolve: t4, t: 50"]
let r1 = '["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: resolve: t3, t: 50","t4 then: resolve: t4, t: 50"]'
it(`should return '${r1}' when run test1'`, async function() {
let ms = await test1()
//console.log(JSON.stringify(ms))
assert.strict.deepStrictEqual(JSON.stringify(ms), r1)
})
async function test2() {
return new Promise((resolve, reject) => {
let ms = []
let pmt = pmThrottle()
let fun1 = function (name, t) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
//console.log('fun1 resolve: ' + name + ', t: ' + t)
resolve('fun1 resolve: ' + name + ', t: ' + t)
}, t)
})
}
let fun2 = function (name, t) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
//console.log('fun2 resolve: ' + name + ', t: ' + t)
resolve('fun2 resolve: ' + name + ', t: ' + t)
}, t)
})
}
//測試不同函數fun1與fun2
pmt(fun1, 't1', 150)
.then(function(msg) {
//console.log('t1 then', msg)
ms.push('t1 then: ' + msg)
})
.catch(function(msg) {
//console.log('t1 catch', msg)
ms.push('t1 catch: ' + 'reason ' + msg.reason)
})
pmt(fun2, 't2', 100)
.then(function(msg) {
//console.log('t2 then', msg)
ms.push('t2 then: ' + msg)
})
.catch(function(msg) {
//console.log('t2 catch', msg)
ms.push('t2 catch: ' + 'reason ' + msg.reason)
})
pmt(fun2, 't3', 50)
.then(function(msg) {
//console.log('t3 then', msg)
ms.push('t3 then: ' + msg)
})
.catch(function(msg) {
//console.log('t3 catch', msg)
ms.push('t3 catch: ' + 'reason ' + msg.reason)
})
setTimeout(() => {
pmt(fun1, 't4', 50)
.then((msg) => {
//console.log('t4 then', msg)
ms.push('t4 then: ' + msg)
})
.catch((msg) => {
//console.log('t4 catch', msg)
ms.push('t4 catch: ' + 'reason ' + msg.reason)
})
.finally(() => {
resolve(ms)
})
}, 200)
})
}
//console.log('test2')
// test2
// t1 catch { reason: 'cancelled' }
// t2 catch { reason: 'cancelled' }
// fun2 resolve: t3, t: 50
// t3 then fun2 resolve: t3, t: 50
// fun1 resolve: t4, t: 50
// t4 then fun1 resolve: t4, t: 50
// ["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: fun2 resolve: t3, t: 50","t4 then: fun1 resolve: t4, t: 50"]
let r2 = '["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: fun2 resolve: t3, t: 50","t4 then: fun1 resolve: t4, t: 50"]'
it(`should return '${r2}' when run test2'`, async function() {
let ms = await test2()
//console.log(JSON.stringify(ms))
assert.strict.deepStrictEqual(JSON.stringify(ms), r2)
})
async function test3() {
return new Promise((resolve, reject) => {
let ms = []
let pmt = pmThrottle()
let i = 0
let fun = function () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
i++
//console.log('resolve: ' + i)
resolve('resolve: ' + i)
}, 100)
})
}
//測試無輸入參數
pmt(fun)
.then(function(msg) {
//console.log('t1 then', msg)
ms.push('t1 then: ' + msg)
})
.catch(function(msg) {
//console.log('t1 catch', msg)
ms.push('t1 catch: ' + 'reason ' + msg.reason)
})
pmt(fun)
.then(function(msg) {
//console.log('t2 then', msg)
ms.push('t2 then: ' + msg)
})
.catch(function(msg) {
//console.log('t2 catch', msg)
ms.push('t2 catch: ' + 'reason ' + msg.reason)
})
pmt(fun)
.then(function(msg) {
//console.log('t3 then', msg)
ms.push('t3 then: ' + msg)
})
.catch(function(msg) {
//console.log('t3 catch', msg)
ms.push('t3 catch: ' + 'reason ' + msg.reason)
})
setTimeout(() => {
pmt(fun)
.then((msg) => {
//console.log('t4 then', msg)
ms.push('t4 then: ' + msg)
})
.catch((msg) => {
//console.log('t4 catch', msg)
ms.push('t4 catch: ' + 'reason ' + msg.reason)
})
.finally(() => {
resolve(ms)
})
}, 200)
})
}
//console.log('test3')
// test3
// t1 catch { reason: 'cancelled' }
// t2 catch { reason: 'cancelled' }
// core: resolve: 1
// t3 then resolve: 1
// core: resolve: 2
// t4 then resolve: 2
// ["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: resolve: 1","t4 then: resolve: 2"]
let r3 = '["t1 catch: reason cancelled","t2 catch: reason cancelled","t3 then: resolve: 1","t4 then: resolve: 2"]'
it(`should return '${r3}' when run test3'`, async function() {
let ms = await test3()
//console.log(JSON.stringify(ms))
assert.strict.deepStrictEqual(JSON.stringify(ms), r3)
})
})