Skip to content

Commit

Permalink
Merge pull request #103 from prey/1.3.8-rc
Browse files Browse the repository at this point in the history
1.3.8-rc
  • Loading branch information
mauricioschneider committed Mar 20, 2015
2 parents 5df316d + 3228b6e commit 32acf00
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 26 deletions.
14 changes: 10 additions & 4 deletions lib/agent/plugins/control-panel/interval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var parse_cmd = function(str) {
}
};

var request = function() {
var request = function(re_schedule) {
if (requesting)
return;

Expand All @@ -32,6 +32,11 @@ var request = function() {
getter.commands(function(err, resp) {
requesting = false;

if(re_schedule) {
logger.debug('Re-scheduling request.');
schedule_request();
}

if (err)
return hooks.trigger('error', err, 'interval');
else if (resp.statusCode != 200)
Expand All @@ -47,7 +52,6 @@ var request = function() {
if (cmd) emitter.emit('command', cmd);
});

schedule_request();
});
};

Expand All @@ -65,14 +69,16 @@ var set_interval = function(delay) {

logger.info('Queuing check-ins every ' + delay/60000 + ' minutes.');
if (timer) {
logger.debug('Clearing previous request schedule.');
clearTimeout(timer);
schedule_request();
}
};

var schedule_request = function (custom_delay) {
var delay = custom_delay || current_delay;
timer = setTimeout(request, delay);
var delay = custom_delay || current_delay,
re_schedule = true;
timer = setTimeout(request, delay, re_schedule);
};

var load_hooks = function() {
Expand Down
104 changes: 93 additions & 11 deletions lib/agent/plugins/control-panel/interval/test/interval_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var assert = require("assert"),
describe('interval', function () {

var setTimeout_spy = {},
clearTimeout_spy = {};
clearTimeout_spy = {},
request_stub = {};

var set_spies = function () {
setTimeout_spy = sinon.spy(global, "setTimeout");
Expand All @@ -32,6 +33,20 @@ describe('interval', function () {
clearTimeout_spy.reset();
};

var set_request_stub = function () {
request_stub = sinon.stub(api.devices.get, 'commands', function (cb) {
var err = new Error("Something wrong"),
resp = {
statusCode: 503
};
cb(err, resp);
});
};

var restore_request_stub = function () {
request_stub.restore();
};

var load_module = function (cb) {
interval.load.call(common_obj, cb);
};
Expand Down Expand Up @@ -87,7 +102,7 @@ describe('interval', function () {
reset_spies();
});

it('dequeues the request', function () {
it('dequeues the previous request', function () {

load_module(function (err, emitter) {
bus.emit("reachable");
Expand Down Expand Up @@ -162,29 +177,96 @@ describe('interval', function () {

});

it('orders request to re-schedule', function () {

set_request_stub();

load_module(function (err, emitter) {
bus.emit("unreachable");
});

setTimeout_spy.args[1][2].should.equal(true);

restore_request_stub();

});

it('keeps scheduling requests', function () {

var clock = sinon.useFakeTimers();

set_request_stub();

load_module(function (err, emitter) {
bus.emit("unreachable");
});

// wait for 3 requests of 30 sec.
clock.tick(90000);
clock.restore();

request_stub.callCount.should.equal(3);

restore_request_stub();

});

});

describe('on check', function() {

var api_stub = {};

before(function () {
set_request_stub();
});

beforeEach(function () {
request_stub.reset();
interval.unload();
api_stub = sinon.stub(api.devices.get, 'commands', function (cb) {
return true;
});
reset_spies();
});

after(function() {
restore_request_stub();
});

it('triggers a request', function(done) {

load_module(function (err, emitter) {
load_module(function (err, emitter) {});
interval.check();
request_stub.calledOnce.should.equal(true);
done();

interval.check();
api_stub.calledOnce.should.equal(true);
done();
});

it('does not order request to re-schedule', function(done) {

load_module(function (err, emitter) {});
interval.check();
request_stub.args[0].length.should.equal(1);
done();

});

it('does not queue a request', function(done) {

var clock = sinon.useFakeTimers();

load_module(function (err, emitter) {
bus.emit("reachable");
});

interval.check();

// wait for 2 requests of 20 min.
clock.tick(2400000);
clock.restore();

// 1st request = interval.check()
// 2nd request = 20 min. after load
// 3rd request = 40 min. after load
request_stub.callCount.should.equal(3);
done();

});

});
Expand Down
7 changes: 4 additions & 3 deletions lib/agent/providers/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var process_response = function(body, cb){
var data = {
lat: coords.location.lat || coords.location.latitude,
lng: coords.location.lng || coords.location.longitude,
accuracy: coords.accuracy || coords.location.accuracy
accuracy: coords.accuracy || coords.location.accuracy,
method: 'wifi'
};

cb(null, data);
Expand All @@ -45,7 +46,7 @@ exports.send_data = function(list, cb){
var str = 'wifi=mac:' + ap.mac_address + '|ssid:' + encodeURIComponent(ap.ssid);
str += '|ss:' + ap.signal_strength;
aps.push(str);
})
});

var opts = { user_agent: get_agent() },
url = 'https://maps.googleapis.com/maps/api/browserlocation/json?',
Expand All @@ -56,7 +57,7 @@ exports.send_data = function(list, cb){
process_response(body, cb);
});

}
};

exports.get_location = function(callback){

Expand Down
25 changes: 17 additions & 8 deletions test/lib/agent/providers/geo/coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('location', function(){

before(function(){
helpers.stub_request('get', null, {}, 'Bad response');
})
});

it('returns error', function(done){

provider.send_data(list, function(err, data){
err.should.be.an.instanceof(Error);
should.not.exist(data);
done();
})
});

});

Expand All @@ -50,18 +50,27 @@ describe('location', function(){
before(function(done){
fs.readFile(__dirname + '/../fixtures/location_response.json', function(err, data){
helpers.stub_request('get', null, { statusCode: 200 }, data.toString().trim());
done()
})
})
done();
});
});

it('callsback coordinates', function(done){

provider.send_data(list, function(err, data){
// should.not.exist(err); TODO: fix this
data.should.be.an.instanceof(Object);
data.should.have.keys(['lat', 'lng', 'accuracy'])
data.should.have.keys(['lat', 'lng', 'accuracy', 'method']);
done();
});

});

it('sets method to wifi', function (done) {

provider.send_data(list, function (err, data){
data.method.should.equal('wifi');
done();
})
});

});

Expand All @@ -81,7 +90,7 @@ describe('location', function(){
console.log('========================================\n');
}
done();
})
});

});

Expand Down

0 comments on commit 32acf00

Please sign in to comment.