function runTest(highWaterMark, objectMode, produce) {

  const old = new EE();
  const r = new Readable({ highWaterMark, objectMode });
  assert.strictEqual(r, r.wrap(old));

  r.on('end', common.mustCall());

  old.pause = function() {
    old.emit('pause');
    flowing = false;
  };

  old.resume = function() {
    old.emit('resume');
    flow();
  };

  let flowing;
  let chunks = 10;
  let oldEnded = false;
  const expected = [];
  function flow() {
    flowing = true;
    while (flowing && chunks-- > 0) {
      const item = produce();
      expected.push(item);
      old.emit('data', item);
    }
    if (chunks <= 0) {
      oldEnded = true;
      old.emit('end');
    }
  }

  const w = new Writable({ highWaterMark: highWaterMark * 2,
                           objectMode });
  const written = [];
  w._write = function(chunk, encoding, cb) {
    written.push(chunk);
    setTimeout(cb, 1);
  };

  w.on('finish', common.mustCall(function() {
    performAsserts();
  }));

  r.pipe(w);

  flow();

  function performAsserts() {
    assert(oldEnded);
    assert.deepStrictEqual(written, expected);
  }
}
示例#2
0
test('writables are not pipable', function(t) {
  var w = new W();
  w._write = function() {};
  var gotError = false;
  w.on('error', function(er) {
    gotError = true;
  });
  w.pipe(process.stdout);
  assert(gotError);
  t.end();
});
示例#3
0
test('finish is emitted if last chunk is empty', function(t) {
  var w = new W();
  w._write = function(chunk, e, cb) {
    process.nextTick(cb);
  };
  w.on('finish', function() {
    t.end();
  });
  w.write(Buffer.allocUnsafe(1));
  w.end(Buffer.alloc(0));
});
示例#4
0
test('writables are not pipable', function(t) {
  const w = new W();
  w._write = common.noop;
  let gotError = false;
  w.on('error', function() {
    gotError = true;
  });
  w.pipe(process.stdout);
  assert(gotError);
  t.end();
});
示例#5
0
test('finish does not come before sync _write cb', function(t) {
  var w = new W();
  var writeCb = false;
  w._write = function(chunk, e, cb) {
    cb();
  };
  w.on('finish', function() {
    assert(writeCb);
    t.end();
  });
  w.write(Buffer(0), function(er) {
    writeCb = true;
  });
  w.end();
});
示例#6
0
test('end(chunk) two times is an error', function(t) {
  var w = new W();
  w._write = function() {};
  var gotError = false;
  w.on('error', function(er) {
    gotError = true;
    t.equal(er.message, 'write after end');
  });
  w.end('this is the end');
  w.end('and so is this');
  process.nextTick(function() {
    assert(gotError);
    t.end();
  });
});
示例#7
0
test('can write objects to stream', function(t) {
  var w = new Writable({ objectMode: true });

  w._write = function(chunk, encoding, cb) {
    assert.deepEqual(chunk, { foo: 'bar' });
    cb();
  };

  w.on('finish', function() {
    t.end();
  });

  w.write({ foo: 'bar' });
  w.end();
});
示例#8
0
test('finish does not come before write cb', function(t) {
  var w = new W();
  var writeCb = false;
  w._write = function(chunk, e, cb) {
    setTimeout(function() {
      writeCb = true;
      cb();
    }, 10);
  };
  w.on('finish', function() {
    assert(writeCb);
    t.end();
  });
  w.write(Buffer(0));
  w.end();
});
示例#9
0
test('dont end while writing', function(t) {
  var w = new W();
  var wrote = false;
  w._write = function(chunk, e, cb) {
    assert(!this.writing);
    wrote = true;
    this.writing = true;
    setTimeout(function() {
      this.writing = false;
      cb();
    });
  };
  w.on('finish', function() {
    assert(wrote);
    t.end();
  });
  w.write(Buffer(0));
  w.end();
});
示例#10
0
test('can write multiple objects to stream', function(t) {
  var w = new Writable({ objectMode: true });
  var list = [];

  w._write = function(chunk, encoding, cb) {
    list.push(chunk);
    cb();
  };

  w.on('finish', function() {
    assert.deepEqual(list, [0, 1, 2, 3, 4]);

    t.end();
  });

  w.write(0);
  w.write(1);
  w.write(2);
  w.write(3);
  w.write(4);
  w.end();
});
示例#11
0
test('buffers finish until cb is called', function(t) {
  var w = new Writable({
    objectMode: true
  });
  var called = false;

  w._write = function(chunk, encoding, cb) {
    assert.equal(chunk, 'foo');

    process.nextTick(function() {
      called = true;
      cb();
    });
  };

  w.on('finish', function() {
    assert.equal(called, true);

    t.end();
  });

  w.write('foo');
  w.end();
});
示例#12
0
test('can write strings as objects', function(t) {
  var w = new Writable({
    objectMode: true
  });
  var list = [];

  w._write = function(chunk, encoding, cb) {
    list.push(chunk);
    process.nextTick(cb);
  };

  w.on('finish', function() {
    assert.deepEqual(list, ['0', '1', '2', '3', '4']);

    t.end();
  });

  w.write('0');
  w.write('1');
  w.write('2');
  w.write('3');
  w.write('4');
  w.end();
});
示例#13
0
  const tw = new W();
  const hex = '018b5e9a8f6236ffe30e31baf80d2cf6eb';
  tw._write = common.mustCall(function(chunk) {
    assert.strictEqual(chunk.toString('hex'), hex);
  });
  const buf = Buffer.from(hex, 'hex');
  tw.write(buf, 'latin1');
}

