test-browser.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var stream = require('stream')
  2. var pump = require('./index')
  3. var rs = new stream.Readable()
  4. var ws = new stream.Writable()
  5. rs._read = function (size) {
  6. this.push(Buffer(size).fill('abc'))
  7. }
  8. ws._write = function (chunk, encoding, cb) {
  9. setTimeout(function () {
  10. cb()
  11. }, 100)
  12. }
  13. var toHex = function () {
  14. var reverse = new (require('stream').Transform)()
  15. reverse._transform = function (chunk, enc, callback) {
  16. reverse.push(chunk.toString('hex'))
  17. callback()
  18. }
  19. return reverse
  20. }
  21. var wsClosed = false
  22. var rsClosed = false
  23. var callbackCalled = false
  24. var check = function () {
  25. if (wsClosed && rsClosed && callbackCalled) {
  26. console.log('test-browser.js passes')
  27. clearTimeout(timeout)
  28. }
  29. }
  30. ws.on('finish', function () {
  31. wsClosed = true
  32. check()
  33. })
  34. rs.on('end', function () {
  35. rsClosed = true
  36. check()
  37. })
  38. pump(rs, toHex(), toHex(), toHex(), ws, function () {
  39. callbackCalled = true
  40. check()
  41. })
  42. setTimeout(function () {
  43. rs.push(null)
  44. rs.emit('close')
  45. }, 1000)
  46. var timeout = setTimeout(function () {
  47. check()
  48. throw new Error('timeout')
  49. }, 5000)