mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
Merge pull request #713 from voodootikigod/open-close-open-close
Closing a port is better tested & correctly reports isOpen in it’s event
This commit is contained in:
commit
38d9ca91ce
7 changed files with 94 additions and 51 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue