-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparallel.test.ts
More file actions
173 lines (146 loc) · 3.62 KB
/
parallel.test.ts
File metadata and controls
173 lines (146 loc) · 3.62 KB
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
import { describe, it } from "bdd";
import { expect } from "expect";
import {
each,
Err,
Ok,
run,
sleep,
spawn,
until,
} from "npm:effection@4.0.0-alpha.8";
import { parallel } from "./parallel.ts";
import type { Operation, Result } from "npm:effection@4.0.0-alpha.8";
const test = describe("parallel()");
interface Defer<T> {
promise: Promise<T>;
resolve: (t: T) => void;
reject: (t: Error) => void;
}
function defer<T>(): Defer<T> {
let resolve: (t: T) => void = () => {};
let reject: (t: Error) => void = () => {};
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
}
it(
test,
"should return an immediate channel with results as they are completed",
async () => {
const result = await run(function* () {
const results = yield* parallel([
function* () {
yield* sleep(20);
return "second";
},
function* () {
yield* sleep(10);
return "first";
},
]);
const res: Result<string>[] = [];
for (const val of yield* each(results.immediate)) {
res.push(val);
yield* each.next();
}
yield* results;
return res;
});
expect(result).toEqual([Ok("first"), Ok("second")]);
},
);
it(
test,
"should return a sequence channel with results preserving array order as results",
async () => {
const result = await run(function* () {
const results = yield* parallel([
function* () {
yield* sleep(20);
return "second";
},
function* () {
yield* sleep(10);
return "first";
},
]);
const res: Result<string>[] = [];
for (const val of yield* each(results.sequence)) {
res.push(val);
yield* each.next();
}
yield* results;
return res;
});
expect(result).toEqual([Ok("second"), Ok("first")]);
},
);
it(
test,
"should return all the result in an array, preserving order",
async () => {
const result = await run(function* () {
const para = yield* parallel([
function* () {
yield* sleep(20);
return "second";
},
function* () {
yield* sleep(10);
return "first";
},
]);
return yield* para;
});
expect(result).toEqual([Ok("second"), Ok("first")]);
},
);
it(test, "should return empty array", async () => {
let actual;
await run(function* (): Operation<void> {
const results = yield* parallel([]);
actual = yield* results;
});
expect(actual).toEqual([]);
});
it(test, "should resolve all async items", async () => {
const two = defer();
function* one() {
yield* sleep(5);
return 1;
}
const result = await run(function* () {
yield* spawn(function* () {
yield* sleep(15);
two.resolve(2);
});
const results = yield* parallel([one, () => until(two.promise)]);
return yield* results;
});
expect(result).toEqual([Ok(1), Ok(2)]);
});
it(test, "should stop all operations when there is an error", async () => {
let actual: Result<number>[] = [];
const one = defer<number>();
const two = defer<number>();
function* genFn() {
try {
const results = yield* parallel([
() => until(one.promise),
() => until(two.promise),
]);
actual = yield* results;
} catch (_) {
actual = [Err(new Error("should not get hit"))];
}
}
const err = new Error("error");
one.reject(err);
two.resolve(1);
await run(genFn);
const expected = [Err(err), Ok(1)];
expect(actual).toEqual(expected);
});