-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
144 lines (130 loc) · 4.55 KB
/
tests.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const test = require('node:test');
const PromisePoolRL = require('./index');
test('Pool', async (t) => {
const tests = [
{
poolSize: 10,
entries: 1000
},
{
poolSize: 10,
entries: 1
},
{
poolSize: 1,
entries: 10
},
];
for (const test of tests) {
await t.test('Pool of 1 with 10 entries', async (t) => {
let count = -1;
let startedEntries = 0;
let doneEntries = 0;
const poolSize = test.poolSize;
const entries = test.entries;
function next() {
const id = ++count;
if (id === entries) return false;
if (id > entries) throw new Error('next() called too much');
return (async function p() {
startedEntries++;
// wait between 0 and 50ms
await new Promise((r) => { setTimeout(r, Math.random() * 50) });
doneEntries++;
});
}
await PromisePoolRL(next, poolSize);
if (startedEntries != entries) throw new Error(`Expected startedEntries: ${startedEntries} to be equal to expectedEntries: ${entries}`)
if (doneEntries != entries) throw new Error(`Expected doneEntries: ${doneEntries} to be equal to expectedEntries: ${entries}`)
});
}
});
test('Reject', async (t) => {
let count = -1;
let startedEntries = 0;
let doneEntries = 0;
const poolSize = 3;
const entries = 10;
function next() {
const id = ++count;
if (id === entries) return false;
if (id > entries) throw new Error('next() called too much');
return (async function p() {
startedEntries++;
// on the 5th promise throw an Error right away
if (id === 5) {
throw new Error('Failing on 6th Promise');
} else { // wait 50ms
await new Promise((r) => { setTimeout(r, 50) });
doneEntries++;
}
});
}
try {
await PromisePoolRL(next, poolSize);
} catch (e) {
if (e.message !== 'Failing on 6th Promise') throw new Error(e);
}
// doneEntries must be < 6
if (doneEntries > 5) throw new Error('Too many next() called');
// startedEntries must be === 6 (6th and 7th migth have been started)
if (startedEntries !== 6) throw new Error('next() must have been called 6 times');
});
test('Reject catching Error and continuing', async (t) => {
let count = -1;
let startedEntries = 0;
let doneEntries = 0;
const poolSize = 3;
const entries = 10;
let gotError = 0;
function onError(err, promise, intermediateResult) {
gotError++;
if (promise.id !== 5) throw new Error('Expected to receive the failing promise');
if (err.message !== 'Failing on 6th Promise') throw new Error(e);
return true;
}
function next() {
const id = ++count;
if (id === entries) return false;
if (id > entries) throw new Error('next() called too much');
const p = async function () {
startedEntries++;
// on the 6th promise throw an Error right away
if (id === 5) {
doneEntries++;
throw new Error('Failing on 6th Promise');
} else { // wait 50ms
await new Promise((r) => { setTimeout(r, 50) });
doneEntries++;
}
};
p.id = id;
return p;
}
await PromisePoolRL(next, poolSize, { onError });
if (gotError != 1) throw new Error(`Expected gotError: ${gotError} to be equal 1`)
if (startedEntries != entries) throw new Error(`Expected startedEntries: ${startedEntries} to be equal to expectedEntries: ${entries}`)
if (doneEntries != entries) throw new Error(`Expected doneEntries: ${doneEntries} to be equal to expectedEntries: ${entries}`)
});
test('Rate limiting', async (t) => {
let count = -1;
const poolSize = 5;
const entries = 50;
const desiredRateHz = 50; // 10 per seconds
const startTime = Date.now();
function next() {
const id = ++count;
if (id === entries) return false;
if (id > entries) throw new Error('next() called too much');
return (async function p() {
await new Promise((r) => { setTimeout(r, 50) });
});
}
const result = await PromisePoolRL(next, poolSize, {rateHz: desiredRateHz});
const endTime = Date.now();
const calculatedRate = count * 1000 / (endTime - startTime);
if (calculatedRate * 0.99 > result.averageRateHz || calculatedRate * 1.01 < result.averageRateHz)
throw new Error(`result.averageRateHz ${result.averageRateHz} should be approx 1% of calculatedRate ${calculatedRate}`);
if (desiredRateHz * 0.9 > result.averageRateHz || desiredRateHz * 1.1 < result.averageRateHz)
throw new Error(`result.averageRateHz ${result.averageRateHz} should be approx 10% of desiredRateHz ${desiredRateHz}`);
});