diff --git a/lib/plugins/timeago.js b/lib/plugins/timeago.js index 6f8989a4876..4b176764d45 100644 --- a/lib/plugins/timeago.js +++ b/lib/plugins/timeago.js @@ -7,6 +7,7 @@ var lastSuspendTime = new Date("1900-01-01"); function init(ctx) { var translate = ctx.language.translate; + var heartbeatMs = ctx.settings.heartbeat * 1000; var timeago = { name: 'timeago', @@ -64,20 +65,29 @@ function init(ctx) { }; timeago.checkStatus = function checkStatus(sbx) { - // Check if the app has been suspended; if yes, snooze data missing alarmn for 15 seconds var now = new Date(); var delta = now.getTime() - lastChecked.getTime(); lastChecked = now; - if (delta > 15 * 1000) { // Looks like we've been hibernating - lastSuspendTime = now; - } + function isHibernationDetected() { + if (sbx.runtimeEnvironment === 'client') { + if (delta > 15 * 1000) { // Looks like we've been hibernating + lastSuspendTime = now; + } - var timeSinceLastSuspended = now.getTime() - lastSuspendTime.getTime(); + var timeSinceLastSuspended = now.getTime() - lastSuspendTime.getTime(); - if (timeSinceLastSuspended < (10 * 1000)) { + return timeSinceLastSuspended < (10 * 1000); + } else if (sbx.runtimeEnvironment === 'server') { + return delta > 2 * heartbeatMs; + } else { + console.error('Cannot detect hibernation, because runtimeEnvironment is not detected from sbx.runtimeEnvironment:', sbx.runtimeEnvironment); + return false; + } + } + if (isHibernationDetected()) { console.log('Hibernation detected, suspending timeago alarm'); return 'current'; } diff --git a/lib/sandbox.js b/lib/sandbox.js index 3acb9a19428..4bcee3b40e4 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -44,6 +44,7 @@ function init () { sbx.serverInit = function serverInit (env, ctx) { reset(); + sbx.runtimeEnvironment = 'server'; sbx.time = Date.now(); sbx.settings = env.settings; sbx.data = ctx.ddata.clone(); @@ -83,6 +84,7 @@ function init () { sbx.clientInit = function clientInit (ctx, time, data) { reset(); + sbx.runtimeEnvironment = 'client'; sbx.settings = ctx.settings; sbx.showPlugins = ctx.settings.showPlugins; sbx.time = time; diff --git a/tests/bgnow.test.js b/tests/bgnow.test.js index 819f3dafbfc..c87e513c48d 100644 --- a/tests/bgnow.test.js +++ b/tests/bgnow.test.js @@ -9,6 +9,7 @@ var SIX_MINS = 360000; describe('BG Now', function ( ) { var ctx = { language: require('../lib/language')() + , settings: require('../lib/settings')() }; ctx.levels = require('../lib/levels'); diff --git a/tests/iob.test.js b/tests/iob.test.js index 30872e4fb4d..1ef86d82aef 100644 --- a/tests/iob.test.js +++ b/tests/iob.test.js @@ -7,6 +7,7 @@ describe('IOB', function() { var ctx = {}; ctx.language = require('../lib/language')(); ctx.language.set('en'); + ctx.settings = require('../lib/settings')(); var iob = require('../lib/plugins/iob')(ctx); diff --git a/tests/loop.test.js b/tests/loop.test.js index 9c65ff9bdd1..b62b39c15eb 100644 --- a/tests/loop.test.js +++ b/tests/loop.test.js @@ -6,6 +6,7 @@ var moment = require('moment'); var ctx = { language: require('../lib/language')() + , settings: require('../lib/settings')() }; ctx.language.set('en'); var env = require('../env')(); diff --git a/tests/openaps.test.js b/tests/openaps.test.js index ed3dd6d3b9f..ce221c8f396 100644 --- a/tests/openaps.test.js +++ b/tests/openaps.test.js @@ -6,6 +6,7 @@ var moment = require('moment'); var ctx = { language: require('../lib/language')() + , settings: require('../lib/settings')() }; ctx.language.set('en'); var env = require('../env')(); diff --git a/tests/pump.test.js b/tests/pump.test.js index c6def822058..94e2de18974 100644 --- a/tests/pump.test.js +++ b/tests/pump.test.js @@ -6,6 +6,7 @@ var moment = require('moment'); var ctx = { language: require('../lib/language')() + , settings: require('../lib/settings')() }; ctx.language.set('en'); var env = require('../env')(); diff --git a/tests/timeago.test.js b/tests/timeago.test.js index 7b4a718ccd0..c0c88b8a1b1 100644 --- a/tests/timeago.test.js +++ b/tests/timeago.test.js @@ -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); @@ -41,6 +43,34 @@ describe('timeago', function() { done(); }); + it('should suspend alarms due to hibernation when 2 heartbeats are skipped on server', 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 + // we force no hibernation by checking status twice + status = timeago.checkStatus(sbx); + 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' }];