diff --git a/tests/frontend/helper.js b/tests/frontend/helper.js index c61a6213b..874248319 100644 --- a/tests/frontend/helper.js +++ b/tests/frontend/helper.js @@ -168,7 +168,7 @@ var helper = {}; return _fail(...args); }; - var intervalCheck = setInterval(function(){ + const check = () => { try { if (!conditionFunc()) return; deferred.resolve(); @@ -177,9 +177,11 @@ var helper = {}; } clearInterval(intervalCheck); clearTimeout(timeout); - }, intervalTime); + }; - var timeout = setTimeout(function(){ + const intervalCheck = setInterval(check, intervalTime); + + const timeout = setTimeout(() => { clearInterval(intervalCheck); var error = new Error("wait for condition never became true " + conditionFunc.toString()); deferred.reject(error); @@ -189,8 +191,11 @@ var helper = {}; } }, timeoutTime); + // Check right away to avoid an unnecessary sleep if the condition is already true. + check(); + return deferred; - } + }; helper.selectLines = function($startLine, $endLine, startOffset, endOffset){ // if no offset is provided, use beginning of start line and end of end line diff --git a/tests/frontend/specs/helper.js b/tests/frontend/specs/helper.js index 062b3f94d..14c7944b6 100644 --- a/tests/frontend/specs/helper.js +++ b/tests/frontend/specs/helper.js @@ -193,6 +193,26 @@ describe("the test helper", function(){ },100); }); }); + + describe('checks first then sleeps', function() { + it('resolves quickly if the predicate is immediately true', async function() { + const before = Date.now(); + await helper.waitFor(() => true, 1000, 900); + expect(Date.now() - before).to.be.lessThan(800); + }); + + it('polls exactly once if timeout < interval', async function() { + let calls = 0; + await helper.waitFor(() => { calls++; }, 1, 1000) + .fail(() => {}) // Suppress the redundant uncatchable exception. + .catch(() => {}); // Don't throw an exception -- we know it rejects. + expect(calls).to.be(1); + }); + + it('resolves if condition is immediately true even if timeout is 0', async function() { + await helper.waitFor(() => true, 0); + }); + }); }); describe("the selectLines method", function(){