2016-11-06 20:52:08 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const { test } = require('ava');
|
2016-11-06 20:52:08 +01:00
|
|
|
const TTLList = require('./ttl-list');
|
|
|
|
const moment = require('moment');
|
2017-08-23 11:21:34 +02:00
|
|
|
const lolex = require('lolex');
|
2016-11-06 20:52:08 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
test.cb('should emit expire', t => {
|
2017-11-02 09:23:38 +01:00
|
|
|
const clock = lolex.install();
|
2016-11-06 20:52:08 +01:00
|
|
|
const list = new TTLList({
|
|
|
|
interval: 20,
|
|
|
|
expireAmount: 10,
|
|
|
|
expireType: 'milliseconds',
|
|
|
|
});
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
list.on('expire', entry => {
|
2016-11-06 20:52:08 +01:00
|
|
|
list.destroy();
|
|
|
|
t.true(entry.n === 1);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
list.add({ n: 1 });
|
2017-11-02 09:23:38 +01:00
|
|
|
clock.tick(21);
|
2016-11-06 20:52:08 +01:00
|
|
|
});
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
test.cb('should slice off list', t => {
|
2017-08-23 11:21:34 +02:00
|
|
|
const clock = lolex.install();
|
2016-12-17 12:55:28 +01:00
|
|
|
|
2016-11-06 20:52:08 +01:00
|
|
|
const list = new TTLList({
|
|
|
|
interval: 10,
|
|
|
|
expireAmount: 10,
|
|
|
|
expireType: 'milliseconds',
|
|
|
|
});
|
|
|
|
|
|
|
|
list.add({ n: '1' }, moment().add(1, 'milliseconds'));
|
2016-11-07 08:44:46 +01:00
|
|
|
list.add({ n: '2' }, moment().add(50, 'milliseconds'));
|
|
|
|
list.add({ n: '3' }, moment().add(200, 'milliseconds'));
|
|
|
|
list.add({ n: '4' }, moment().add(300, 'milliseconds'));
|
2016-11-06 20:52:08 +01:00
|
|
|
|
|
|
|
const expired = [];
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
list.on('expire', entry => {
|
2016-11-06 20:52:08 +01:00
|
|
|
// console.timeEnd(entry.n);
|
|
|
|
expired.push(entry);
|
|
|
|
});
|
|
|
|
|
2016-12-17 12:55:28 +01:00
|
|
|
clock.tick(21);
|
|
|
|
t.true(expired.length === 1);
|
|
|
|
|
|
|
|
clock.tick(51);
|
|
|
|
t.true(expired.length === 2);
|
|
|
|
|
|
|
|
clock.tick(201);
|
|
|
|
t.true(expired.length === 3);
|
|
|
|
|
|
|
|
clock.tick(301);
|
|
|
|
t.true(expired.length === 4);
|
|
|
|
|
|
|
|
list.destroy();
|
2017-08-23 11:21:34 +02:00
|
|
|
clock.uninstall();
|
2016-12-17 12:55:28 +01:00
|
|
|
|
|
|
|
t.end();
|
2016-11-06 20:52:08 +01:00
|
|
|
});
|