Improvements for SNTP robustness and occasional test failures

Also added an unrelated test to improve coverage
Esse commit está contido em:
Karl Semich
2016-10-22 23:00:18 +00:00
commit cfb8071a9d
4 arquivos alterados com 110 adições e 17 exclusões
+3
Ver Arquivo
@@ -40,3 +40,6 @@ hardwareVoltageOutputAll.txt
# For git
*.orig
# Text editor temporary files
.*.sw* # vi/vim
+26 -15
Ver Arquivo
@@ -213,6 +213,12 @@ function OpenBCIFactory () {
if (this.options.sntpTimeSync) {
// establishing ntp connection
this.sntpStart()
.catch(ignored => {
// try again once after a delay
return new Promise((resolve, reject) => {
setTimeout(resolve, 500);
}).then(() => this.sntpStart());
})
.then(() => {
if (this.options.verbose) console.log('SNTP: connected');
})
@@ -2149,10 +2155,16 @@ function OpenBCIFactory () {
* @author AJ Keller (@pushtheworldllc)
*/
OpenBCIBoard.prototype.sntpGetOffset = function () {
this.options.sntpTimeSync = true;
return new Promise((resolve, reject) => {
Sntp.offset(function (err, offset) {
Sntp.offset({
host: this.options.sntpTimeSyncHost, // Defaults to pool.ntp.org
port: this.options.sntpTimeSyncPort, // Defaults to 123 (NTP)
clockSyncRefresh: 30 * 60 * 1000 // Resync every 30 minutes
}, function (err, offset) {
if (err) reject(err);
resolve(offset);
else resolve(offset);
});
});
};
@@ -2175,20 +2187,19 @@ function OpenBCIFactory () {
*/
OpenBCIBoard.prototype.sntpStart = function (options) {
return new Promise((resolve, reject) => {
this.options.sntpTimeSync = true;
Sntp.start({
host: this.options.sntpTimeSyncHost, // Defaults to pool.ntp.org
port: this.options.sntpTimeSyncPort, // Defaults to 123 (NTP)
clockSyncRefresh: 30 * 60 * 1000 // Resync every 30 minutes
}, err => {
if (err) {
this.sync.sntpActive = false;
reject(err);
} else {
this.sync.sntpActive = true;
resolve();
this.sntpGetOffset().then(() => {
Sntp.start({
host: this.options.sntpTimeSyncHost, // Defaults to pool.ntp.org
port: this.options.sntpTimeSyncPort, // Defaults to 123 (NTP)
clockSyncRefresh: 30 * 60 * 1000 // Resync every 30 minutes
}, () => {
this.emit('sntpTimeLock');
}
});
this.sync.sntpActive = true;
resolve();
}, (err) => {
this.sync.sntpActive = false;
reject(err);
});
});
};
+52 -2
Ver Arquivo
@@ -242,15 +242,36 @@ describe('openbci-sdk', function () {
expect(ourBoard2.options.simulatorSerialPortFailure).to.be.true;
});
it('should be able to enter sync mode', function () {
var ourBoard1 = new openBCIBoard.OpenBCIBoard({
var ourBoard1, ourBoard2;
ourBoard1 = new openBCIBoard.OpenBCIBoard({
sntpTimeSync: true
});
expect(ourBoard1.options.sntpTimeSync).to.be.true;
// Verify multi case support
var ourBoard2 = new openBCIBoard.OpenBCIBoard({
ourBoard2 = new openBCIBoard.OpenBCIBoard({
sntptimesync: true
});
expect(ourBoard2.options.sntpTimeSync).to.be.true;
return Promise.all([
new Promise((resolve, reject) => {
ourBoard1.once('sntpTimeLock', resolve);
ourBoard1.once('error', reject);
}),
new Promise((resolve, reject) => {
ourBoard2.once('sntpTimeLock', resolve);
ourBoard2.once('error', reject);
})
]).then(() => {
ourBoard1.sntpStop();
ourBoard2.sntpStop();
}, err => {
ourBoard1.sntpStop();
ourBoard2.sntpStop();
return Promise.reject(err);
});
});
it('should be able to change the ntp pool host', function () {
var expectedPoolName = 'time.apple.com';
@@ -276,6 +297,19 @@ describe('openbci-sdk', function () {
});
expect(ourBoard2.options.sntpTimeSyncPort).to.equal(expectedPortNumber);
});
it('should report when sntp fails', function (done) {
var ourBoard = new openBCIBoard.OpenBCIBoard({
sntpTimeSync: true,
sntpTimeSyncHost: 'no\'where'
});
ourBoard.once('error', () => {
done();
});
ourBoard.once('sntpTimeLock', () => {
ourBoard.sntpStop();
done('got a time lock with nowhere');
});
});
it('can enter verbose mode', function () {
ourBoard = new openBCIBoard.OpenBCIBoard({
verbose: true
@@ -1418,6 +1452,7 @@ describe('openbci-sdk', function () {
});
describe('#time', function () {
this.timeout(5000);
it('should use sntp time when sntpTimeSync specified in options', function () {
var board = new openBCIBoard.OpenBCIBoard({
verbose: true,
@@ -1463,6 +1498,7 @@ describe('openbci-sdk', function () {
after(() => {
board.sntpStop();
});
this.timeout(5000);
it('should be able to start ntp server', done => {
expect(board.sntp.isLive()).to.be.false;
board.sntpStart()
@@ -1475,6 +1511,7 @@ describe('openbci-sdk', function () {
});
});
describe('#sntpStop', function () {
this.timeout(5000);
var board;
before(done => {
board = new openBCIBoard.OpenBCIBoard({
@@ -1502,6 +1539,19 @@ describe('openbci-sdk', function () {
expect(board.sntp.isLive()).to.be.false;
});
});
describe('#sntpGetOffset', function () {
it('should get the sntp offset', function (done) {
var board = new openBCIBoard.OpenBCIBoard({
sntpTimeSync: true
});
board.once('sntpTimeLock', () => {
board.sntpGetOffset().then(offset => {
board.sntpStop();
done();
}, done);
});
});
});
describe('#_processParseBufferForReset', function () {
var ourBoard;
+29
Ver Arquivo
@@ -122,6 +122,35 @@ describe('openBCISimulator', function () {
} catch (e) { done(); }
});
});
describe('#write', function () {
it('should refuse to write when not connected', function (done) {
var simulator = new openBCISimulator.OpenBCISimulator(k.OBCISimulatorPortName);
try {
simulator.write('q');
done('did not throw on disconnected write');
} catch (e) {
simulator.write('q', err => {
if (err) {
done();
} else {
done('did not provide error on disconnected write');
}
});
}
});
});
describe('#close', function () {
it('should provide an error closing when already closed', function (done) {
var simulator = new openBCISimulator.OpenBCISimulator(k.OBCISimulatorPortName);
simulator.close(err => {
if (err) {
done();
} else {
done('closed successfully but had no time to open');
}
});
});
});
describe(`_startStream`, function () {
it('should return a packet with sample data in it', function (done) {
var simulator = new openBCISimulator.OpenBCISimulator(k.OBCISimulatorPortName);