Ejemplo n.º 1
0
 .on('data', function (data) {
   if(++ count < 12) return
   if(_client.connected) {
     _client.disconnect()
     console.log('DISCONNECT')
   }
   console.log('DATA', data, count)
 })
Ejemplo n.º 2
0
;(function disconnect () {

  var _client = new RemoteEventEmitter()
  var _server = new RemoteEventEmitter()
  _client.getStream().pipe(_server.getStream()).pipe(_client.getStream())
 
  _client.on('disconnect', function () {
    console.log('CLIENT DISCONNECT')
  })
  _server.on('disconnect', function () {
    console.log('SERVER DISCONNECT')
  })
  var client = bs(_client)
  var server = bs(_server)

  var randoms = []
  function rand() {
    var r
    randoms.push(r = Math.random())
    return r
  }
  var clientErr = false, serverErr = false
  process.on('exit', function () {
    a.ok(clientErr, 'expected client to emit an error')
    a.ok(serverErr, 'expected server to emit an error')
    console.log('end point emitted errors correctly')
  })

  var streams = 0, ended = 0
  server.on('connection', function (stream) {
    streams ++
    stream
      .on('data', function (data) {
        var r 
        a.equal(data, r = randoms.shift())
        console.log('data', r)
      })
      .on('error', function () {
        //I'm expecting this
        serverErr = true
        a.equal(streams, 1)
        console.log('error!')
      })
    var r = Math.random()
    var _ended = false
    stream.on('end', function () {
      a.ok(!_ended, 'end MUST only be emitted once')
      _ended = true
      a.equal(streams, ++ ended)
      console.log('end!')
    })
  })

  var c = client.createWriteStream('A')
  c.on('error', function () {
    //expecting this!
    clientErr = true
    console.log('error')
  })

  c.write(rand())
  c.write(rand())
  c.write(rand())
  c.write(rand())
  _client.disconnect()

  if(c.writable)
    c.write(rand())
  a.throws(function () { c.write(rand()) })

})();