-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
161 lines (153 loc) · 4.19 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
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
import {
assertEquals,
assertThrows,
} from 'https://deno.land/std@0.69.0/testing/asserts.ts';
import { spy, Spy } from 'https://deno.land/x/mock@v0.6.1/spy.ts';
import { queue } from './mod.ts';
Deno.test('drain as callback', async function (): Promise<void> {
let result = 0;
const task: Spy<void> = spy((a: number) => (result += a));
const q = queue(task, 2);
return new Promise((resolve) => {
q.drain(() => {
assertEquals(result, 6);
assertEquals(task.calls.length, 3);
assertEquals(task.calls[2].args[0], 3);
resolve();
});
q.push(1);
q.push([2, 3]);
});
});
Deno.test('async drain', async function (): Promise<void> {
let result = 0;
const task: Spy<void> = spy((a: number) => (result += a));
const q = queue(task, 2);
q.push(1);
q.push([2, 3]);
await q.drain();
assertEquals(result, 6);
assertEquals(task.calls.length, 3);
q.push(1);
await q.drain();
assertEquals(result, 7);
assertEquals(task.calls.length, 4);
assertEquals(task.calls[3].args[0], 1);
});
Deno.test('multiple drains as callback', async function (): Promise<any> {
let result = 0;
const task: Spy<void> = spy((a: number) => (result += a));
const q = queue(task, 2);
return Promise.all([
new Promise((resolve) => {
q.drain(() => {
assertEquals(result, 6);
resolve();
});
}),
new Promise((resolve) => {
q.drain(() => {
assertEquals(result, 6);
resolve();
});
q.push(1);
q.push([2, 3]);
}),
]);
});
Deno.test('onDone works', async function (): Promise<any> {
let result = 0;
const task: Spy<void> = spy((a: number) => (result += a));
const done: Spy<void> = spy(() => {});
const q = queue(task, 2);
q.onDone(done);
q.push(1);
q.push([2, 3]);
await q.drain();
assertEquals(done.calls.length, 3);
assertEquals(done.calls[0].args.slice(0, 2), [1, 1]);
assertEquals(done.calls[1].args.slice(0, 2), [2, 3]);
assertEquals(done.calls[2].args.slice(0, 2), [3, 6]);
});
Deno.test('onError works', async function (): Promise<any> {
const task: Spy<void> = spy((a: number) => {
throw new Error('ow no!');
});
const error: Spy<void> = spy(() => {
// console.log('error!!11');
});
const q = queue(task, 2);
q.onError(error);
q.push(1);
q.push([2, 3]);
await q.drain();
assertEquals(error.calls.length, 3);
assertEquals(error.calls[0].args[0], 1);
assertEquals(error.calls[1].args[0], 2);
assertEquals(error.calls[2].args[0], 3);
});
Deno.test('throws with wrong concurrency', async function (): Promise<any> {
assertThrows(() => {
queue(() => {}, -2);
});
assertThrows(() => {
queue(() => {}, 0);
});
assertThrows(() => {
queue(() => {}, -99999999999999999999999999);
});
assertThrows(() => {
queue(() => {}, 2.5);
});
});
Deno.test(
'works with high amount of tasks and low concurrency',
async function (): Promise<any> {
let result = 0;
const N = 50_000;
const task: Spy<void> = spy((a: number, taskId) => {
// console.log(taskId, "started");
return (result += a);
});
const q = queue(task, 1);
// q.onDone((task, result, taskIndex) => {
// console.log(`task ${task}(${taskIndex}) ended with result ${result}`);
// });
const jobs = new Array(N + 1)
.join(',')
.split('')
.map((_) => 1);
q.push(jobs);
await q.drain();
assertEquals(result, N);
assertEquals(task.calls.length, N);
}
);
Deno.test(
'works with high amount of async tasks and high concurrency',
async function (): Promise<any> {
let result = 0;
const N = 50_000;
const task: Spy<void> = spy((a: number, taskId) => {
return new Promise((resolve) => {
setTimeout(() => {
// console.log(taskId, "started");
result += a;
resolve(result);
}, 5);
});
});
const q = queue(task, 1000);
// q.onDone((task, result, taskIndex) => {
// console.log(`task ${task}(${taskIndex}) ended with result ${result}`);
// });
const jobs = new Array(N + 1)
.join(',')
.split('')
.map((_) => 1);
q.push(jobs);
await q.drain();
assertEquals(result, N);
assertEquals(task.calls.length, N);
}
);