test-node.js 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var pump = require('./index')
  2. var rs = require('fs').createReadStream('/dev/random')
  3. var ws = require('fs').createWriteStream('/dev/null')
  4. var toHex = function () {
  5. var reverse = new (require('stream').Transform)()
  6. reverse._transform = function (chunk, enc, callback) {
  7. reverse.push(chunk.toString('hex'))
  8. callback()
  9. }
  10. return reverse
  11. }
  12. var wsClosed = false
  13. var rsClosed = false
  14. var callbackCalled = false
  15. var check = function () {
  16. if (wsClosed && rsClosed && callbackCalled) {
  17. console.log('test-node.js passes')
  18. clearTimeout(timeout)
  19. }
  20. }
  21. ws.on('close', function () {
  22. wsClosed = true
  23. check()
  24. })
  25. rs.on('close', function () {
  26. rsClosed = true
  27. check()
  28. })
  29. var res = pump(rs, toHex(), toHex(), toHex(), ws, function () {
  30. callbackCalled = true
  31. check()
  32. })
  33. if (res !== ws) {
  34. throw new Error('should return last stream')
  35. }
  36. setTimeout(function () {
  37. rs.destroy()
  38. }, 1000)
  39. var timeout = setTimeout(function () {
  40. throw new Error('timeout')
  41. }, 5000)