node/test/parallel/test-net-socket-setnodelay.js
Robert Nagy eeccd52b4e net: make readable/writable start as true
`net.Socket` is slightly breaking stream invariants by
having readable/writable going from `false` to `true`.
Streams assume that readable/writable starts out `true`
and then goes to `false` through `push(null)`/`end()`
after which it never goes back to `true`, e.g. once a
stream is `writable == false` it is assumed it will
never become `true`.

This PR changes 2 things:

Unless explicitly set to `false` through options:

- starts as `readable`/`writable` `true` by default.
- uses `push(null)`/`end()` to set `readable`/`writable`
  to `false`. Note that this would cause the socket to
  emit the `'end'`/`'finish'` events, which it did not
  do previously.

In the case it is explicitly set to `false` through
options` it is assumed to never become `true`.

PR-URL: https://github.com/nodejs/node/pull/32272
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-03-24 09:45:25 +01:00

57 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const truthyValues = [true, 1, 'true', {}, []];
const falseyValues = [false, 0, ''];
const genSetNoDelay = (desiredArg) => (enable) => {
assert.strictEqual(enable, desiredArg);
};
// setNoDelay should default to true
let socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(true)),
readStart() {}
}
});
socket.setNoDelay();
socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(true), 1),
readStart() {}
}
});
truthyValues.forEach((testVal) => socket.setNoDelay(testVal));
socket = new net.Socket({
handle: {
setNoDelay: common.mustNotCall(),
readStart() {}
}
});
falseyValues.forEach((testVal) => socket.setNoDelay(testVal));
socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(() => {}, 3),
readStart() {}
}
});
truthyValues.concat(falseyValues).concat(truthyValues)
.forEach((testVal) => socket.setNoDelay(testVal));
// If a handler doesn't have a setNoDelay function it shouldn't be called.
// In the case below, if it is called an exception will be thrown
socket = new net.Socket({
handle: {
setNoDelay: null,
readStart() {}
}
});
const returned = socket.setNoDelay(true);
assert.ok(returned instanceof net.Socket);