-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpipeline.test.js
167 lines (162 loc) · 5.72 KB
/
pipeline.test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
'use strict';
const assert = require('assert');
const { MapConcurrent } = require('../../lib/generator/mapconcurrent');
const { Pipeline, executePipeline } = require('../../lib/generator/pipeline');
async function toArray(items) {
const result = [];
for await (const item of items) {
result.push(item);
}
return result;
}
describe('pipeline', function() {
it('empty-pipeline', async function() {
const pipeline = new Pipeline();
const result = await toArray(pipeline.execute([ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(result, [ 1, 2, 3, 4, 5 ]);
});
it('single-operation-pipeline', async function() {
const pipeline = new Pipeline({
execute: async function*(items) {
for await (const item of items) {
yield item + 1;
}
}
});
const result = await toArray(pipeline.execute([ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(result, [ 2, 3, 4, 5, 6 ]);
});
it('two-operation-pipeline', async function() {
const pipeline = new Pipeline({
execute: async function*(items) {
for await (const item of items) {
yield item + 1;
}
}
}, {
execute: async function*(items) {
for await (const item of items) {
yield item * 2;
}
}
});
const result = await toArray(pipeline.execute([ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(result, [ 4, 6, 8, 10, 12 ]);
});
it('pipeline-batch-map', async function() {
const pipeline = new Pipeline(
new MapConcurrent({
execute: async function*(values) {
const batch = [];
for await (const value of values) {
batch.push(value);
}
yield* batch.reverse();
}
}, {
maxBatchLength: 3
})
);
const result = await toArray(pipeline.execute([ 1, 2, 3, 4, 5 ]));
assert.deepStrictEqual(result, [ 3, 2, 1, 5, 4 ]);
});
it('args', async function() {
const pipeline = new Pipeline({
execute: async function*(values, arg) {
for await (const value of values) {
yield value + arg;
}
}
});
const result = await toArray(pipeline.execute([ 1, 2, 3, 4 ], 7));
assert.deepStrictEqual(result, [ 8, 9, 10, 11 ]);
});
it('executePipeline', async function() {
let sum = 0;
const pipeline = new Pipeline({
execute: async function*(values) {
for await (const value of values) {
sum += value;
yield value;
}
}
});
await executePipeline(pipeline, [1, 2, 3, 4]);
assert.strictEqual(sum, 10);
});
it('executePipeline-args', async function() {
let sum = 0;
const pipeline = new Pipeline({
execute: async function*(values, arg) {
for await (const value of values) {
sum += value + arg;
yield value;
}
}
});
await executePipeline(pipeline, [1, 2, 3, 4], 7);
assert.strictEqual(sum, 38);
});
it('filter-single-pipeline', async function() {
let sum = 0;
const pipeline = new Pipeline({
execute: async function*(values) {
for await (const value of values) {
sum += value;
yield value;
}
}
});
pipeline.setFilterFunction({
execute: async function*(values) {
for await (const value of values) {
if ((value % 2) === 0) {
yield value;
}
}
}
});
await executePipeline(pipeline, [1, 2, 3, 4, 5, 6]);
assert.strictEqual(sum, 12);
});
it('filter-two-function-pipeline', async function() {
const pipeline = new Pipeline({
execute: async function*(values) {
for await (const value of values) {
yield value + 1;
}
}
}, {
execute: async function*(values) {
for await (const value of values) {
yield value * 2;
}
}
});
pipeline.setFilterFunction({
execute: async function*(values) {
let firstValue = true;
for await (const value of values) {
if (!firstValue) {
yield value;
}
firstValue = false;
}
}
});
const result = await toArray(pipeline.execute([1, 2, 3, 4, 5, 6]));
assert.deepStrictEqual(result, [8, 10, 12, 14]); // 1 and 2 were filtered out
});
});