-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.ts
136 lines (103 loc) · 2.63 KB
/
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
// This file is part of cwait, copyright (c) 2015-2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.
declare var process: any;
import * as Promise from 'bluebird';
import {TaskQueue} from '..';
function equals(a: any, b: any) {
if(a != b) {
console.log('ERROR ' + a + ' != ' + b);
process.exit(1);
}
console.log('OK ' + a);
}
var testCount = 0;
function test1() {
/** Queue allowing 3 concurrent function calls. */
var queue = new TaskQueue(Promise, 3);
var running = 0;
var maxRunning = 0;
/** Test function returning a promise with a slight delay
* and tracking concurrent executions. */
function run(item: string) {
if(++running > maxRunning) maxRunning = running;
return(Promise.delay(100, true).then(() => {
--running;
return(item);
}));
}
/** List of 6 items to loop through. */
var list = '123456'.split('');
// Run test without limiting concurrency.
return(
Promise.map(list, run).then((result: string[]) => {
++testCount;
equals(result.join(''), '123456');
equals(maxRunning, 6);
maxRunning = 0;
// Run test and limit concurrency.
return(Promise.map(list, queue.wrap(run)))
}).then((result: string[]) => {
++testCount;
equals(result.join(''), '123456');
equals(maxRunning, 3);
})
);
}
function test2() {
function throws() {
if(1) throw(new Error('Beep'));
return(Promise.resolve(true));
}
var queue = new TaskQueue(Promise, 1);
return(
Promise.all([
queue.wrap(throws)().then(null as any, (err: any) => ++testCount),
queue.wrap(throws)().then(null as any, (err: any) => ++testCount)
])
);
}
function test3() {
function rejects() {
return(Promise.reject(new Error('Beep')));
}
var queue = new TaskQueue(Promise, 1);
return(
Promise.all([
queue.wrap(rejects)().then(null as any, (err: any) => ++testCount),
queue.wrap(rejects)().then(null as any, (err: any) => ++testCount)
])
);
}
function test4() {
const unsorted: number[] = [];
function compare(a: number, b: number) { return(a - b); }
for(let num = 0; num < 100; ++num) {
unsorted[num] = ~~(Math.random() * 50);
}
const correct = unsorted.slice(0).sort(compare);
const queue = new TaskQueue(Promise, 1);
const result: number[] = [];
const start = new Date().getTime();
return(
Promise.map(
unsorted,
(item: number) => queue.add(
() => {
const delta = new Date().getTime() - start - item * 10;
if(delta > 0 && delta < 20) result.push(item);
},
item * 10
)
).then(
() => result.join(' ') == correct.join(' ') && ++testCount
)
);
}
Promise.all([
test1(),
test2(),
test3(),
test4()
]).then(
() => equals(testCount, 7)
);