Skip to content

Commit

Permalink
test: update test case in test-net-internal.js
Browse files Browse the repository at this point in the history
Add test code for `makeSyncWrite`, which improve
`test/parallel/test-net-internal.js` test coverage to 100%
  • Loading branch information
leeight committed Nov 23, 2018
1 parent d45e303 commit ae69291
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions test/parallel/test-net-internal.js
Expand Up @@ -2,9 +2,9 @@

// Flags: --expose-internals

require('../common');
const common = require('../common');
const assert = require('assert');
const isLegalPort = require('internal/net').isLegalPort;
const { isLegalPort, makeSyncWrite } = require('internal/net');

for (let n = 0; n <= 0xFFFF; n++) {
assert(isLegalPort(n));
Expand All @@ -18,3 +18,38 @@ const bad = [-1, 'a', {}, [], false, true, 0xFFFF + 1, Infinity,
-Infinity, NaN, undefined, null, '', ' ', 1.1, '0x',
'-0x1', '-0o1', '-0b1', '0o', '0b'];
bad.forEach((i) => assert(!isLegalPort(i)));


function writeToStdout() {
const ctx = {
_handle: {
bytesWritten: 0
}
};
const syncWrite = makeSyncWrite(process.stdout.fd);
syncWrite.call(ctx, 'hello', 'utf-8', common.mustCall((ex) => {
assert.strictEqual(undefined, ex);
}));
syncWrite.call(ctx, Buffer.from('world'), 'buffer', common.mustCall((ex) => {
assert.strictEqual(undefined, ex);
}));
assert.strictEqual(ctx._handle.bytesWritten, 10);
}
function writeToInvalidFD() {
const ctx = {
_handle: {
bytesWritten: 0
}
};
const invalidFD = -1;
const syncWrite = makeSyncWrite(invalidFD);
syncWrite.call(ctx, Buffer.from('world'), 'buffer', common.mustCall((ex) => {
assert.strictEqual(ex.code, 'EBADF');
}));
assert.strictEqual(ctx._handle.bytesWritten, 5);
}
if (!common.isWindows) {
// This test will crash on windows, so skip it
writeToStdout();
writeToInvalidFD();
}

0 comments on commit ae69291

Please sign in to comment.