Skip to content

Commit

Permalink
test: refactor stream-*-constructor-set-methods
Browse files Browse the repository at this point in the history
- Use `common.mustCall()` to ensure that callbacks are called.
- Remove no longer needed variables.
- Remove unnecessary `process.on('exit')` usage.

PR-URL: nodejs#18817
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
lpinca authored and BridgeAR committed Feb 17, 2018
1 parent 8123232 commit 33103e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
28 changes: 13 additions & 15 deletions test/parallel/test-stream-transform-constructor-set-methods.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const Transform = require('stream').Transform;
const { strictEqual } = require('assert');
const { Transform } = require('stream');

const _transform = common.mustCall(function _transform(d, e, n) {
n();
const _transform = common.mustCall((chunk, _, next) => {
next();
});

const _final = common.mustCall(function _final(n) {
n();
const _final = common.mustCall((next) => {
next();
});

const _flush = common.mustCall(function _flush(n) {
n();
const _flush = common.mustCall((next) => {
next();
});

const t = new Transform({
Expand All @@ -22,21 +22,19 @@ const t = new Transform({
final: _final
});

const t2 = new Transform({});
strictEqual(t._transform, _transform);
strictEqual(t._flush, _flush);
strictEqual(t._final, _final);

t.end(Buffer.from('blerg'));
t.resume();

const t2 = new Transform({});

common.expectsError(() => {
t2.end(Buffer.from('blerg'));
}, {
type: Error,
code: 'ERR_METHOD_NOT_IMPLEMENTED',
message: 'The _transform method is not implemented'
});

process.on('exit', () => {
assert.strictEqual(t._transform, _transform);
assert.strictEqual(t._flush, _flush);
assert.strictEqual(t._final, _final);
});
52 changes: 22 additions & 30 deletions test/parallel/test-stream-writable-constructor-set-methods.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const Writable = require('stream').Writable;
const { strictEqual } = require('assert');
const { Writable } = require('stream');

let _writeCalled = false;
function _write(d, e, n) {
_writeCalled = true;
}
const _write = common.mustCall((chunk, _, next) => {
next();
});

const _writev = common.mustCall((chunks, next) => {
strictEqual(chunks.length, 2);
next();
});

const w = new Writable({ write: _write });
w.end(Buffer.from('blerg'));
const w = new Writable({ write: _write, writev: _writev });

let _writevCalled = false;
let dLength = 0;
function _writev(d, n) {
dLength = d.length;
_writevCalled = true;
}
strictEqual(w._write, _write);
strictEqual(w._writev, _writev);

const w2 = new Writable({ writev: _writev });
w2.cork();
w.write(Buffer.from('blerg'));

w2.write(Buffer.from('blerg'));
w2.write(Buffer.from('blerg'));
w2.end();
w.cork();
w.write(Buffer.from('blerg'));
w.write(Buffer.from('blerg'));

const w3 = new Writable();
w.end();

w3.on('error', common.expectsError({
const w2 = new Writable();

w2.on('error', common.expectsError({
type: Error,
code: 'ERR_METHOD_NOT_IMPLEMENTED',
message: 'The _write method is not implemented'
}));

w3.end(Buffer.from('blerg'));

process.on('exit', function() {
assert.strictEqual(w._write, _write);
assert(_writeCalled);
assert.strictEqual(w2._writev, _writev);
assert.strictEqual(dLength, 2);
assert(_writevCalled);
});
w2.end(Buffer.from('blerg'));

0 comments on commit 33103e9

Please sign in to comment.