1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 19:42:01 +02:00

The doubly linked list implementation was revealed to be buggy by simple unit tests. Fixed.

This commit is contained in:
Simo Kinnunen 2015-06-02 15:35:16 +09:00
parent d806d4fe54
commit 9130717e0e
2 changed files with 104 additions and 1 deletions

View file

@ -28,7 +28,10 @@ TtlSet.prototype.bump = function(value, time) {
this.tail = item
if (!this.head) {
if (item.prev) {
item.prev.next = item
}
else {
this.head = item
this._scheduleCheck()
}

100
test/util/ttlset.js Normal file
View file

@ -0,0 +1,100 @@
var chai = require('chai')
var expect = chai.expect
var TtlSet = require('../../lib/util/ttlset')
describe('TtlSet', function() {
describe('bump', function() {
it('should create an item for the value if none exists', function(done) {
var ttlset = new TtlSet(5000)
ttlset.bump(5)
expect(ttlset.head).to.equal(ttlset.tail)
expect(ttlset.head.value).to.equal(5)
done()
})
it('should make the item the new tail', function(done) {
var ttlset = new TtlSet(5000)
ttlset.bump(5)
expect(ttlset.tail.value).to.equal(5)
ttlset.bump(6)
expect(ttlset.tail.value).to.equal(6)
done()
})
it('should set head if none exists', function(done) {
var ttlset = new TtlSet(5000)
expect(ttlset.head).to.be.null
ttlset.bump(5)
expect(ttlset.head.value).to.equal(5)
ttlset.bump(6)
expect(ttlset.head.value).to.equal(5)
done()
})
it('should take old item out and make it the tail', function(done) {
var ttlset = new TtlSet(5000)
ttlset.bump(1)
expect(ttlset.head.value).to.equal(1)
expect(ttlset.tail.value).to.equal(1)
expect(ttlset.head.next).to.be.null
expect(ttlset.head.prev).to.be.null
expect(ttlset.tail.next).to.be.null
expect(ttlset.tail.prev).to.be.null
ttlset.bump(2)
expect(ttlset.head.value).to.equal(1)
expect(ttlset.tail.value).to.equal(2)
expect(ttlset.head.next).to.equal(ttlset.tail)
expect(ttlset.head.prev).to.be.null
expect(ttlset.tail.next).to.be.null
expect(ttlset.tail.prev).to.equal(ttlset.head)
ttlset.bump(1)
expect(ttlset.head.value).to.equal(2)
expect(ttlset.tail.value).to.equal(1)
expect(ttlset.head.next).to.equal(ttlset.tail)
expect(ttlset.head.prev).to.be.null
expect(ttlset.tail.next).to.be.null
expect(ttlset.tail.prev).to.equal(ttlset.head)
ttlset.bump(1)
expect(ttlset.head.value).to.equal(2)
expect(ttlset.tail.value).to.equal(1)
expect(ttlset.head.next).to.equal(ttlset.tail)
expect(ttlset.head.prev).to.be.null
expect(ttlset.tail.next).to.be.null
expect(ttlset.tail.prev).to.equal(ttlset.head)
done()
})
})
describe('drop', function() {
it('should silently ignore unknown values', function(done) {
var ttlset = new TtlSet(5000)
ttlset.drop(5)
done()
})
it('should remove the value from the set', function(done) {
var ttlset = new TtlSet(5000)
ttlset.bump(5)
ttlset.drop(5)
expect(ttlset.tail).to.be.null
expect(ttlset.head).to.be.null
ttlset.bump(1)
ttlset.bump(2)
ttlset.drop(1)
expect(ttlset.tail).to.equal(ttlset.head)
expect(ttlset.tail.value).to.equal(2)
ttlset.bump(3)
ttlset.drop(3)
expect(ttlset.tail).to.equal(ttlset.head)
expect(ttlset.tail.value).to.equal(2)
done()
})
})
})