stream1-backpressure.js 625 B

12345678910111213141516171819202122232425262728293031
  1. var test = require('tape');
  2. var read = require('..');
  3. var co = require('co');
  4. var wait = require('co-wait');
  5. var through = require('through');
  6. test('backpressure', function(t) {
  7. var times = 3;
  8. t.plan(2 + times);
  9. co(function*() {
  10. var stream = through();
  11. process.nextTick(function() {
  12. (function next() {
  13. stream.queue('foo');
  14. if (--times) setTimeout(next, 10);
  15. else stream.end();
  16. })();
  17. });
  18. var chunk;
  19. while (chunk = yield read(stream)) {
  20. t.equal(chunk, 'foo', 'data event');
  21. yield wait(50);
  22. }
  23. t.ok(true, 'ended');
  24. }, t.error.bind(t));
  25. });