Permalink
Browse files

use tests from node-core

  • Loading branch information...
1 parent 7e09d0b commit 327af9a535d3fcfe21fcbb2d5147e53de0263e23 @isaacs isaacs committed Dec 1, 2012
View
191 test/common.js
@@ -0,0 +1,191 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var path = require('path');
+var assert = require('assert');
+
+exports.testDir = path.dirname(__filename);
+exports.fixturesDir = path.join(exports.testDir, 'fixtures');
+exports.libDir = path.join(exports.testDir, '../lib');
+exports.tmpDir = path.join(exports.testDir, 'tmp');
+exports.PORT = 12346;
+
+if (process.platform === 'win32') {
+ exports.PIPE = '\\\\.\\pipe\\libuv-test';
+} else {
+ exports.PIPE = exports.tmpDir + '/test.sock';
+}
+
+var util = require('util');
+for (var i in util) exports[i] = util[i];
+//for (var i in exports) global[i] = exports[i];
+
+function protoCtrChain(o) {
+ var result = [];
+ for (; o; o = o.__proto__) { result.push(o.constructor); }
+ return result.join();
+}
+
+exports.indirectInstanceOf = function(obj, cls) {
+ if (obj instanceof cls) { return true; }
+ var clsChain = protoCtrChain(cls.prototype);
+ var objChain = protoCtrChain(obj);
+ return objChain.slice(-clsChain.length) === clsChain;
+};
+
+
+exports.ddCommand = function(filename, kilobytes) {
+ if (process.platform === 'win32') {
+ var p = path.resolve(exports.fixturesDir, 'create-file.js');
+ return '"' + process.argv[0] + '" "' + p + '" "' +
+ filename + '" ' + (kilobytes * 1024);
+ } else {
+ return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes;
+ }
+};
+
+
+exports.spawnPwd = function(options) {
+ var spawn = require('child_process').spawn;
+
+ if (process.platform === 'win32') {
+ return spawn('cmd.exe', ['/c', 'cd'], options);
+ } else {
+ return spawn('pwd', [], options);
+ }
+};
+
+
+// Turn this off if the test should not check for global leaks.
+exports.globalCheck = true;
+
+process.on('exit', function() {
+ if (!exports.globalCheck) return;
+ var knownGlobals = [setTimeout,
+ setInterval,
+ setImmediate,
+ clearTimeout,
+ clearInterval,
+ clearImmediate,
+ console,
+ Buffer,
+ process,
+ global];
+
+ if (global.errno) {
+ knownGlobals.push(errno);
+ }
+
+ if (global.gc) {
+ knownGlobals.push(gc);
+ }
+
+ if (global.DTRACE_HTTP_SERVER_RESPONSE) {
+ knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
+ knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
+ knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
+ knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
+ knownGlobals.push(DTRACE_NET_STREAM_END);
+ knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
+ knownGlobals.push(DTRACE_NET_SOCKET_READ);
+ knownGlobals.push(DTRACE_NET_SOCKET_WRITE);
+ }
+ if (global.COUNTER_NET_SERVER_CONNECTION) {
+ knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
+ knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
+ knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
+ knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
+ knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
+ knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
+ }
+
+ if (global.ArrayBuffer) {
+ knownGlobals.push(ArrayBuffer);
+ knownGlobals.push(Int8Array);
+ knownGlobals.push(Uint8Array);
+ knownGlobals.push(Uint8ClampedArray);
+ knownGlobals.push(Int16Array);
+ knownGlobals.push(Uint16Array);
+ knownGlobals.push(Int32Array);
+ knownGlobals.push(Uint32Array);
+ knownGlobals.push(Float32Array);
+ knownGlobals.push(Float64Array);
+ knownGlobals.push(DataView);
+ }
+
+ for (var x in global) {
+ var found = false;
+
+ for (var y in knownGlobals) {
+ if (global[x] === knownGlobals[y]) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ console.error('Unknown global: %s', x);
+ assert.ok(false, 'Unknown global found');
+ }
+ }
+});
+
+
+var mustCallChecks = [];
+
+
+function runCallChecks() {
+ var failed = mustCallChecks.filter(function(context) {
+ return context.actual !== context.expected;
+ });
+
+ failed.forEach(function(context) {
+ console.log('Mismatched %s function calls. Expected %d, actual %d.',
+ context.name,
+ context.expected,
+ context.actual);
+ console.log(context.stack.split('\n').slice(2).join('\n'));
+ });
+
+ if (failed.length) process.exit(1);
+}
+
+
+exports.mustCall = function(fn, expected) {
+ if (typeof expected !== 'number') expected = 1;
+
+ var context = {
+ expected: expected,
+ actual: 0,
+ stack: (new Error).stack,
+ name: fn.name || '<anonymous>'
+ };
+
+ // add the exit listener only once to avoid listener leak warnings
+ if (mustCallChecks.length === 0) process.on('exit', runCallChecks);
+
+ mustCallChecks.push(context);
+
+ return function() {
+ context.actual++;
+ return fn.apply(this, arguments);
+ };
+};
View
62 test/from-list.js
@@ -1,62 +0,0 @@
-var test = require('tap').test;
-var fromList = require('../readable.js')._fromList;
-
-test('buffers', function(t) {
- // have a length
- var len = 16;
- var list = [ new Buffer('foog'),
- new Buffer('bark'),
- new Buffer('bazy'),
- new Buffer('kuel') ];
-
- // read more than the first element.
- var ret = fromList(6, list, 16);
- t.equal(ret.toString(), 'foogba');
-
- // read exactly the first element.
- ret = fromList(2, list, 10);
- t.equal(ret.toString(), 'rk');
-
- // read less than the first element.
- ret = fromList(2, list, 8);
- t.equal(ret.toString(), 'ba');
-
- // read more than we have.
- ret = fromList(100, list, 6);
- t.equal(ret.toString(), 'zykuel');
-
- // all consumed.
- t.same(list, []);
-
- t.end();
-});
-
-test('strings', function(t) {
- // have a length
- var len = 16;
- var list = [ 'foog',
- 'bark',
- 'bazy',
- 'kuel' ];
-
- // read more than the first element.
- var ret = fromList(6, list, 16, true);
- t.equal(ret, 'foogba');
-
- // read exactly the first element.
- ret = fromList(2, list, 10, true);
- t.equal(ret, 'rk');
-
- // read less than the first element.
- ret = fromList(2, list, 8, true);
- t.equal(ret, 'ba');
-
- // read more than we have.
- ret = fromList(100, list, 6, true);
- t.equal(ret, 'zykuel');
-
- // all consumed.
- t.same(list, []);
-
- t.end();
-});
View
54 test/fs.js
@@ -1,54 +0,0 @@
-var test = require('tap').test
-var FSReadable = require('../fs.js');
-var fs = require('fs');
-
-var path = require('path');
-var file = path.resolve(__dirname, 'fixtures', 'x1024.txt');
-
-var size = fs.statSync(file).size;
-
-// expect to see chunks no more than 10 bytes each.
-var expectLengths = [];
-for (var i = size; i > 0; i -= 10) {
- expectLengths.push(Math.min(i, 10));
-}
-
-var util = require('util');
-var Stream = require('stream');
-
-util.inherits(TestWriter, Stream);
-
-function TestWriter() {
- Stream.apply(this);
- this.buffer = [];
- this.length = 0;
-}
-
-TestWriter.prototype.write = function(c) {
- this.buffer.push(c.toString());
- this.length += c.length;
- return true;
-};
-
-TestWriter.prototype.end = function(c) {
- if (c) this.buffer.push(c.toString());
- this.emit('results', this.buffer);
-}
-
-test('fs test', function(t) {
- var r = new FSReadable(file, { bufferSize: 10 });
- var w = new TestWriter();
-
- w.on('results', function(res) {
- console.error(res, w.length);
- t.equal(w.length, size);
- var l = 0;
- t.same(res.map(function (c) {
- return c.length;
- }), expectLengths);
- t.end();
- });
-
- r.pipe(w, { chunkSize: 10 });
-
-});
View
69 test/basic.js → test/simple/test-stream2-basic.js
@@ -1,5 +1,28 @@
-var tap = require('tap');
-var R = require('../readable');
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+var common = require('../common.js');
+var R = require('../../readable');
+var assert = require('assert');
var util = require('util');
var EE = require('events').EventEmitter;
@@ -71,7 +94,31 @@ TestWriter.prototype.end = function(c) {
////////
-tap.test('a most basic test', function(t) {
+// tiny node-tap lookalike.
+var tests = [];
+function test(name, fn) {
+ tests.push([name, fn]);
+}
+
+function run() {
+ var next = tests.shift();
+ if (!next)
+ return console.error('ok');
+
+ var name = next[0];
+ var fn = next[1];
+ console.log('# %s', name);
+ fn({
+ same: assert.deepEqual,
+ equal: assert.equal,
+ end: run
+ });
+}
+
+process.nextTick(run);
+
+
+test('a most basic test', function(t) {
var r = new TestReader(20);
var reads = [];
@@ -113,7 +160,7 @@ tap.test('a most basic test', function(t) {
flow();
});
-tap.test('pipe', function(t) {
+test('pipe', function(t) {
var r = new TestReader(5);
var expect = [ 'xxxxx',
@@ -140,7 +187,7 @@ tap.test('pipe', function(t) {
[1,2,3,4,5,6,7,8,9].forEach(function(SPLIT) {
- tap.test('unpipe', function(t) {
+ test('unpipe', function(t) {
var r = new TestReader(5);
// unpipe after 3 writes, then write to another stream instead.
@@ -162,19 +209,27 @@ tap.test('pipe', function(t) {
w[0].on('write', function() {
if (--writes === 0) {
r.unpipe();
+ t.equal(r._readableState.pipes, null);
w[0].end();
r.pipe(w[1]);
+ t.equal(r._readableState.pipes, w[1]);
}
});
var ended = 0;
+ var ended0 = false;
+ var ended1 = false;
w[0].on('end', function(results) {
+ t.equal(ended0, false);
+ ended0 = true;
ended++;
t.same(results, expect[0]);
});
w[1].on('end', function(results) {
+ t.equal(ended1, false);
+ ended1 = true;
ended++;
t.equal(ended, 2);
t.same(results, expect[1]);
@@ -187,7 +242,7 @@ tap.test('pipe', function(t) {
// both writers should get the same exact data.
-tap.test('multipipe', function(t) {
+test('multipipe', function(t) {
var r = new TestReader(5);
var w = [ new TestWriter, new TestWriter ];
@@ -218,7 +273,7 @@ tap.test('multipipe', function(t) {
[1,2,3,4,5,6,7,8,9].forEach(function(SPLIT) {
- tap.test('multi-unpipe', function(t) {
+ test('multi-unpipe', function(t) {
var r = new TestReader(5);
// unpipe after 3 writes, then write to another stream instead.
View
76 test/simple/test-stream2-fs.js
@@ -0,0 +1,76 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+var common = require('../common.js');
+var R = require('../../readable');
+var assert = require('assert');
+
+var fs = require('fs');
+var FSReadable = fs.ReadStream;
+
+var path = require('path');
+var file = path.resolve(common.fixturesDir, 'x1024.txt');
+
+var size = fs.statSync(file).size;
+
+// expect to see chunks no more than 10 bytes each.
+var expectLengths = [];
+for (var i = size; i > 0; i -= 10) {
+ expectLengths.push(Math.min(i, 10));
+}
+
+var util = require('util');
+var Stream = require('stream');
+
+util.inherits(TestWriter, Stream);
+
+function TestWriter() {
+ Stream.apply(this);
+ this.buffer = [];
+ this.length = 0;
+}
+
+TestWriter.prototype.write = function(c) {
+ this.buffer.push(c.toString());
+ this.length += c.length;
+ return true;
+};
+
+TestWriter.prototype.end = function(c) {
+ if (c) this.buffer.push(c.toString());
+ this.emit('results', this.buffer);
+}
+
+var r = new FSReadable(file, { bufferSize: 10 });
+var w = new TestWriter();
+
+w.on('results', function(res) {
+ console.error(res, w.length);
+ assert.equal(w.length, size);
+ var l = 0;
+ assert.deepEqual(res.map(function (c) {
+ return c.length;
+ }), expectLengths);
+ console.log('ok');
+});
+
+r.pipe(w, { chunkSize: 10 });
View
107 test/simple/test-stream2-pipe-error-handling.js
@@ -0,0 +1,107 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var stream = require('stream');
+var Readable = require('../../readable.js');
+var Writable = require('../../writable.js');
+
+(function testErrorListenerCatches() {
+ var count = 1000;
+
+ var source = new Readable();
+ source._read = function(n, cb) {
+ n = Math.min(count, n);
+ count -= n;
+ cb(null, new Buffer(n));
+ };
+
+ var unpipedDest;
+ source.unpipe = function(dest) {
+ unpipedDest = dest;
+ Readable.prototype.unpipe.call(this, dest);
+ };
+
+ var dest = new Writable();
+ dest._write = function(chunk, cb) {
+ cb();
+ };
+
+ source.pipe(dest);
+
+ var gotErr = null;
+ dest.on('error', function(err) {
+ gotErr = err;
+ });
+
+ var unpipedSource;
+ dest.on('unpipe', function(src) {
+ unpipedSource = src;
+ });
+
+ var err = new Error('This stream turned into bacon.');
+ dest.emit('error', err);
+ assert.strictEqual(gotErr, err);
+ assert.strictEqual(unpipedSource, source);
+ assert.strictEqual(unpipedDest, dest);
+})();
+
+(function testErrorWithoutListenerThrows() {
+ var count = 1000;
+
+ var source = new Readable();
+ source._read = function(n, cb) {
+ n = Math.min(count, n);
+ count -= n;
+ cb(null, new Buffer(n));
+ };
+
+ var unpipedDest;
+ source.unpipe = function(dest) {
+ unpipedDest = dest;
+ Readable.prototype.unpipe.call(this, dest);
+ };
+
+ var dest = new Writable();
+ dest._write = function(chunk, cb) {
+ cb();
+ };
+
+ source.pipe(dest);
+
+ var unpipedSource;
+ dest.on('unpipe', function(src) {
+ unpipedSource = src;
+ });
+
+ var err = new Error('This stream turned into bacon.');
+
+ var gotErr = null;
+ try {
+ dest.emit('error', err);
+ } catch (e) {
+ gotErr = e;
+ }
+ assert.strictEqual(gotErr, err);
+ assert.strictEqual(unpipedSource, source);
+ assert.strictEqual(unpipedDest, dest);
+})();
View
109 test/simple/test-stream2-readable-from-list.js
@@ -0,0 +1,109 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var assert = require('assert');
+var common = require('../common.js');
+var fromList = require('../../readable')._fromList;
+
+// tiny node-tap lookalike.
+var tests = [];
+function test(name, fn) {
+ tests.push([name, fn]);
+}
+
+function run() {
+ var next = tests.shift();
+ if (!next)
+ return console.log('ok');
+
+ var name = next[0];
+ var fn = next[1];
+ console.log('# %s', name);
+ fn({
+ same: assert.deepEqual,
+ equal: assert.equal,
+ end: run
+ });
+}
+
+process.nextTick(run);
+
+
+
+test('buffers', function(t) {
+ // have a length
+ var len = 16;
+ var list = [ new Buffer('foog'),
+ new Buffer('bark'),
+ new Buffer('bazy'),
+ new Buffer('kuel') ];
+
+ // read more than the first element.
+ var ret = fromList(6, list, 16);
+ t.equal(ret.toString(), 'foogba');
+
+ // read exactly the first element.
+ ret = fromList(2, list, 10);
+ t.equal(ret.toString(), 'rk');
+
+ // read less than the first element.
+ ret = fromList(2, list, 8);
+ t.equal(ret.toString(), 'ba');
+
+ // read more than we have.
+ ret = fromList(100, list, 6);
+ t.equal(ret.toString(), 'zykuel');
+
+ // all consumed.
+ t.same(list, []);
+
+ t.end();
+});
+
+test('strings', function(t) {
+ // have a length
+ var len = 16;
+ var list = [ 'foog',
+ 'bark',
+ 'bazy',
+ 'kuel' ];
+
+ // read more than the first element.
+ var ret = fromList(6, list, 16, true);
+ t.equal(ret, 'foogba');
+
+ // read exactly the first element.
+ ret = fromList(2, list, 10, true);
+ t.equal(ret, 'rk');
+
+ // read less than the first element.
+ ret = fromList(2, list, 8, true);
+ t.equal(ret, 'ba');
+
+ // read more than we have.
+ ret = fromList(100, list, 6, true);
+ t.equal(ret, 'zykuel');
+
+ // all consumed.
+ t.same(list, []);
+
+ t.end();
+});
View
54 test/set-encoding.js → test/simple/test-stream2-set-encoding.js
@@ -1,7 +1,55 @@
-var R = require('../readable.js');
-var test = require('tap').test;
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+var common = require('../common.js');
+var assert = require('assert');
+var R = require('../../readable');
var util = require('util');
+// tiny node-tap lookalike.
+var tests = [];
+function test(name, fn) {
+ tests.push([name, fn]);
+}
+
+function run() {
+ var next = tests.shift();
+ if (!next)
+ return console.log('ok');
+
+ var name = next[0];
+ var fn = next[1];
+ console.log('# %s', name);
+ fn({
+ same: assert.deepEqual,
+ equal: assert.equal,
+ end: run
+ });
+}
+
+process.nextTick(run);
+
+/////
+
util.inherits(TestReader, R);
function TestReader(n, opts) {
@@ -30,7 +78,7 @@ TestReader.prototype._read = function(n, cb) {
ret.fill('a');
return cb(null, ret);
- }.bind(this), 10);
+ }.bind(this), 1);
};
test('setEncoding utf8', function(t) {
View
116 test/transform.js → test/simple/test-stream2-transform.js
@@ -1,9 +1,56 @@
-var PassThrough = require('../passthrough.js');
-var Transform = require('../transform.js');
-var test = require('tap').test;
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var assert = require('assert');
+var common = require('../common.js');
+var PassThrough = require('../../passthrough');
+var Transform = require('../../transform');
+
+// tiny node-tap lookalike.
+var tests = [];
+function test(name, fn) {
+ tests.push([name, fn]);
+}
+
+function run() {
+ var next = tests.shift();
+ if (!next)
+ return console.log('ok');
+
+ var name = next[0];
+ var fn = next[1];
+ console.log('# %s', name);
+ fn({
+ same: assert.deepEqual,
+ equal: assert.equal,
+ end: run
+ });
+}
+
+process.nextTick(run);
+
+/////
test('passthrough', function(t) {
- var pt = new PassThrough;
+ var pt = new PassThrough();
pt.write(new Buffer('foog'));
pt.write(new Buffer('bark'));
@@ -93,7 +140,7 @@ test('assymetric transform (expand)', function(t) {
t.equal(pt.read(5).toString(), 'uelku');
t.equal(pt.read(5).toString(), 'el');
t.end();
- }, 100);
+ }, 200);
});
test('assymetric transform (compress)', function(t) {
@@ -110,7 +157,6 @@ test('assymetric transform (compress)', function(t) {
setTimeout(function() {
this.state += s.charAt(0);
if (this.state.length === 3) {
- console.error('call output!')
output(new Buffer(this.state));
this.state = '';
}
@@ -155,25 +201,34 @@ test('assymetric transform (compress)', function(t) {
});
-test('passthrough reordered', function(t) {
- var pt = new PassThrough;
+test('passthrough event emission', function(t) {
+ var pt = new PassThrough({
+ lowWaterMark: 0
+ });
var emits = 0;
pt.on('readable', function() {
- console.error('emit readable', emits)
+ var state = pt._readableState;
+ console.error('>>> emit readable %d', emits);
emits++;
});
+ var i = 0;
+
pt.write(new Buffer('foog'));
pt.write(new Buffer('bark'));
t.equal(pt.read(5).toString(), 'foogb');
- t.equal(pt.read(5), null);
+ t.equal(pt.read(5) + '', 'null');
console.error('need emit 0');
pt.write(new Buffer('bazy'));
+ console.error('should have emitted, but not again');
pt.write(new Buffer('kuel'));
+ console.error('should have emitted readable now 1 === %d', emits);
+ t.equal(emits, 1);
+
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5), null);
@@ -182,13 +237,54 @@ test('passthrough reordered', function(t) {
pt.end();
+ t.equal(emits, 2);
+
t.equal(pt.read(5).toString(), 'l');
t.equal(pt.read(5), null);
+ console.error('should not have emitted again');
t.equal(emits, 2);
t.end();
});
+test('passthrough event emission reordered', function(t) {
+ var pt = new PassThrough;
+ var emits = 0;
+ pt.on('readable', function() {
+ console.error('emit readable', emits)
+ emits++;
+ });
+
+ pt.write(new Buffer('foog'));
+ pt.write(new Buffer('bark'));
+
+ t.equal(pt.read(5).toString(), 'foogb');
+ t.equal(pt.read(5), null);
+
+ console.error('need emit 0');
+ pt.once('readable', function() {
+ t.equal(pt.read(5).toString(), 'arkba');
+
+ t.equal(pt.read(5), null);
+
+ console.error('need emit 1');
+ pt.once('readable', function() {
+ t.equal(pt.read(5).toString(), 'zykue');
+ t.equal(pt.read(5), null);
+ pt.once('readable', function() {
+ t.equal(pt.read(5).toString(), 'l');
+ t.equal(pt.read(5), null);
+ t.equal(emits, 3);
+ t.end();
+ });
+ pt.end();
+ });
+ pt.write(new Buffer('kuel'));
+ });
+
+ pt.write(new Buffer('bazy'));
+});
+
test('passthrough facaded', function(t) {
console.error('passthrough facaded');
var pt = new PassThrough;
View
240 test/simple/test-stream2-writable.js
@@ -0,0 +1,240 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common.js');
+var W = require('../../writable');
+var assert = require('assert');
+
+var util = require('util');
+util.inherits(TestWriter, W);
+
+function TestWriter() {
+ W.apply(this, arguments);
+ this.buffer = [];
+ this.written = 0;
+}
+
+TestWriter.prototype._write = function(chunk, cb) {
+ // simulate a small unpredictable latency
+ setTimeout(function() {
+ this.buffer.push(chunk.toString());
+ this.written += chunk.length;
+ cb();
+ }.bind(this), Math.floor(Math.random() * 10));
+};
+
+var chunks = new Array(50);
+for (var i = 0; i < chunks.length; i++) {
+ chunks[i] = new Array(i + 1).join('x');
+}
+
+// tiny node-tap lookalike.
+var tests = [];
+function test(name, fn) {
+ tests.push([name, fn]);
+}
+
+function run() {
+ var next = tests.shift();
+ if (!next)
+ return console.log('ok');
+
+ var name = next[0];
+ var fn = next[1];
+ console.log('# %s', name);
+ fn({
+ same: assert.deepEqual,
+ equal: assert.equal,
+ end: run
+ });
+}
+
+process.nextTick(run);
+
+test('write fast', function(t) {
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 100
+ });
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got chunks in the right order');
+ t.end();
+ });
+
+ chunks.forEach(function(chunk) {
+ // screw backpressure. Just buffer it all up.
+ tw.write(chunk);
+ });
+ tw.end();
+});
+
+test('write slow', function(t) {
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 100
+ });
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got chunks in the right order');
+ t.end();
+ });
+
+ var i = 0;
+ (function W() {
+ tw.write(chunks[i++]);
+ if (i < chunks.length)
+ setTimeout(W, 10);
+ else
+ tw.end();
+ })();
+});
+
+test('write backpressure', function(t) {
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 50
+ });
+
+ var drains = 0;
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got chunks in the right order');
+ t.equal(drains, 17);
+ t.end();
+ });
+
+ tw.on('drain', function() {
+ drains++;
+ });
+
+ var i = 0;
+ (function W() {
+ do {
+ var ret = tw.write(chunks[i++]);
+ } while (ret !== false && i < chunks.length);
+
+ if (i < chunks.length) {
+ assert(tw._writableState.length >= 50);
+ tw.once('drain', W);
+ } else {
+ tw.end();
+ }
+ })();
+});
+
+test('write bufferize', function(t) {
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 100
+ });
+
+ var encodings =
+ [ 'hex',
+ 'utf8',
+ 'utf-8',
+ 'ascii',
+ 'binary',
+ 'base64',
+ 'ucs2',
+ 'ucs-2',
+ 'utf16le',
+ 'utf-16le',
+ undefined ];
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got the expected chunks');
+ });
+
+ chunks.forEach(function(chunk, i) {
+ var enc = encodings[ i % encodings.length ];
+ chunk = new Buffer(chunk);
+ tw.write(chunk.toString(enc), enc);
+ });
+ t.end();
+});
+
+test('write no bufferize', function(t) {
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 100,
+ decodeStrings: false
+ });
+
+ tw._write = function(chunk, cb) {
+ assert(Array.isArray(chunk));
+ assert(typeof chunk[0] === 'string');
+ chunk = new Buffer(chunk[0], chunk[1]);
+ return TestWriter.prototype._write.call(this, chunk, cb);
+ };
+
+ var encodings =
+ [ 'hex',
+ 'utf8',
+ 'utf-8',
+ 'ascii',
+ 'binary',
+ 'base64',
+ 'ucs2',
+ 'ucs-2',
+ 'utf16le',
+ 'utf-16le',
+ undefined ];
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got the expected chunks');
+ });
+
+ chunks.forEach(function(chunk, i) {
+ var enc = encodings[ i % encodings.length ];
+ chunk = new Buffer(chunk);
+ tw.write(chunk.toString(enc), enc);
+ });
+ t.end();
+});
+
+test('write callbacks', function (t) {
+ var callbacks = chunks.map(function(chunk, i) {
+ return [i, function(er) {
+ callbacks._called[i] = chunk;
+ }];
+ }).reduce(function(set, x) {
+ set['callback-' + x[0]] = x[1];
+ return set;
+ }, {});
+ callbacks._called = [];
+
+ var tw = new TestWriter({
+ lowWaterMark: 5,
+ highWaterMark: 100
+ });
+
+ tw.on('finish', function() {
+ t.same(tw.buffer, chunks, 'got chunks in the right order');
+ t.same(callbacks._called, chunks, 'called all callbacks');
+ t.end();
+ });
+
+ chunks.forEach(function(chunk, i) {
+ tw.write(chunk, callbacks['callback-' + i]);
+ });
+ tw.end();
+});
View
86 test/writable.js
@@ -1,86 +0,0 @@
-var test = require('tap').test;
-var W = require('../writable.js');
-
-var util = require('util');
-util.inherits(TestWriter, W);
-
-function TestWriter() {
- W.apply(this, arguments);
- this.buffer = [];
-}
-
-TestWriter.prototype._write = function(chunk, cb) {
- // simulate a small unpredictable latency
- setTimeout(function() {
- this.buffer.push(chunk);
- cb();
- }.bind(this), Math.floor(Math.random() * 10));
-};
-
-var chunks = new Array(50);
-for (var i = 0; i < chunks.length; i++) {
- chunks[i] = new Array(i + 1).join('x');
-}
-
-test('write fast', function(t) {
- var tw = new TestWriter({
- lowWaterMark: 5,
- highWaterMark: 100
- });
-
- tw.on('finish', function() {
- t.same(tw.buffer, chunks, 'got chunks in the right order');
- t.end();
- });
-
- chunks.forEach(function(chunk) {
- // screw backpressure. Just buffer it all up.
- tw.write(chunk);
- });
- tw.end();
-});
-
-test('write slow', function(t) {
- var tw = new TestWriter({
- lowWaterMark: 5,
- highWaterMark: 100
- });
-
- tw.on('finish', function() {
- t.same(tw.buffer, chunks, 'got chunks in the right order');
- t.end();
- });
-
- var i = 0;
- (function W() {
- tw.write(chunks[i++]);
- if (i < chunks.length)
- setTimeout(W, 10);
- else
- tw.end();
- })();
-});
-
-test('write backpressure', function(t) {
- var tw = new TestWriter({
- lowWaterMark: 5,
- highWaterMark: 100
- });
-
- tw.on('finish', function() {
- t.same(tw.buffer, chunks, 'got chunks in the right order');
- t.end();
- });
-
- var i = 0;
- (function W() {
- do {
- var ret = tw.write(chunks[i++]);
- } while (ret !== false);
-
- if (i < chunks.length)
- tw.once('drain', W);
- else
- tw.end();
- })();
-});

0 comments on commit 327af9a

Please sign in to comment.