stream-spec
executable specification for Stream (to make testing streams easy)
Last updated 12 years ago .
MIT · Repository · Original npm · Tarball · package.json
$ npm install stream-spec 
SYNC missed versions from official npm registry.

StreamSpec

Automatic checking of the Stream implementations. stream-spec instruments your stream to verify that it has correct behaviour. All you need to do to test a stream is to wrap it with stream-spec, and then pipe some test data through it. it's purpose it to make it easy to test user-land streams have correct behavour.

correct stream behaviour illustrated

correct stream behaviour explained

stream api design style

a simple test

using stream-tester

var spec = require('stream-spec')
var tester = require('stream-tester')

spec(stream)
  .through({strict: true})
  .validateOnExit()

tester.createRandomStream(function () {
    return 'line ' + Math.random() + '\n'
  }, 1000)
  .pipe(stream)
  .pipe(tester.createPauseStream())

send 1000 random lines through the stream and check that it buffers on pause.

types of Stream

Writable (but not readable)

a WritableStream must implement write, end, destroy and emit 'drain' if it pauses, and 'close' after the stream ends, or is destroyed.

If the stream is sync (does no io) it probably does not need to pause, so the write() should never equal false

spec(stream)
  .writable()
  .drainable()
  .validateOnExit()

Readable (but not writable)

a ReadableStream must emit 'data', 'end', and implement destroy, and 'close' after the stream ends, or is destroyed. is strongly recommended to implement pause and resume

If the option {strict: true} is passed, it means the stream is not allowed to emit 'data' or 'end' when the stream is paused.

If the option {end: false} is passed, then end may not be emitted.

spec(stream)
  .readable()
  .pausable({strict: true})) //strict is optional.
  .validateOnExit()

Through (sync writable and readable, aka: 'filter')

A Stream that is both readable and writable, and where the input is processed and then emitted as output, more or less directly. Example, zlib. contrast this with duplex stream.

when you call pause() on a ThroughStream, it should change it into a paused state on the writable side also, and write()===false. Calling resume() should cause 'drain' to be emitted eventually.

If the option {strict: true} is passed, it means the stream is not allowed to emit 'data' or 'end' when the stream is paused.

spec(stream)
  .through({strict: true}) //strict is optional. 
  .validateOnExit()

Duplex

A Stream that is both readable and writable, but the streams go off to some other place or thing, and are not coupled directly. The readable and writable side of a DuplexStream each have their own pause state.

If the option {strict: true} is passed, it means the stream is not allowed to emit 'data' or 'end' when the stream is paused.

spec(stream)
  .duplex({strict: true})
  .validateOnExit()

other options

spec(stream, name) //use name as the name of the stream in error messages.

spec(stream, {
  name: name,   //same as above.
  strict: true, //'data' nor 'end' may be emitted when paused.
  error: true,  //this stream *must* error.
  error: false, //this stream *must not* error.
                //if neither error: boolean option is passed, the stream *may* error.
  })



Current Tags

  • 0.3.6                                ...           latest (9 years ago)

13 Versions

  • 0.3.6                                ...           9 years ago
  • 0.3.5                                ...           12 years ago
  • 0.3.4                                ...           12 years ago
  • 0.3.3                                ...           12 years ago
  • 0.3.2                                ...           12 years ago
  • 0.3.1                                ...           12 years ago
  • 0.3.0                                ...           12 years ago
  • 0.2.1                                ...           12 years ago
  • 0.2.0                                ...           12 years ago
  • 0.1.2                                ...           12 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
  • 0.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 13
Today 0
This Week 0
This Month 13
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE