File: two-stream.js

package info (click to toggle)
node-block-stream 0.0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 132 kB
  • sloc: makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download | duplicates (3)
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
var t = require('tap'),
    BlockStream = require("../block-stream.js"),
    isize = 0, tsize = 0, fsize = 0, psize = 0, i = 0,
    filter = null, paper = null, stack = null,

// a source data buffer
tsize = 1 * 1024; // <- 1K
stack = new Buffer( tsize );
for ( ; i < tsize; i++) stack[i] = "x".charCodeAt(0);

isize = 1 * 1024; // <- initial packet size with 4K no bug!
fsize = 2 * 1024 ; // <- first block-stream size 
psize = Math.ceil( isize / 6 ); // <- second block-stream size

fexpected = Math.ceil( tsize / fsize ); // <- packets expected for first 
pexpected = Math.ceil( tsize / psize ); // <- packets expected for second


filter = new BlockStream( fsize, { nopad : true } );
paper = new BlockStream( psize, { nopad : true } );


var fcounter = 0;
filter.on( 'data', function (c) {
  // verify that they're not null-padded
  for (var i = 0; i < c.length; i ++) {
    t.strictEqual(c[i], "x".charCodeAt(0))
  }
    ++fcounter;
} );

var pcounter = 0;
paper.on( 'data', function (c) {
  // verify that they're not null-padded
  for (var i = 0; i < c.length; i ++) {
    t.strictEqual(c[i], "x".charCodeAt(0))
  }
    ++pcounter;
} );

filter.pipe( paper );

filter.on( 'end', function () {
    t.strictEqual( fcounter, fexpected );
} );

paper.on( 'end', function () {
    t.strictEqual( pcounter, pexpected );
} );


for ( i = 0, j = isize; j <= tsize; j += isize ) {
    filter.write( stack.slice( j - isize, j ) );
}

filter.end();