spec.asynct.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. assert that data is called many times
  3. assert that end is called eventually
  4. assert that when stream enters pause state,
  5. on drain is emitted eventually.
  6. */
  7. var es = require('..')
  8. var it = require('it-is').style('colour')
  9. var spec = require('stream-spec')
  10. exports['simple stream'] = function (test) {
  11. var stream = es.through()
  12. var x = spec(stream).basic().pausable()
  13. stream.write(1)
  14. stream.write(1)
  15. stream.pause()
  16. stream.write(1)
  17. stream.resume()
  18. stream.write(1)
  19. stream.end(2) //this will call write()
  20. process.nextTick(function (){
  21. x.validate()
  22. test.done()
  23. })
  24. }
  25. exports['throw on write when !writable'] = function (test) {
  26. var stream = es.through()
  27. var x = spec(stream).basic().pausable()
  28. stream.write(1)
  29. stream.write(1)
  30. stream.end(2) //this will call write()
  31. stream.write(1) //this will be throwing..., but the spec will catch it.
  32. process.nextTick(function () {
  33. x.validate()
  34. test.done()
  35. })
  36. }
  37. exports['end fast'] = function (test) {
  38. var stream = es.through()
  39. var x = spec(stream).basic().pausable()
  40. stream.end() //this will call write()
  41. process.nextTick(function () {
  42. x.validate()
  43. test.done()
  44. })
  45. }
  46. /*
  47. okay, that was easy enough, whats next?
  48. say, after you call paused(), write should return false
  49. until resume is called.
  50. simple way to implement this:
  51. write must return !paused
  52. after pause() paused = true
  53. after resume() paused = false
  54. on resume, if !paused drain is emitted again.
  55. after drain, !paused
  56. there are lots of subtle ordering bugs in streams.
  57. example, set !paused before emitting drain.
  58. the stream api is stateful.
  59. */
  60. require('./helper')(module)