test: use ES6 classes instead of util.inherits

PR-URL: https://github.com/nodejs/node/pull/16938
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
Tobias Nießen 2017-11-10 19:18:12 +01:00 committed by James M Snell
parent c5a49e148d
commit 3fe165ace6
25 changed files with 377 additions and 435 deletions

View File

@ -7,7 +7,6 @@ if (!common.hasCrypto)
const assert = require('assert'); const assert = require('assert');
const crypto = require('crypto'); const crypto = require('crypto');
const Stream = require('stream'); const Stream = require('stream');
const util = require('util');
const hasher1 = crypto.createHash('sha256'); const hasher1 = crypto.createHash('sha256');
const hasher2 = crypto.createHash('sha256'); const hasher2 = crypto.createHash('sha256');
@ -18,12 +17,12 @@ hasher1.end();
const expected = hasher1.read().toString('hex'); const expected = hasher1.read().toString('hex');
function OldStream() { class OldStream extends Stream {
Stream.call(this); constructor() {
super();
this.readable = true; this.readable = true;
}
} }
util.inherits(OldStream, Stream);
const stream = new OldStream(); const stream = new OldStream();

View File

@ -26,26 +26,26 @@ if (!common.hasCrypto)
const assert = require('assert'); const assert = require('assert');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
const crypto = require('crypto'); const crypto = require('crypto');
// Small stream to buffer converter
function Stream2buffer(callback) {
stream.Writable.call(this);
this._buffers = [];
this.once('finish', function() {
callback(null, Buffer.concat(this._buffers));
});
}
util.inherits(Stream2buffer, stream.Writable);
Stream2buffer.prototype._write = function(data, encodeing, done) {
this._buffers.push(data);
return done(null);
};
if (!common.hasFipsCrypto) { if (!common.hasFipsCrypto) {
// Small stream to buffer converter
class Stream2buffer extends stream.Writable {
constructor(callback) {
super();
this._buffers = [];
this.once('finish', function() {
callback(null, Buffer.concat(this._buffers));
});
}
_write(data, encodeing, done) {
this._buffers.push(data);
return done(null);
}
}
// Create an md5 hash of "Hallo world" // Create an md5 hash of "Hallo world"
const hasher1 = crypto.createHash('md5'); const hasher1 = crypto.createHash('md5');
hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) { hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {

View File

@ -24,12 +24,9 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const events = require('events'); const events = require('events');
const util = require('util');
function listener() {} function listener() {}
function listener2() {} function listener2() {}
class TestStream { constructor() { } }
util.inherits(TestStream, events.EventEmitter);
{ {
const ee = new events.EventEmitter(); const ee = new events.EventEmitter();
@ -81,6 +78,7 @@ util.inherits(TestStream, events.EventEmitter);
} }
{ {
class TestStream extends events.EventEmitter {}
const s = new TestStream(); const s = new TestStream();
assert.deepStrictEqual(s.listeners('foo'), []); assert.deepStrictEqual(s.listeners('foo'), []);
} }

View File

@ -2,38 +2,34 @@
require('../common'); require('../common');
const net = require('net'); const net = require('net');
const http = require('http'); const http = require('http');
const util = require('util');
function Agent() { class Agent extends http.Agent {
http.Agent.call(this); createConnection() {
} const socket = new net.Socket();
util.inherits(Agent, http.Agent);
Agent.prototype.createConnection = function() { socket.on('error', function() {
const self = this; socket.push('HTTP/1.1 200\r\n\r\n');
const socket = new net.Socket();
socket.on('error', function() {
socket.push('HTTP/1.1 200\r\n\r\n');
});
socket.on('newListener', function onNewListener(name) {
if (name !== 'error')
return;
socket.removeListener('newListener', onNewListener);
// Let other listeners to be set up too
process.nextTick(function() {
self.breakSocket(socket);
}); });
});
return socket; let onNewListener;
}; socket.on('newListener', onNewListener = (name) => {
if (name !== 'error')
return;
socket.removeListener('newListener', onNewListener);
Agent.prototype.breakSocket = function breakSocket(socket) { // Let other listeners to be set up too
socket.emit('error', new Error('Intentional error')); process.nextTick(() => {
}; this.breakSocket(socket);
});
});
return socket;
}
breakSocket(socket) {
socket.emit('error', new Error('Intentional error'));
}
}
const agent = new Agent(); const agent = new Agent();

View File

@ -23,41 +23,37 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const http = require('http'); const http = require('http');
const util = require('util');
const Duplex = require('stream').Duplex; const Duplex = require('stream').Duplex;
function FakeAgent() { class FakeAgent extends http.Agent {
http.Agent.call(this); createConnection() {
const s = new Duplex();
let once = false;
s._read = function() {
if (once)
return this.push(null);
once = true;
this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
this.push('b\r\nhello world\r\n');
this.readable = false;
this.push('0\r\n\r\n');
};
// Blackhole
s._write = function(data, enc, cb) {
cb();
};
s.destroy = s.destroySoon = function() {
this.writable = false;
};
return s;
}
} }
util.inherits(FakeAgent, http.Agent);
FakeAgent.prototype.createConnection = function() {
const s = new Duplex();
let once = false;
s._read = function() {
if (once)
return this.push(null);
once = true;
this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
this.push('b\r\nhello world\r\n');
this.readable = false;
this.push('0\r\n\r\n');
};
// Blackhole
s._write = function(data, enc, cb) {
cb();
};
s.destroy = s.destroySoon = function() {
this.writable = false;
};
return s;
};
let received = ''; let received = '';

View File

@ -2,17 +2,13 @@
const common = require('../common'); const common = require('../common');
const http = require('http'); const http = require('http');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
const stream = require('stream'); const stream = require('stream');
// Verify that when piping a stream to an `OutgoingMessage` (or a type that // Verify that when piping a stream to an `OutgoingMessage` (or a type that
// inherits from `OutgoingMessage`), if data is emitted after the // inherits from `OutgoingMessage`), if data is emitted after the
// `OutgoingMessage` was closed - a `write after end` error is raised // `OutgoingMessage` was closed - a `write after end` error is raised
function MyStream() { class MyStream extends stream {}
stream.call(this);
}
util.inherits(MyStream, stream);
const server = http.createServer(common.mustCall(function(req, res) { const server = http.createServer(common.mustCall(function(req, res) {
const myStream = new MyStream(); const myStream = new MyStream();

View File

@ -27,17 +27,14 @@ const assert = require('assert');
const readline = require('readline'); const readline = require('readline');
const internalReadline = require('internal/readline'); const internalReadline = require('internal/readline');
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
const { Writable, Readable } = require('stream'); const { Writable, Readable } = require('stream');
function FakeInput() { class FakeInput extends EventEmitter {
EventEmitter.call(this); resume() {}
pause() {}
write() {}
end() {}
} }
inherits(FakeInput, EventEmitter);
FakeInput.prototype.resume = () => {};
FakeInput.prototype.pause = () => {};
FakeInput.prototype.write = () => {};
FakeInput.prototype.end = () => {};
function isWarned(emitter) { function isWarned(emitter) {
for (const name in emitter) { for (const name in emitter) {

View File

@ -2,14 +2,9 @@
const common = require('../common'); const common = require('../common');
const PassThrough = require('stream').PassThrough; const PassThrough = require('stream').PassThrough;
const assert = require('assert'); const assert = require('assert');
const inherits = require('util').inherits;
const Interface = require('readline').Interface; const Interface = require('readline').Interface;
class FakeInput extends PassThrough {}
function FakeInput() {
PassThrough.call(this);
}
inherits(FakeInput, PassThrough);
function extend(k) { function extend(k) {
return Object.assign({ ctrl: false, meta: false, shift: false }, k); return Object.assign({ ctrl: false, meta: false, shift: false }, k);

View File

@ -22,31 +22,26 @@
'use strict'; 'use strict';
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
const stream = require('stream'); const stream = require('stream');
let passed = false; let passed = false;
function PassThrough() { class PassThrough extends stream.Transform {
stream.Transform.call(this); _transform(chunk, encoding, done) {
} this.push(chunk);
util.inherits(PassThrough, stream.Transform); done();
PassThrough.prototype._transform = function(chunk, encoding, done) {
this.push(chunk);
done();
};
function TestStream() {
stream.Transform.call(this);
}
util.inherits(TestStream, stream.Transform);
TestStream.prototype._transform = function(chunk, encoding, done) {
if (!passed) {
// Char 'a' only exists in the last write
passed = chunk.toString().includes('a');
} }
done(); }
};
class TestStream extends stream.Transform {
_transform(chunk, encoding, done) {
if (!passed) {
// Char 'a' only exists in the last write
passed = chunk.toString().includes('a');
}
done();
}
}
const s1 = new PassThrough(); const s1 = new PassThrough();
const s2 = new PassThrough(); const s2 = new PassThrough();

View File

@ -1,26 +1,23 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
function Writable() { class Writable extends stream.Writable {
this.writable = true; constructor() {
stream.Writable.call(this); super();
this.prependListener = undefined; this.prependListener = undefined;
} }
util.inherits(Writable, stream.Writable);
Writable.prototype._write = function(chunk, end, cb) {
cb();
};
function Readable() { _write(chunk, end, cb) {
this.readable = true; cb();
stream.Readable.call(this); }
}
class Readable extends stream.Readable {
_read() {
this.push(null);
}
} }
util.inherits(Readable, stream.Readable);
Readable.prototype._read = function() {
this.push(null);
};
const w = new Writable(); const w = new Writable();
w.on('pipe', common.mustCall()); w.on('pipe', common.mustCall());

View File

@ -24,36 +24,33 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const Readable = require('_stream_readable'); const Readable = require('_stream_readable');
const Writable = require('_stream_writable'); const Writable = require('_stream_writable');
const util = require('util');
util.inherits(TestReadable, Readable); class TestReadable extends Readable {
function TestReadable(opt) { constructor(opt) {
if (!(this instanceof TestReadable)) super(opt);
return new TestReadable(opt); this._ended = false;
Readable.call(this, opt); }
this._ended = false;
_read() {
if (this._ended)
this.emit('error', new Error('_read called twice'));
this._ended = true;
this.push(null);
}
} }
TestReadable.prototype._read = function() { class TestWritable extends Writable {
if (this._ended) constructor(opt) {
this.emit('error', new Error('_read called twice')); super(opt);
this._ended = true; this._written = [];
this.push(null); }
};
util.inherits(TestWritable, Writable); _write(chunk, encoding, cb) {
function TestWritable(opt) { this._written.push(chunk);
if (!(this instanceof TestWritable)) cb();
return new TestWritable(opt); }
Writable.call(this, opt);
this._written = [];
} }
TestWritable.prototype._write = function(chunk, encoding, cb) {
this._written.push(chunk);
cb();
};
// this one should not emit 'end' until we read() from it later. // this one should not emit 'end' until we read() from it later.
const ender = new TestReadable(); const ender = new TestReadable();

View File

@ -24,32 +24,32 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const Readable = require('stream').Readable; const Readable = require('stream').Readable;
const util = require('util');
util.inherits(MyStream, Readable); class MyStream extends Readable {
function MyStream(options) { constructor(options) {
Readable.call(this, options); super(options);
this._chunks = 3; this._chunks = 3;
}
MyStream.prototype._read = function(n) {
switch (this._chunks--) {
case 0:
return this.push(null);
case 1:
return setTimeout(function() {
this.push('last chunk');
}.bind(this), 100);
case 2:
return this.push('second to last chunk');
case 3:
return process.nextTick(function() {
this.push('first chunk');
}.bind(this));
default:
throw new Error('?');
} }
};
_read(n) {
switch (this._chunks--) {
case 0:
return this.push(null);
case 1:
return setTimeout(function() {
this.push('last chunk');
}.bind(this), 100);
case 2:
return this.push('second to last chunk');
case 3:
return process.nextTick(function() {
this.push('first chunk');
}.bind(this));
default:
throw new Error('?');
}
}
}
const ms = new MyStream(); const ms = new MyStream();
const results = []; const results = [];

View File

@ -24,20 +24,19 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
function MyWritable(fn, options) { class MyWritable extends stream.Writable {
stream.Writable.call(this, options); constructor(fn, options) {
this.fn = fn; super(options);
this.fn = fn;
}
_write(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
}
} }
util.inherits(MyWritable, stream.Writable);
MyWritable.prototype._write = function(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
};
(function defaultCondingIsUtf8() { (function defaultCondingIsUtf8() {
const m = new MyWritable(function(isBuffer, type, enc) { const m = new MyWritable(function(isBuffer, type, enc) {
assert.strictEqual(enc, 'utf8'); assert.strictEqual(enc, 'utf8');

View File

@ -24,20 +24,19 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
function MyWritable(fn, options) { class MyWritable extends stream.Writable {
stream.Writable.call(this, options); constructor(fn, options) {
this.fn = fn; super(options);
this.fn = fn;
}
_write(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
}
} }
util.inherits(MyWritable, stream.Writable);
MyWritable.prototype._write = function(chunk, encoding, callback) {
this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding);
callback();
};
{ {
const m = new MyWritable(function(isBuffer, type, enc) { const m = new MyWritable(function(isBuffer, type, enc) {
assert(isBuffer); assert(isBuffer);

View File

@ -3,19 +3,18 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
function MyWritable(options) { class MyWritable extends stream.Writable {
stream.Writable.call(this, options); constructor(opt) {
super(opt);
}
_write(chunk, encoding, callback) {
assert.notStrictEqual(chunk, null);
callback();
}
} }
util.inherits(MyWritable, stream.Writable);
MyWritable.prototype._write = function(chunk, encoding, callback) {
assert.notStrictEqual(chunk, null);
callback();
};
common.expectsError( common.expectsError(
() => { () => {
const m = new MyWritable({ objectMode: true }); const m = new MyWritable({ objectMode: true });

View File

@ -25,68 +25,67 @@ const common = require('../common');
const R = require('_stream_readable'); const R = require('_stream_readable');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
const EE = require('events').EventEmitter; const EE = require('events').EventEmitter;
function TestReader(n) { class TestReader extends R {
R.apply(this); constructor(n) {
this._buffer = Buffer.alloc(n || 100, 'x'); super();
this._pos = 0; this._buffer = Buffer.alloc(n || 100, 'x');
this._bufs = 10; this._pos = 0;
} this._bufs = 10;
util.inherits(TestReader, R);
TestReader.prototype._read = function(n) {
const max = this._buffer.length - this._pos;
n = Math.max(n, 0);
const toRead = Math.min(n, max);
if (toRead === 0) {
// simulate the read buffer filling up with some more bytes some time
// in the future.
setTimeout(function() {
this._pos = 0;
this._bufs -= 1;
if (this._bufs <= 0) {
// read them all!
if (!this.ended)
this.push(null);
} else {
// now we have more.
// kinda cheating by calling _read, but whatever,
// it's just fake anyway.
this._read(n);
}
}.bind(this), 10);
return;
} }
const ret = this._buffer.slice(this._pos, this._pos + toRead); _read(n) {
this._pos += toRead; const max = this._buffer.length - this._pos;
this.push(ret); n = Math.max(n, 0);
}; const toRead = Math.min(n, max);
if (toRead === 0) {
// simulate the read buffer filling up with some more bytes some time
// in the future.
setTimeout(() => {
this._pos = 0;
this._bufs -= 1;
if (this._bufs <= 0) {
// read them all!
if (!this.ended)
this.push(null);
} else {
// now we have more.
// kinda cheating by calling _read, but whatever,
// it's just fake anyway.
this._read(n);
}
}, 10);
return;
}
const ret = this._buffer.slice(this._pos, this._pos + toRead);
this._pos += toRead;
this.push(ret);
}
}
///// /////
function TestWriter() { class TestWriter extends EE {
EE.apply(this); constructor() {
this.received = []; super();
this.flush = false; this.received = [];
this.flush = false;
}
write(c) {
this.received.push(c.toString());
this.emit('write', c);
return true;
}
end(c) {
if (c) this.write(c);
this.emit('end', this.received);
}
} }
util.inherits(TestWriter, EE);
TestWriter.prototype.write = function(c) {
this.received.push(c.toString());
this.emit('write', c);
return true;
};
TestWriter.prototype.end = function(c) {
if (c) this.write(c);
this.emit('end', this.received);
};
{ {
// Test basic functionality // Test basic functionality
const r = new TestReader(20); const r = new TestReader(20);

View File

@ -25,26 +25,24 @@ const R = require('_stream_readable');
const W = require('_stream_writable'); const W = require('_stream_writable');
const assert = require('assert'); const assert = require('assert');
const util = require('util');
let ondataCalled = 0; let ondataCalled = 0;
function TestReader() { class TestReader extends R {
R.apply(this); constructor() {
this._buffer = Buffer.alloc(100, 'x'); super();
this._buffer = Buffer.alloc(100, 'x');
this.on('data', function() { this.on('data', () => {
ondataCalled++; ondataCalled++;
}); });
}
_read(n) {
this.push(this._buffer);
this._buffer = Buffer.alloc(0);
}
} }
util.inherits(TestReader, R);
TestReader.prototype._read = function(n) {
this.push(this._buffer);
this._buffer = Buffer.alloc(0);
};
const reader = new TestReader(); const reader = new TestReader();
setImmediate(function() { setImmediate(function() {
assert.strictEqual(ondataCalled, 1); assert.strictEqual(ondataCalled, 1);
@ -52,18 +50,18 @@ setImmediate(function() {
reader.push(null); reader.push(null);
}); });
function TestWriter() { class TestWriter extends W {
W.apply(this); constructor() {
this.write('foo'); super();
this.end(); this.write('foo');
this.end();
}
_write(chunk, enc, cb) {
cb();
}
} }
util.inherits(TestWriter, W);
TestWriter.prototype._write = function(chunk, enc, cb) {
cb();
};
const writer = new TestWriter(); const writer = new TestWriter();
process.on('exit', function() { process.on('exit', function() {

View File

@ -22,30 +22,21 @@
'use strict'; 'use strict';
require('../common'); require('../common');
const util = require('util');
const stream = require('stream'); const stream = require('stream');
class Read extends stream.Readable {
function Read() { _read(size) {
stream.Readable.call(this); this.push('x');
this.push(null);
}
} }
util.inherits(Read, stream.Readable);
Read.prototype._read = function(size) { class Write extends stream.Writable {
this.push('x'); _write(buffer, encoding, cb) {
this.push(null); this.emit('error', new Error('boom'));
}; this.emit('alldone');
}
function Write() {
stream.Writable.call(this);
} }
util.inherits(Write, stream.Writable);
Write.prototype._write = function(buffer, encoding, cb) {
this.emit('error', new Error('boom'));
this.emit('alldone');
};
const read = new Read(); const read = new Read();
const write = new Write(); const write = new Write();

View File

@ -23,40 +23,37 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const R = require('_stream_readable'); const R = require('_stream_readable');
const util = require('util');
util.inherits(TestReader, R); class TestReader extends R {
constructor(n, opts) {
super(opts);
this.pos = 0;
this.len = n || 100;
}
function TestReader(n, opts) { _read(n) {
R.call(this, opts); setTimeout(() => {
if (this.pos >= this.len) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}
this.pos = 0; n = Math.min(n, this.len - this.pos);
this.len = n || 100; if (n <= 0) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}
this.pos += n;
const ret = Buffer.alloc(n, 'a');
return this.push(ret);
}, 1);
}
} }
TestReader.prototype._read = function(n) {
setTimeout(function() {
if (this.pos >= this.len) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}
n = Math.min(n, this.len - this.pos);
if (n <= 0) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}
this.pos += n;
const ret = Buffer.alloc(n, 'a');
return this.push(ret);
}.bind(this), 1);
};
{ {
// Verify utf8 encoding // Verify utf8 encoding
const tr = new TestReader(100); const tr = new TestReader(100);

View File

@ -24,30 +24,27 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
function TestWriter() { class TestWriter extends stream.Writable {
stream.Writable.call(this); _write(buffer, encoding, callback) {
console.log('write called');
// super slow write stream (callback never called)
}
} }
util.inherits(TestWriter, stream.Writable);
TestWriter.prototype._write = function(buffer, encoding, callback) {
console.log('write called');
// super slow write stream (callback never called)
};
const dest = new TestWriter(); const dest = new TestWriter();
function TestReader(id) { class TestReader extends stream.Readable {
stream.Readable.call(this); constructor() {
this.reads = 0; super();
} this.reads = 0;
util.inherits(TestReader, stream.Readable); }
TestReader.prototype._read = function(size) { _read(size) {
this.reads += 1; this.reads += 1;
this.push(Buffer.alloc(size)); this.push(Buffer.alloc(size));
}; }
}
const src1 = new TestReader(); const src1 = new TestReader();
const src2 = new TestReader(); const src2 = new TestReader();

View File

@ -26,29 +26,27 @@ const stream = require('stream');
const chunk = Buffer.from('hallo'); const chunk = Buffer.from('hallo');
const util = require('util'); class TestWriter extends stream.Writable {
_write(buffer, encoding, callback) {
function TestWriter() { callback(null);
stream.Writable.call(this); }
} }
util.inherits(TestWriter, stream.Writable);
TestWriter.prototype._write = function(buffer, encoding, callback) {
callback(null);
};
const dest = new TestWriter(); const dest = new TestWriter();
// Set this high so that we'd trigger a nextTick warning // Set this high so that we'd trigger a nextTick warning
// and/or RangeError if we do maybeReadMore wrong. // and/or RangeError if we do maybeReadMore wrong.
function TestReader() { class TestReader extends stream.Readable {
stream.Readable.call(this, { highWaterMark: 0x10000 }); constructor() {
} super({
util.inherits(TestReader, stream.Readable); highWaterMark: 0x10000
});
}
TestReader.prototype._read = function(size) { _read(size) {
this.push(chunk); this.push(chunk);
}; }
}
const src = new TestReader(); const src = new TestReader();

View File

@ -26,24 +26,23 @@ const W = require('_stream_writable');
const D = require('_stream_duplex'); const D = require('_stream_duplex');
const assert = require('assert'); const assert = require('assert');
const util = require('util'); class TestWriter extends W {
util.inherits(TestWriter, W); constructor(opts) {
super(opts);
this.buffer = [];
this.written = 0;
}
function TestWriter() { _write(chunk, encoding, cb) {
W.apply(this, arguments); // simulate a small unpredictable latency
this.buffer = []; setTimeout(() => {
this.written = 0; this.buffer.push(chunk.toString());
this.written += chunk.length;
cb();
}, Math.floor(Math.random() * 10));
}
} }
TestWriter.prototype._write = function(chunk, encoding, 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));
};
const chunks = new Array(50); const chunks = new Array(50);
for (let i = 0; i < chunks.length; i++) { for (let i = 0; i < chunks.length; i++) {
chunks[i] = 'x'.repeat(i); chunks[i] = 'x'.repeat(i);

View File

@ -249,15 +249,16 @@ assert.strictEqual(util.format('abc%', 1), 'abc% 1');
// Errors // Errors
const err = new Error('foo'); const err = new Error('foo');
assert.strictEqual(util.format(err), err.stack); assert.strictEqual(util.format(err), err.stack);
function CustomError(msg) { class CustomError extends Error {
Error.call(this); constructor(msg) {
Object.defineProperty(this, 'message', super();
{ value: msg, enumerable: false }); Object.defineProperty(this, 'message',
Object.defineProperty(this, 'name', { value: msg, enumerable: false });
{ value: 'CustomError', enumerable: false }); Object.defineProperty(this, 'name',
Error.captureStackTrace(this, CustomError); { value: 'CustomError', enumerable: false });
Error.captureStackTrace(this, CustomError);
}
} }
util.inherits(CustomError, Error);
const customError = new CustomError('bar'); const customError = new CustomError('bar');
assert.strictEqual(util.format(customError), customError.stack); assert.strictEqual(util.format(customError), customError.stack);
// Doesn't capture stack trace // Doesn't capture stack trace

View File

@ -28,7 +28,6 @@ const assert = require('assert');
const tls = require('tls'); const tls = require('tls');
const fixtures = require('../common/fixtures'); const fixtures = require('../common/fixtures');
const stream = require('stream'); const stream = require('stream');
const util = require('util');
const request = Buffer.from('ABCD'.repeat(1024 * 256 - 1)); // 1mb const request = Buffer.from('ABCD'.repeat(1024 * 256 - 1)); // 1mb
@ -37,21 +36,22 @@ const options = {
cert: fixtures.readKey('agent1-cert.pem') cert: fixtures.readKey('agent1-cert.pem')
}; };
function Mediator() { class Mediator extends stream.Writable {
stream.Writable.call(this); constructor() {
this.buf = ''; super();
} this.buf = '';
util.inherits(Mediator, stream.Writable);
Mediator.prototype._write = function _write(data, enc, cb) {
this.buf += data;
setTimeout(cb, 0);
if (this.buf.length >= request.length) {
assert.strictEqual(this.buf, request.toString());
server.close();
} }
};
_write(data, enc, cb) {
this.buf += data;
setTimeout(cb, 0);
if (this.buf.length >= request.length) {
assert.strictEqual(this.buf, request.toString());
server.close();
}
}
}
const mediator = new Mediator(); const mediator = new Mediator();

View File

@ -34,28 +34,27 @@ const size = fs.statSync(file).size;
const expectLengths = [1024]; const expectLengths = [1024];
const util = require('util');
const Stream = require('stream'); const Stream = require('stream');
util.inherits(TestWriter, Stream); class TestWriter extends Stream {
constructor() {
super();
this.buffer = [];
this.length = 0;
}
function TestWriter() { write(c) {
Stream.apply(this); this.buffer.push(c.toString());
this.buffer = []; this.length += c.length;
this.length = 0; return true;
}
end(c) {
if (c) this.buffer.push(c.toString());
this.emit('results', this.buffer);
}
} }
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);
};
const r = new FSReadable(file); const r = new FSReadable(file);
const w = new TestWriter(); const w = new TestWriter();