diff options
Diffstat (limited to 'node_modules/cloneable-readable/test.js')
-rw-r--r-- | node_modules/cloneable-readable/test.js | 162 |
1 files changed, 135 insertions, 27 deletions
diff --git a/node_modules/cloneable-readable/test.js b/node_modules/cloneable-readable/test.js index e1d88d4d9..37379c361 100644 --- a/node_modules/cloneable-readable/test.js +++ b/node_modules/cloneable-readable/test.js @@ -1,7 +1,10 @@ 'use strict' +var fs = require('fs') +var path = require('path') var test = require('tape').test var from = require('from2') +var crypto = require('crypto') var sink = require('flush-write-stream') var cloneable = require('./') @@ -297,7 +300,7 @@ test('basic passthrough with readable event on clone', function (t) { }) test('source error destroys all', function (t) { - t.plan(5) + t.plan(3) var source = from() var instance = cloneable(source) @@ -310,17 +313,9 @@ test('source error destroys all', function (t) { t.ok(err === err2, 'instance receives same error') }) - instance.on('close', function () { - t.pass('instance is closed') - }) - clone.on('error', function (err3) { t.ok(err === err3, 'clone receives same error') }) - - clone.on('close', function () { - t.pass('clone is closed') - }) }) source.emit('error', new Error()) @@ -333,19 +328,22 @@ test('source destroy destroys all', function (t) { var instance = cloneable(source) var clone = instance.clone() - instance.on('close', function () { - t.pass('instance is closed') + instance.on('end', function () { + t.pass('instance has ended') }) - clone.on('close', function () { - t.pass('clone is closed') + clone.on('end', function () { + t.pass('clone has ended') }) + clone.resume() + instance.resume() + source.destroy() }) test('instance error destroys all but the source', function (t) { - t.plan(4) + t.plan(2) var source = from() var instance = cloneable(source) @@ -357,18 +355,18 @@ test('instance error destroys all but the source', function (t) { instance.on('error', function (err) { t.is(err.message, 'beep', 'instance errors') + }) - instance.on('close', function () { - t.pass('instance is closed') - }) + instance.on('close', function () { + t.fail('close should not be emitted') + }) - clone.on('error', function (err3) { - t.ok(err === err3, 'clone receives same error') - }) + clone.on('error', function (err) { + t.is(err.message, 'beep', 'instance errors') + }) - clone.on('close', function () { - t.pass('clone is closed') - }) + clone.on('close', function () { + t.fail('close should not be emitted') }) instance.destroy(new Error('beep')) @@ -385,14 +383,17 @@ test('instance destroy destroys all but the source', function (t) { t.fail('source should not be closed') }) - instance.on('close', function () { - t.pass('instance is closed') + instance.on('end', function () { + t.pass('instance has ended') }) - clone.on('close', function () { - t.pass('clone is closed') + clone.on('end', function () { + t.pass('clone has ended') }) + instance.resume() + clone.resume() + instance.destroy() }) @@ -574,3 +575,110 @@ test('isCloneable', function (t) { var cloneClone = clone.clone() t.ok(cloneable.isCloneable(cloneClone), 'a clone of a clone is cloneable') }) + +test('emits finish', function (t) { + var chunks = ['a', 'b', 'c', 'd', null] + var e1 = ['a', 'b', 'c', 'd'] + var e2 = ['a', 'b', 'c', 'd'] + + t.plan(2 + e1.length + e2.length) + + var source = from(function (size, next) { + setImmediate(next, null, chunks.shift()) + }) + + var instance = cloneable(source) + + var clone = instance.clone() + + clone.on('finish', t.pass.bind(null, 'clone emits finish')) + instance.on('finish', t.pass.bind(null, 'main emits finish')) + + instance.pipe(sink(function (chunk, enc, cb) { + t.equal(chunk.toString(), e1.shift(), 'chunk matches') + cb() + })) + + clone.on('data', function (chunk) { + t.equal(chunk.toString(), e2.shift(), 'chunk matches') + }) +}) + +test('clone async w resume', function (t) { + t.plan(4) + + var read = false + var source = from(function (size, next) { + if (read) { + this.push(null) + } else { + read = true + this.push('hello world') + } + next() + }) + + var instance = cloneable(source) + t.notOk(read, 'stream not started') + + var cloned = instance.clone() + t.notOk(read, 'stream not started') + + instance.on('end', t.pass.bind(null, 'end emitted')) + instance.resume() + + setImmediate(function () { + cloned.on('end', t.pass.bind(null, 'end emitted')) + cloned.resume() + }) +}) + +test('big file', function (t) { + t.plan(13) + + var stream = cloneable(fs.createReadStream(path.join(__dirname, 'big'))) + var hash = crypto.createHash('sha1') + hash.setEncoding('hex') + + var toCheck + + fs.createReadStream(path.join(__dirname, 'big')) + .pipe(hash) + .once('readable', function () { + toCheck = hash.read() + t.ok(toCheck) + }) + + function pipe (s, num) { + s.on('end', function () { + t.pass('end for ' + num) + }) + + var dest = path.join(__dirname, 'out') + + s.pipe(fs.createWriteStream(dest)) + .on('finish', function () { + t.pass('finish for ' + num) + + var destHash = crypto.createHash('sha1') + destHash.setEncoding('hex') + + fs.createReadStream(dest) + .pipe(destHash) + .once('readable', function () { + var hash = destHash.read() + t.ok(hash) + t.equal(hash, toCheck) + }) + }) + } + + // Pipe in another event loop tick <-- this one finished only, it's the original cloneable. + setImmediate(pipe.bind(null, stream, 1)) + + // Pipe in the same event loop tick + pipe(stream.clone(), 0) + + // Pipe a long time after + setTimeout(pipe.bind(null, stream.clone(), 2), 1000) +}) |