-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestTools.js
More file actions
70 lines (65 loc) · 2.05 KB
/
testTools.js
File metadata and controls
70 lines (65 loc) · 2.05 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
"use strict"
const Stream = require('stream')
const Readable = Stream.Readable
const Writable = Stream.Writable
const time = require('time-since')
const start = new Date();
module.exports = {
createArithmaticStream: function (char, field, step, last) {
var i = 0,
stream = new Readable({ objectMode: true, highWaterMark: 2 }),
p
stream._read = function () {
if (i == last + step) {
this.push(null)
i++
}
else do {
var j = { key: i };
j[field] = char + i;
p = this.push(j)
i += step
}
while (i <= last && p)
}
return stream
},
createRepeatingKeyStream: function (keysAndMultiplicity, char, field) {
var ind = 0,
keys = Object.keys(keysAndMultiplicity),
stream = new Readable({ objectMode: true }),
done = false;
stream._read = function () {
do {
var obj = { key: keys[0] };
obj[field] = char + ind;
ind++;
var p = this.push(obj)
if (!--keysAndMultiplicity[keys[0]]) // decrement the multiplicity counter and check if zero
keys.shift() //remove keys[0] from keys
} while (p && keys.length)
if (!keys.length & !done) {
done = true;
this.push(null)
}
}
return stream;
},
slowWriter: function () {
var ret = new Writable({ objectMode: true, highWaterMark: 2 })
ret._write = function (data, _, callback) {
setTimeout(callback, 75)
}
return ret
},
trivialTransform: function () {
var ret = new Stream.Transform({ objectMode: true, highWaterMark: 2 })
ret._transform = function (data, _, callback) {
var p = this.push(data);
if (!p)
console.log('buffered')
callback();
}
return ret
},
}