test.js 7 KB
Newer Older
huahua committed
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
var tape = require('tape')
var through = require('through2')
var concat = require('concat-stream')
var stream = require('readable-stream')
var net = require('net')
var duplexify = require('./')

var HELLO_WORLD = (Buffer.from && Buffer.from !== Uint8Array.from)
 ? Buffer.from('hello world')
 : new Buffer('hello world')

tape('passthrough', function(t) {
  t.plan(2)

  var pt = through()
  var dup = duplexify(pt, pt)

  dup.end('hello world')
  dup.on('finish', function() {
    t.ok(true, 'should finish')
  })
  dup.pipe(concat(function(data) {
    t.same(data.toString(), 'hello world', 'same in as out')
  }))
})

tape('passthrough + double end', function(t) {
  t.plan(2)

  var pt = through()
  var dup = duplexify(pt, pt)

  dup.end('hello world')
  dup.end()

  dup.on('finish', function() {
    t.ok(true, 'should finish')
  })
  dup.pipe(concat(function(data) {
    t.same(data.toString(), 'hello world', 'same in as out')
  }))
})

tape('async passthrough + end', function(t) {
  t.plan(2)

  var pt = through.obj({highWaterMark:1}, function(data, enc, cb) {
    setTimeout(function() {
      cb(null, data)
    }, 100)
  })

  var dup = duplexify(pt, pt)

  dup.write('hello ')
  dup.write('world')
  dup.end()

  dup.on('finish', function() {
    t.ok(true, 'should finish')
  })
  dup.pipe(concat(function(data) {
    t.same(data.toString(), 'hello world', 'same in as out')
  }))
})

tape('duplex', function(t) {
  var readExpected = ['read-a', 'read-b', 'read-c']
  var writeExpected = ['write-a', 'write-b', 'write-c']

  t.plan(readExpected.length+writeExpected.length+2)

  var readable = through.obj()
  var writable = through.obj(function(data, enc, cb) {
    t.same(data, writeExpected.shift(), 'onwrite should match')
    cb()
  })

  var dup = duplexify.obj(writable, readable)

  readExpected.slice().forEach(function(data) {
    readable.write(data)
  })
  readable.end()

  writeExpected.slice().forEach(function(data) {
    dup.write(data)
  })
  dup.end()

  dup.on('data', function(data) {
    t.same(data, readExpected.shift(), 'ondata should match')
  })
  dup.on('end', function() {
    t.ok(true, 'should end')
  })
  dup.on('finish', function() {
    t.ok(true, 'should finish')
  })
})

tape('async', function(t) {
  var dup = duplexify()
  var pt = through()

  dup.pipe(concat(function(data) {
    t.same(data.toString(), 'i was async', 'same in as out')
    t.end()
  }))

  dup.write('i')
  dup.write(' was ')
  dup.end('async')

  setTimeout(function() {
    dup.setWritable(pt)
    setTimeout(function() {
      dup.setReadable(pt)
    }, 50)
  }, 50)
})

tape('destroy', function(t) {
  t.plan(2)

  var write = through()
  var read = through()
  var dup = duplexify(write, read)

  write.destroy = function() {
    t.ok(true, 'write destroyed')
  }

  dup.on('close', function() {
    t.ok(true, 'close emitted')
  })

  dup.destroy()
  dup.destroy() // should only work once
  dup.end()
})

tape('destroy both', function(t) {
  t.plan(3)

  var write = through()
  var read = through()
  var dup = duplexify(write, read)

  write.destroy = function() {
    t.ok(true, 'write destroyed')
  }

  read.destroy = function() {
    t.ok(true, 'read destroyed')
  }

  dup.on('close', function() {
    t.ok(true, 'close emitted')
  })

  dup.destroy()
  dup.destroy() // should only work once
})

