From ae69291115e6f8641956346b094d4cbc0cdea134 Mon Sep 17 00:00:00 2001 From: leeight Date: Sun, 18 Nov 2018 20:26:16 +0800 Subject: [PATCH] test: update test case in test-net-internal.js Add test code for `makeSyncWrite`, which improve `test/parallel/test-net-internal.js` test coverage to 100% --- test/parallel/test-net-internal.js | 39 ++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-net-internal.js b/test/parallel/test-net-internal.js index 309b56d4d9aafc..57fb6ea6dc7dca 100644 --- a/test/parallel/test-net-internal.js +++ b/test/parallel/test-net-internal.js @@ -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)); @@ -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(); +}