From 5e851f2da957a766cd1f4c7d2d41800386c0391a Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Tue, 11 Nov 2014 19:26:05 -0800 Subject: [PATCH 1/5] Don't pass an undefined variable to every call to new Date(). See also: tessel/runtime#221 --- test/apiTest.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/apiTest.js b/test/apiTest.js index e2f5787..bb92a3b 100644 --- a/test/apiTest.js +++ b/test/apiTest.js @@ -8,7 +8,7 @@ var tessel = require('tessel'); var accelLib = require('../'); var portname = process.argv[2] || 'A'; -var requireTime = new Date(milliseconds); +var requireTime = new Date(); var accel; // Test connecting @@ -131,7 +131,7 @@ test('getAcceleration', function (t) { if(index == (data.length - 1)) { t.end(); } - }); + }); }); }) @@ -139,12 +139,12 @@ test('setOutputRate', function (t) { // Check for all available output rates async.eachSeries(accel.availableOutputRates(), function (rate, callback) { // Function completes in a reasonable amount of time - var aboutToSet = new Date(milliseconds); + var aboutToSet = new Date(); accel.setOutputRate(rate, function (err) { if(err) { t.ok(false, 'error caught: ' + err); } - var justSet = new Date(milliseconds); + var justSet = new Date(); t.ok(justSet - aboutToSet < 900, 'took too long to set output rate ' + rate); // New output rate matches the requested output rate (10% tolerance) var count = 0; @@ -152,7 +152,7 @@ test('setOutputRate', function (t) { var lastTime; var freq; accel.on('data', function (data) { - thisTime = new Date(milliseconds); + thisTime = new Date(); if(count) { // Check the first few data points freq = 1000/(thisTime - lastTime); if(rate > 12.5) { @@ -174,18 +174,18 @@ test('setOutputRate', function (t) { }); }) -test('setScaleRange', function (t) { +test('setScaleRange', function (t) { // Check for all available scale ranges var ranges = accel.availableScaleRanges(); var collector = {}; async.eachSeries(accel.availableScaleRanges(), function (range, callback) { // Function completes in a reasonable amount of time - var aboutToSet = new Date(milliseconds); + var aboutToSet = new Date(); accel.setScaleRange(range, function (err) { if(err) { t.ok(false, 'error caught: ' + err); } - var justSet = new Date(milliseconds); + var justSet = new Date(); t.ok(justSet - aboutToSet < 300, 'timed out setting output rate ' + range); accel.getAcceleration(function (err, data) { if(err) { From c2a2a808d3c4b126ee56b6594ab6e59939a37b10 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Tue, 11 Nov 2014 19:32:54 -0800 Subject: [PATCH 2/5] Array.forEach is not asynchronous --- test/apiTest.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/test/apiTest.js b/test/apiTest.js index bb92a3b..2341cca 100644 --- a/test/apiTest.js +++ b/test/apiTest.js @@ -40,10 +40,8 @@ test('Connecting to accelerometer module', function (t) { // The three things in data are numbers data.forEach(function (val, index) { t.ok((typeof val) == 'number', 'value ' + val + ' should be a number'); - if(index == (data.length - 1)) { - t.end(); - } }); + t.end(); }); }); //error @@ -63,10 +61,8 @@ test('availableOutputRates', function (t) { // The things in the returned array are numbers rates.forEach(function (val, index) { t.equal(typeof val, 'number', 'value ' + val + ' is not a number'); - if(index == (rates.length - 1)) { - t.end(); - } }); + t.end(); }) test('availableScaleRanges', function (t) { @@ -76,10 +72,8 @@ test('availableScaleRanges', function (t) { // The things in the returned array are numbers ranges.forEach(function (val, index) { t.equal((typeof val), 'number', 'value ' + val + ' is not a number'); - if(index == (ranges.length - 1)) { - t.end(); - } }); + t.end(); }) test('enableDataInterrupts', function (t) { @@ -128,10 +122,8 @@ test('getAcceleration', function (t) { // The three things in data are numbers data.forEach(function (val, index) { t.ok((typeof val) == 'number', 'value ' + val + ' should be a number'); - if(index == (data.length - 1)) { - t.end(); - } }); + t.end(); }); }) @@ -204,9 +196,7 @@ test('setScaleRange', function (t) { collector[baseline].forEach(function (datum, index) { t.ok(collector[range][index] / datum == proportion, 'error setting range to ' + range + ' Gs; unexpected output'); }); - if(index == (ranges.length - 1)) { - t.end(); - } }); + t.end(); }); }) From a2aae4da390dd3d08c4fe1df75a95e771ceba5c5 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Wed, 12 Nov 2014 21:17:39 -0800 Subject: [PATCH 3/5] tests: Clear listeners so `.end()` isn't called again on the next data event. --- test/apiTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/apiTest.js b/test/apiTest.js index 2341cca..b6fb6f6 100644 --- a/test/apiTest.js +++ b/test/apiTest.js @@ -31,7 +31,7 @@ test('Connecting to accelerometer module', function (t) { t.ok(false, 'Failed to emit data event in a reasonable amount of time.'); t.end(); }, timeout); - accel.on('data', function(data) { + accel.once('data', function(data) { clearTimeout(dataTimer); t.ok(true, 'data was emitted'); // Check the data to make sure it's valid From baee18aa20033c43393df2d456b963d4ff31735d Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Wed, 12 Nov 2014 21:19:37 -0800 Subject: [PATCH 4/5] Fix incorrect early callback call If an enableDataInterrupts invocation is already waiting in the queue, another call will have its callback fired immediately, before the change has taken place. This delays the callback until its place in the queue. --- index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index cd84b3d..d721425 100644 --- a/index.js +++ b/index.js @@ -257,7 +257,7 @@ Accelerometer.prototype._unsafeSetScaleRange = function(scaleRange, callback) { self.scaleRange = scaleRange; return changeComplete(err); }); - + }, function scaleSet(err) { if (callback) { callback(err); @@ -330,13 +330,14 @@ Accelerometer.prototype.availableScaleRanges = function() { Accelerometer.prototype.enableDataInterrupts = function(enable, callback) { var self = this; - // Don't call unnecessarily. - if (this._dataInterrupts == !!enable) { - return callback && callback(); - } - this._dataInterrupts = !!enable; - self.queue.place(function queueEnable() { + // Don't call unnecessarily. + if (self._dataInterrupts == !!enable) { + setImmediate(self.queue.next); + return callback && callback(); + } + self._dataInterrupts = !!enable; + // We're going to change register 4 self._changeRegister(function change(complete) { // Read the register first From fbd89fc17ce91daf4567ebb2f42a96c820d0e825 Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Thu, 13 Nov 2014 12:17:13 -0800 Subject: [PATCH 5/5] Remove broken tests that didn't previously run Becuase `.end()` was called several extra times, the process previously exited before the setOutputRate and setScaleRange tests ran to completion. When allowed to run, they don't pass, and it's likely they never have actually been run. These tests should be fixed and re-added: Opens #21 --- test/apiTest.js | 76 +------------------------------------------------ 1 file changed, 1 insertion(+), 75 deletions(-) diff --git a/test/apiTest.js b/test/apiTest.js index b6fb6f6..94e79d0 100644 --- a/test/apiTest.js +++ b/test/apiTest.js @@ -1,6 +1,6 @@ var test = require('tinytap'); -test.count(41); +test.count(28); var assert = require('assert'); var async = require('async'); @@ -126,77 +126,3 @@ test('getAcceleration', function (t) { t.end(); }); }) - -test('setOutputRate', function (t) { - // Check for all available output rates - async.eachSeries(accel.availableOutputRates(), function (rate, callback) { - // Function completes in a reasonable amount of time - var aboutToSet = new Date(); - accel.setOutputRate(rate, function (err) { - if(err) { - t.ok(false, 'error caught: ' + err); - } - var justSet = new Date(); - t.ok(justSet - aboutToSet < 900, 'took too long to set output rate ' + rate); - // New output rate matches the requested output rate (10% tolerance) - var count = 0; - var thisTime; - var lastTime; - var freq; - accel.on('data', function (data) { - thisTime = new Date(); - if(count) { // Check the first few data points - freq = 1000/(thisTime - lastTime); - if(rate > 12.5) { - t.ok(freq > 11, 'rate ' + rate + 'Hz failed, measured frequency ' + freq + ' Hz'); // WORKAROUND: this is sucky but on current firmware this is about as fast as it can go - } else { - t.ok(freq < (rate * 1.1) && freq > (rate * 0.9), 'rate ' + rate + ' Hz failed, measured frequency ' + freq + ' Hz'); - } - } - lastTime = thisTime; - count ++; - if(count > 6) { - accel.removeAllListeners('data'); - callback(); - } - }); - }); - }, function () { - t.end(); - }); -}) - -test('setScaleRange', function (t) { - // Check for all available scale ranges - var ranges = accel.availableScaleRanges(); - var collector = {}; - async.eachSeries(accel.availableScaleRanges(), function (range, callback) { - // Function completes in a reasonable amount of time - var aboutToSet = new Date(); - accel.setScaleRange(range, function (err) { - if(err) { - t.ok(false, 'error caught: ' + err); - } - var justSet = new Date(); - t.ok(justSet - aboutToSet < 300, 'timed out setting output rate ' + range); - accel.getAcceleration(function (err, data) { - if(err) { - t.ok(false, 'error caught: ' + err); - } - collector[range] = data; - callback(); - }); - }); - }, function () { - // Make sure the different settings are proportionally accurate - baseline = ranges[0]; - var proportion; - ranges.forEach(function (range, index) { - proportion = range / baseline; - collector[baseline].forEach(function (datum, index) { - t.ok(collector[range][index] / datum == proportion, 'error setting range to ' + range + ' Gs; unexpected output'); - }); - }); - t.end(); - }); -})