1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

use fake timers for ttl test

This commit is contained in:
ivaosthu 2016-12-17 12:55:28 +01:00
parent d4ddb24b1d
commit 2c46672784

View File

@ -3,6 +3,7 @@
const test = require('ava'); const test = require('ava');
const TTLList = require('./ttl-list'); const TTLList = require('./ttl-list');
const moment = require('moment'); const moment = require('moment');
const sinon = require('sinon');
test.cb('should emit expire', (t) => { test.cb('should emit expire', (t) => {
const list = new TTLList({ const list = new TTLList({
@ -21,6 +22,8 @@ test.cb('should emit expire', (t) => {
}); });
test.cb('should slice off list', (t) => { test.cb('should slice off list', (t) => {
const clock = sinon.useFakeTimers();
const list = new TTLList({ const list = new TTLList({
interval: 10, interval: 10,
expireAmount: 10, expireAmount: 10,
@ -40,18 +43,21 @@ test.cb('should slice off list', (t) => {
expired.push(entry); expired.push(entry);
}); });
setTimeout(() => { clock.tick(21);
t.true(expired.length === 1); t.true(expired.length === 1);
}, 30);
setTimeout(() => { clock.tick(51);
t.true(expired.length === 2); t.true(expired.length === 2);
}, 71);
setTimeout(() => { clock.tick(201);
t.true(expired.length === 3); t.true(expired.length === 3);
}, 221);
setTimeout(() => { clock.tick(301);
t.true(expired.length === 4); t.true(expired.length === 4);
list.destroy();
t.end(); list.destroy();
}, 330); clock.restore();
sinon.restore();
t.end();
}); });