{
  // Verify writables cannot be piped
  const w = new W();
  w._write = common.mustNotCall();
  let gotError = false;
  w.on('error', function() {
    gotError = true;
  });
  w.pipe(process.stdout);
  assert.strictEqual(gotError, true);
}

{
  // Verify that duplex streams cannot be piped
  const d = new D();
  d._read = common.mustCall();
  d._write = common.mustNotCall();
  let gotError = false;
  d.on('error', function() {
    gotError = true;
  });
  d.pipe(process.stdout);
示例#14
0
  for (let i = 0; i < 6; i++) {
    const bool = r.push(i);
    assert.strictEqual(bool, i !== 5);
  }
}

{
  // Verify that objects can be written to stream
  const w = new Writable({ objectMode: true });

  w._write = function(chunk, encoding, cb) {
    assert.deepStrictEqual(chunk, { foo: 'bar' });
    cb();
  };

  w.on('finish', common.mustCall());
  w.write({ foo: 'bar' });
  w.end();
}

{
  // Verify that multiple objects can be written to stream
  const w = new Writable({ objectMode: true });
  const list = [];

  w._write = function(chunk, encoding, cb) {
    list.push(chunk);
    cb();
  };

  w.on('finish', common.mustCall(function() {
function runTest(highWaterMark, objectMode, produce) {
  testRuns++;

  var old = new EE();
  var r = new Readable({ highWaterMark: highWaterMark,
                         objectMode: objectMode });
  assert.equal(r, r.wrap(old));

  var ended = false;
  r.on('end', function() {
    ended = true;
  });

  old.pause = function() {
    console.error('old.pause()');
    old.emit('pause');
    flowing = false;
  };

  old.resume = function() {
    console.error('old.resume()');
    old.emit('resume');
    flow();
  };

  var flowing;
  var chunks = 10;
  var oldEnded = false;
  var expected = [];
  function flow() {
    flowing = true;
    while (flowing && chunks-- > 0) {
      var item = produce();
      expected.push(item);
      console.log('old.emit', chunks, flowing);
      old.emit('data', item);
      console.log('after emit', chunks, flowing);
    }
    if (chunks <= 0) {
      oldEnded = true;
      console.log('old end', chunks, flowing);
      old.emit('end');
    }
  }

  var w = new Writable({ highWaterMark: highWaterMark * 2,
                         objectMode: objectMode });
  var written = [];
  w._write = function(chunk, encoding, cb) {
    console.log('_write', chunk);
    written.push(chunk);
    setTimeout(cb);
  };

  w.on('finish', function() {
    completedRuns++;
    performAsserts();
  });

  r.pipe(w);

  flow();

  function performAsserts() {
    assert(ended);
    assert(oldEnded);
    assert.deepEqual(written, expected);
  }
}
示例#16
0
  if (chunks <= 0) {
    oldEnded = true;
    old.emit('end');
  }
}

var w = new Writable({ highWaterMark: 20 });
var written = [];
w._write = function(chunk, encoding, cb) {
  written.push(chunk.toString());
  setTimeout(cb);
};

var finished = false;
w.on('finish', function() {
  finished = true;
});


var expect = new Array(11).join('xxxxxxxxxx');

r.pipe(w);

flow();

process.on('exit', function() {
  assert.equal(pauses, 10);
  assert.equal(resumes, 9);
  assert(ended);
  assert(finished);
  assert(oldEnded);