Skip to content

Commit

Permalink
Add test that verifies the hibernation detection behaviour
Browse files Browse the repository at this point in the history
The real-world test would be to actually wait for a couple of minutes (with 2 *
heartbeat of default settings) in the unit test, but this is not feasible, so
just modify the heartbeat-setting to a lower value.

I tested it by only running tests inside `tests/timeago.test.js` and by actually
deploying this code and testing the alarms with Pushover and reading the logs in
Papertrail.

Before this change, I saw a 'Hibernation detected' log every minute. After this
change, I didn't see it anymore, probably because the app wasn't actually
hibernated (yet).
  • Loading branch information
fibbers committed Oct 31, 2019
1 parent 7a4cc71 commit aa52a81
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/timeago.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('timeago', function() {
ctx.ddata = require('../lib/data/ddata')();
ctx.notifications = require('../lib/notifications')(env, ctx);
ctx.language = require('../lib/language')();
ctx.settings = require('../lib/settings')();
ctx.settings.heartbeat = 0.5; // short heartbeat to speedup tests

var timeago = require('../lib/plugins/timeago')(ctx);

Expand Down Expand Up @@ -41,6 +43,32 @@ describe('timeago', function() {
done();
});

it('should suspend alarms due to hibernation when 2 heartbeats are skipped', function() {
ctx.ddata.sgvs = [{ mills: Date.now() - times.mins(16).msecs, mgdl: 100, type: 'sgv' }];

var sbx = freshSBX()
var status = timeago.checkStatus(sbx);
// By default (no hibernation detected) a warning should be given
should.equal(status, 'warn')

// 10ms more than suspend-threshold to prevent flapping tests
var timeoutMs = 2 * ctx.settings.heartbeat * 1000 + 100;
return new Promise(function(resolve, reject) {
setTimeout(function() {
status = timeago.checkStatus(sbx);
// Because hibernation should now be detected, no warning should be given
should.equal(status, 'current')

// We immediately ask status again, so hibernation should not be detected anymore,
// and we should receive a warning again
status = timeago.checkStatus(sbx);
should.equal(status, 'warn')

resolve()
}, timeoutMs)
})
});

it('should trigger a warning when data older than 15m', function(done) {
ctx.notifications.initRequests();
ctx.ddata.sgvs = [{ mills: Date.now() - times.mins(16).msecs, mgdl: 100, type: 'sgv' }];
Expand Down

0 comments on commit aa52a81

Please sign in to comment.