|
1 | 1 | 'use strict';
|
2 | 2 | const eachAsync = require('../../lib/core/utils').eachAsync;
|
| 3 | +const makeInterruptableAsyncInterval = require('../../lib/utils').makeInterruptableAsyncInterval; |
| 4 | +const now = require('../../lib/utils').now; |
3 | 5 | const expect = require('chai').expect;
|
4 | 6 |
|
5 | 7 | describe('utils', function() {
|
6 |
| - describe('eachAsync', function() { |
| 8 | + context('eachAsync', function() { |
7 | 9 | it('should callback with an error', function(done) {
|
8 | 10 | eachAsync(
|
9 | 11 | [{ error: false }, { error: true }],
|
@@ -33,4 +35,76 @@ describe('utils', function() {
|
33 | 35 | done();
|
34 | 36 | });
|
35 | 37 | });
|
| 38 | + |
| 39 | + context('makeInterruptableAsyncInterval', function() { |
| 40 | + const roundToNearestMultipleOfTen = x => Math.floor(x / 10) * 10; |
| 41 | + |
| 42 | + it('should execute a method in an repeating interval', function(done) { |
| 43 | + let lastTime = now(); |
| 44 | + const marks = []; |
| 45 | + const executor = makeInterruptableAsyncInterval( |
| 46 | + callback => { |
| 47 | + marks.push(now() - lastTime); |
| 48 | + lastTime = now(); |
| 49 | + callback(); |
| 50 | + }, |
| 51 | + { interval: 10 } |
| 52 | + ); |
| 53 | + |
| 54 | + setTimeout(() => { |
| 55 | + const roundedMarks = marks.map(roundToNearestMultipleOfTen); |
| 56 | + expect(roundedMarks.every(mark => roundedMarks[0] === mark)).to.be.true; |
| 57 | + executor.stop(); |
| 58 | + done(); |
| 59 | + }, 50); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should schedule execution sooner if requested within min interval threshold', function(done) { |
| 63 | + let lastTime = now(); |
| 64 | + const marks = []; |
| 65 | + const executor = makeInterruptableAsyncInterval( |
| 66 | + callback => { |
| 67 | + marks.push(now() - lastTime); |
| 68 | + lastTime = now(); |
| 69 | + callback(); |
| 70 | + }, |
| 71 | + { interval: 50, minInterval: 10 } |
| 72 | + ); |
| 73 | + |
| 74 | + // immediately schedule execution |
| 75 | + executor.wake(); |
| 76 | + |
| 77 | + setTimeout(() => { |
| 78 | + const roundedMarks = marks.map(roundToNearestMultipleOfTen); |
| 79 | + expect(roundedMarks[0]).to.equal(10); |
| 80 | + executor.stop(); |
| 81 | + done(); |
| 82 | + }, 50); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should debounce multiple requests to wake the interval sooner', function(done) { |
| 86 | + let lastTime = now(); |
| 87 | + const marks = []; |
| 88 | + const executor = makeInterruptableAsyncInterval( |
| 89 | + callback => { |
| 90 | + marks.push(now() - lastTime); |
| 91 | + lastTime = now(); |
| 92 | + callback(); |
| 93 | + }, |
| 94 | + { interval: 50, minInterval: 10 } |
| 95 | + ); |
| 96 | + |
| 97 | + for (let i = 0; i < 100; ++i) { |
| 98 | + executor.wake(); |
| 99 | + } |
| 100 | + |
| 101 | + setTimeout(() => { |
| 102 | + const roundedMarks = marks.map(roundToNearestMultipleOfTen); |
| 103 | + expect(roundedMarks[0]).to.equal(10); |
| 104 | + expect(roundedMarks.slice(1).every(mark => mark === 50)).to.be.true; |
| 105 | + executor.stop(); |
| 106 | + done(); |
| 107 | + }, 250); |
| 108 | + }); |
| 109 | + }); |
36 | 110 | });
|
0 commit comments