tape('bubble read errors', function(t) {
  t.plan(2)

  var write = through()
  var read = through()
  var dup = duplexify(write, read)

  dup.on('error', function(err) {
    t.same(err.message, 'read-error', 'received read error')
  })
  dup.on('close', function() {
    t.ok(true, 'close emitted')
  })

  read.emit('error', new Error('read-error'))
  write.emit('error', new Error('write-error')) // only emit first error
})

tape('bubble write errors', function(t) {
  t.plan(2)

  var write = through()
  var read = through()
  var dup = duplexify(write, read)

  dup.on('error', function(err) {
    t.same(err.message, 'write-error', 'received write error')
  })
  dup.on('close', function() {
    t.ok(true, 'close emitted')
  })

  write.emit('error', new Error('write-error'))
  read.emit('error', new Error('read-error')) // only emit first error
})

tape('bubble errors from write()', function(t) {
  t.plan(3)

  var errored = false
  var dup = duplexify(new stream.Writable({
    write: function(chunk, enc, next) {
      next(new Error('write-error'))
    }
  }))

  dup.on('error', function(err) {
    errored = true
    t.same(err.message, 'write-error', 'received write error')
  })
  dup.on('close', function() {
    t.pass('close emitted')
    t.ok(errored, 'error was emitted before close')
  })
  dup.end('123')
})

tape('destroy while waiting for drain', function(t) {
  t.plan(3)

  var errored = false
  var dup = duplexify(new stream.Writable({
    highWaterMark: 0,
    write: function() {}
  }))

  dup.on('error', function(err) {
    errored = true
    t.same(err.message, 'destroy-error', 'received destroy error')
  })
  dup.on('close', function() {
    t.pass('close emitted')
    t.ok(errored, 'error was emitted before close')
  })
  dup.write('123')
  dup.destroy(new Error('destroy-error'))
})

tape('reset writable / readable', function(t) {
  t.plan(3)

  var toUpperCase = function(data, enc, cb) {
    cb(null, data.toString().toUpperCase())
  }

  var passthrough = through()
  var upper = through(toUpperCase)
  var dup = duplexify(passthrough, passthrough)

  dup.once('data', function(data) {
    t.same(data.toString(), 'hello')
    dup.setWritable(upper)
    dup.setReadable(upper)
    dup.once('data', function(data) {
      t.same(data.toString(), 'HELLO')
      dup.once('data', function(data) {
        t.same(data.toString(), 'HI')
        t.end()
      })
    })
    dup.write('hello')
    dup.write('hi')
  })
  dup.write('hello')
})

tape('cork', function(t) {
  var passthrough = through()
  var dup = duplexify(passthrough, passthrough)
  var ok = false

  dup.on('prefinish', function() {
    dup.cork()
    setTimeout(function() {
      ok = true
      dup.uncork()
    }, 100)
  })
  dup.on('finish', function() {
    t.ok(ok)
    t.end()
  })
  dup.end()
})

tape('prefinish not twice', function(t) {
  var passthrough = through()
  var dup = duplexify(passthrough, passthrough)
  var prefinished = false

  dup.on('prefinish', function() {
    t.ok(!prefinished, 'only prefinish once')
    prefinished = true
  })

  dup.on('finish', function() {
    t.end()
  })

  dup.end()
})

tape('close', function(t) {
  var passthrough = through()
  var dup = duplexify(passthrough, passthrough)

  passthrough.emit('close')
  dup.on('close', function() {
    t.ok(true, 'should forward close')
    t.end()
  })
})

tape('works with node native streams (net)', function(t) {
  t.plan(1)

  var server = net.createServer(function(socket) {
    var dup = duplexify(socket, socket)

    dup.once('data', function(chunk) {
      t.same(chunk, HELLO_WORLD)
      server.close()
      socket.end()
      t.end()
    })
  })

  server.listen(0, function () {
    var socket = net.connect(server.address().port)
    var dup = duplexify(socket, socket)

    dup.write(HELLO_WORLD)
  })
})