From 77851e2e75f8a790e292d1b583b21e5e63c7dd56 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Thu, 31 Mar 2016 12:11:31 -0400 Subject: [PATCH] =?UTF-8?q?Closing=20a=20port=20is=20better=20tested=20&?= =?UTF-8?q?=20correctly=20reports=20isOpen=20in=20it=E2=80=99s=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ports correctly respond to `isOpen` during a close event - cleanup some assertions - if a CI has a port to test with, open and close it Bonus - Appveyor seems to think if you have a `&` in your commit message you’re trying to run a command --- .travis.yml | 2 + README.md | 4 +- appveyor.yml | 8 ++-- serialport.js | 16 +++---- test/mocks/darwin-hardware.js | 3 ++ test/serialport-basic.js | 87 ++++++++++++++++++++-------------- test/serialport-integration.js | 25 +++++++++- 7 files changed, 94 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3dcf50b..7fe6073 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,6 +53,8 @@ before_install: - $CXX --version # get commit message - COMMIT_MESSAGE=$(git show -s --format=%B $TRAVIS_COMMIT | tr -d '\n') +# Set a test serialport for integration testing when we run our tests if we have one +- if [[ $TRAVIS_OS_NAME == "osx" ]]; then export TEST_PORT='/dev/cu.serial1'; fi # figure out if we should publish - PUBLISH_BINARY=false # if we are building a tag then publish diff --git a/README.md b/README.md index b9fc240..06ef8c1 100644 --- a/README.md +++ b/README.md @@ -344,7 +344,7 @@ Closes an open connection. **_callback (optional)_** -Called once a connection is closed. Closing a connection will also remove all event listeners. The callback should be a function that looks like: `function (error) { ... }` +Called once a connection is closed. Closing a connection will also remove all event listeners. The callback should be a function that looks like: `function (error) { ... }` If called without an callback and there is an error, an error event will be emitted. ## Events @@ -355,7 +355,7 @@ Callback is called with no arguments when the port is opened and ready for writi Callback is called with data depending on your chosen parser. The default `raw` parser will have a `Buffer` object with a varying amount of data in it. The `readLine` parser will provide a string of your line. See the [parsers](#parsers) section for more information ### .on('close', callback) -Callback is called with no arguments when the port is closed. +Callback is called with no arguments when the port is closed. In the event of an error, an error event will be triggered ### .on('error', callback) Callback is called with an error object whenever there is an error. diff --git a/appveyor.yml b/appveyor.yml index 839c8bf..ab55b4a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -23,9 +23,9 @@ platform: - x64 install: - - cmd: ECHO %nodejs_version% - - cmd: ECHO %platform% - - cmd: ECHO %APPVEYOR_REPO_COMMIT_MESSAGE% + - cmd: ECHO "%nodejs_version%" + - cmd: ECHO "%platform%" + - cmd: ECHO "%APPVEYOR_REPO_COMMIT_MESSAGE%" - cmd: SET PATH=%cd%\node_modules\.bin\;%PATH% - ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) $env:platform @@ -45,6 +45,8 @@ install: # Or look for commit message containing `[publish binary]` - cmd: IF not x%COMMIT_MSG:[publish binary]=%==x%COMMIT_MSG% SET PUBLISH_BINARY=true - cmd: ECHO %PUBLISH_BINARY% + # Set a serialport for us to play with + - cmd: SET TEST_PORT=COM1 build_script: - cmd: npm install --build-from-source --msvs_version=2013 diff --git a/serialport.js b/serialport.js index 76ba773..4a2a038 100644 --- a/serialport.js +++ b/serialport.js @@ -448,6 +448,7 @@ function SerialPortFactory(_spfOptions) { var fd = self.fd; if (self.closing) { + // TODO this should be an error return; } if (!this.isOpen()) { @@ -480,21 +481,18 @@ function SerialPortFactory(_spfOptions) { return; } - self.emit('close'); - self.removeAllListeners(); self.closing = false; self.fd = null; - - if (callback) { - callback(); - } + self.emit('close'); + if (callback) { callback() } + self.removeAllListeners(); }); - } catch (ex) { + } catch (err) { self.closing = false; if (callback) { - callback(ex); + callback(err); } else { - self.emit('error', ex); + self.emit('error', err); } } }; diff --git a/test/mocks/darwin-hardware.js b/test/mocks/darwin-hardware.js index 9e5f489..ddf8c5c 100644 --- a/test/mocks/darwin-hardware.js +++ b/test/mocks/darwin-hardware.js @@ -128,6 +128,9 @@ Hardware.prototype.close = function (fd, cb) { if (!port) { return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); } + if (!port.open) { + return cb(new Error(fd + ' fd is already closed')); + } port.open = false; cb && cb(null); }; diff --git a/test/serialport-basic.js b/test/serialport-basic.js index a7d8c72..2e09ca1 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -2,6 +2,7 @@ var sinon = require('sinon'); var chai = require('chai'); +var assert = chai.assert; var expect = chai.expect; var MockedSerialPort = require('./mocks/darwin-hardware'); @@ -39,7 +40,7 @@ describe('SerialPort', function () { it('emits an error on the factory when erroring without a callback', function (done) { // finish the test on error MockedSerialPort.once('error', function (err) { - chai.assert.isDefined(err, 'didn\'t get an error'); + assert.instanceOf(err, Error); done(); }); @@ -50,14 +51,14 @@ describe('SerialPort', function () { var port = new SerialPort('/dev/johnJacobJingleheimerSchmidt'); port.once('error', function(err) { - chai.assert.isDefined(err); + assert.instanceOf(err, Error); done(); }); }); it('errors with invalid databits', function (done) { var errorCallback = function (err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }; @@ -66,7 +67,7 @@ describe('SerialPort', function () { it('errors with invalid stopbits', function (done) { var errorCallback = function (err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }; @@ -75,7 +76,7 @@ describe('SerialPort', function () { it('errors with invalid parity', function (done) { var errorCallback = function (err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }; @@ -84,7 +85,7 @@ describe('SerialPort', function () { it('errors with invalid flow control', function (done) { var errorCallback = function (err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }; @@ -104,7 +105,7 @@ describe('SerialPort', function () { var cb = function () {}; var port = new SerialPort('/dev/exists', false, cb); port.write(null, function(err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }); }); @@ -113,7 +114,7 @@ describe('SerialPort', function () { var cb = function () {}; var port = new SerialPort('/dev/exists', false, cb); port.close(function(err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }); }); @@ -122,7 +123,7 @@ describe('SerialPort', function () { var cb = function () {}; var port = new SerialPort('/dev/exists', false, cb); port.flush(function(err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }); }); @@ -131,7 +132,7 @@ describe('SerialPort', function () { var cb = function () {}; var port = new SerialPort('/dev/exists', false, cb); port.set({}, function(err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }); }); @@ -140,7 +141,7 @@ describe('SerialPort', function () { var cb = function () {}; var port = new SerialPort('/dev/exists', false, cb); port.drain(function(err) { - chai.assert.isDefined(err, 'err is not defined'); + assert.instanceOf(err, Error); done(); }); }); @@ -252,17 +253,50 @@ describe('SerialPort', function () { }); describe('#close', function () { - it('fires a close event when it\'s closed', function (done) { + it('emits a close event', function (done) { var port = new SerialPort('/dev/exists', function () { - var closeSpy = sandbox.spy(); - port.on('close', closeSpy); + port.on('close', function() { + assert.isFalse(port.isOpen()); + done(); + }); port.close(); - expect(closeSpy.calledOnce); - expect(port.isOpen()).to.be.false; - done(); }); }); + it('has a close callback', function (done) { + var port = new SerialPort('/dev/exists', function () { + port.close(function() { + assert.isFalse(port.isOpen()); + done(); + }); + }); + }); + + it('emits the close event and runs the callback', function (done) { + var called = 0; + var doneIfTwice = function() { + called++; + if (called === 2) { return done() } + }; + var port = new SerialPort('/dev/exists', function() { + port.close(doneIfTwice); + }); + port.on('close', doneIfTwice); + }); + + it('emits an error event or error callback but not both', function(done) { + var port = new SerialPort('/dev/exists', false); + var called = 0; + var doneIfTwice = function(err) { + assert.instanceOf(err, Error); + called++; + if (called === 2) { return done() } + }; + port.on('error', doneIfTwice); + port.close(); + port.close(doneIfTwice); + }); + it('fires a close event after being reopened', function (done) { var port = new SerialPort('/dev/exists', function () { var closeSpy = sandbox.spy(); @@ -274,23 +308,6 @@ describe('SerialPort', function () { done(); }); }); - - it('emits a close event', function (done) { - var port = new SerialPort('/dev/exists', function () { - port.on('close', done); - port.close(); - }); - }); - - it('should consider 0 a valid fd', function(done) { - var port = new SerialPort('/dev/exists', function() { - expect(port.fd).to.equal(0); - port.close(function () { - expect(port.fd).to.be.null; - done(); - }); - }); - }); }); describe('disconnect', function () { @@ -315,7 +332,7 @@ describe('SerialPort', function () { it('emits a disconnect event', function (done) { var port = new SerialPort('/dev/exists', function () { port.on('disconnect', function(err) { - expect(err).to.be.ok; + assert.instanceOf(err, Error); done(); }); hardware.disconnect('/dev/exists'); diff --git a/test/serialport-integration.js b/test/serialport-integration.js index 75c129e..0d085d2 100644 --- a/test/serialport-integration.js +++ b/test/serialport-integration.js @@ -1,6 +1,6 @@ 'use strict'; -var chai = require('chai'); +var assert = require('chai').assert; var serialPort = require('../serialport'); var SerialPort = serialPort.SerialPort; @@ -8,7 +8,7 @@ describe('SerialPort', function () { describe('Initialization', function () { it('Throws an error in callback when trying to open an invalid port', function (done) { this.port = new SerialPort('/dev/nullbad', function (err) { - chai.assert.isDefined(err); + assert.isDefined(err); done(); }); }); @@ -16,4 +16,25 @@ describe('SerialPort', function () { it('.list', function(done) { serialPort.list(done); }); + + describe('Working with virtual ports', function() { + var testPort = process.env.TEST_PORT; + + if (!testPort) { + it('Cannot be tested as we have no test ports'); + return; + } + + it('can open and close ports', function(done) { + var port = new SerialPort(testPort); + port.on('open', function() { + assert.isTrue(port.isOpen()); + port.close(); + }); + port.on('close', function() { + assert.isFalse(port.isOpen()); + done(); + }); + }); + }); });