stream2-read2.js 645 B

12345678910111213141516171819202122232425262728293031
  1. var test = require('tape');
  2. var read = require('..');
  3. var co = require('co');
  4. var Readable = require('stream').Readable;
  5. test('read2', function(t) {
  6. var times = 3;
  7. t.plan(2 + (times * 2));
  8. co(function*() {
  9. var stream = Readable();
  10. stream._read = function() {
  11. setTimeout(function() {
  12. if (times-- > 0) {
  13. stream.push('foo');
  14. stream.push('bar');
  15. } else {
  16. stream.push(null);
  17. }
  18. }, 10);
  19. };
  20. var chunk;
  21. while (chunk = yield read(stream)) {
  22. t.ok(/foo|bar/.test(chunk.toString()), 'data event');
  23. }
  24. t.ok(true, 'ended');
  25. }, t.error.bind(t));
  26. });