Blame view

node_modules/mux-demux/test/consistent.js 1.06 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  var es = require('event-stream')
  //var a  = require('assertions')
  
  module.exports =
  function consistent(a) {
    //test = test || a.deepEqual
    var stream = es.through()
    var chunks = 0
    stream.on('data', function () {
      chunks ++
    })
    stream.createSlave = function () {
      var expected = [], count = 0, ended = false
      stream.on('data', function (data) {
        expected.push(data)
      })
      var slave = es.through()
      slave.on('data', function (data) {
        a.ok(expected.length > 0, 'slave stream did not expect write')
        a.equal(ended, false, 'slave expected stream not to have ended') 
        var next = expected.shift()
        count ++
        a.deepEqual(next, data)
      })
      //it's okay to pass data to end(data)
      //but never emit('end', data)
      slave.on('end', function () {
        ended = true
        a.equal(expected.length, 0, 'slave stream expected 0 more writes')
      })
      slave.validate = function (message) {
        a.equal(count, chunks, 'slave must recieve same number of chunks as master')
        a.ok(count)
      }
      return slave
    }
    return stream
  }