This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
120 lines (99 loc) · 2.12 KB
/
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
'use strict';
var Generator = require('audio-generator/stream');
var Sink = require('./direct');
var PullSink = require('./pull');
var StreamSink = require('./stream');
var Transform = require('stream').Transform;
var test = require('tst');
var assert = require('assert');
var pull = require('pull-stream');
test('Direct', (done) => {
let count = 0;
let sink = Sink((data, cb) => {
setTimeout(() => {
count++;
cb(null, count);
}, 10)
});
assert.equal(count, 0);
sink(1, (err, data) => {
assert.equal(err, null);
assert.equal(data, 1);
});
assert.equal(count, 0);
setTimeout(() => {
assert.equal(count, 1);
done();
}, 20);
});
test('Pull-stream', (done) => {
let count = 0;
let sink = PullSink((data, cb) => {
setTimeout(() => {
count++;
if (count >= 10) sink.abort();
cb();
}, 10);
});
pull(
pull.infinite(),
pull.take(10),
sink
);
setTimeout(() => {
assert.equal(count, 10);
done();
}, 200);
});
test('Pull-stream readme case', done => {
const pull = require('pull-stream/pull');
const sink = require('./pull');
const generator = require('audio-generator/pull');
let count = 0;
let s = sink((data, cb) => {
if (count++ >= 4) {
cb(true);
done();
// s.abort();
return;
}
setTimeout(cb, 100);
});
//stream generated data to sink with pressure control
pull(
generator(time => Math.sin(time * Math.PI * 2 * 440)),
s
);
});
test('Stream', (done) => {
let count = 0;
Generator(function (time) {
return time ? 0 : 1;
})
.pipe(StreamSink(function (data) {
console.log('Passed through chunk of data ', data.length);
count++;
if (count >= 3) this.end();
// setTimeout(cb, 1000);
}))
.on('end', () => {
assert.equal(count, 3);
})
.pipe(Transform({
objectMode: true,
transform: function (data, enc, cb) {
cb(null, data)
}
}))
.pipe(StreamSink(function (data, cb) {
setTimeout(cb, 200);
console.log('Got chunk of data ', data.length);
}));
setTimeout(() => {
assert.equal(count, 3);
done();
}, 600);
});
//TODO: move to audio-slice test
// var Slice = require('../slice');
// Generator().pipe(Slice(2)).pipe(Sink(v => console.log(v)));