-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug when tail moves during initial fill
Fix #192
- Loading branch information
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
process.env.TAP_BAIL = '1' | ||
const t = require('tap') | ||
const LRU = require('../') | ||
const max = 10000 | ||
const cache = new LRU({ max }) | ||
|
||
const crypto = require('crypto') | ||
const getVal = () => [ | ||
crypto.randomBytes(12).toString('hex'), | ||
crypto.randomBytes(12).toString('hex'), | ||
crypto.randomBytes(12).toString('hex'), | ||
crypto.randomBytes(12).toString('hex'), | ||
] | ||
|
||
const seeds = new Array(max * 3) | ||
// fill up the cache to start | ||
for (let i = 0; i < max * 3; i++) { | ||
const v = getVal() | ||
seeds[i] = [v.join(':'), v] | ||
} | ||
t.pass('generated seed data') | ||
|
||
const verifyCache = () => { | ||
// walk down the internal list ensuring that every key is the key to that | ||
// index in the keyMap, and the value matches. | ||
for (const [k, i] of (cache.map || cache.keyMap).entries()) { | ||
const v = cache.valList[i] | ||
const key = cache.keyList[i] | ||
if (k !== key) { | ||
t.equal(k, key, 'key at proper index', { k, i }) | ||
} | ||
if (v.join(':') !== k) { | ||
t.equal(k, v.join(':'), 'proper value at index', { v, i }) | ||
} | ||
} | ||
} | ||
|
||
let cycles = 0 | ||
const cycleLength = Math.floor(max / 100) | ||
while (cycles < max * 5) { | ||
const r = Math.floor(Math.random() * seeds.length) | ||
const seed = seeds[r] | ||
const v = cache.get(seed[0]) | ||
if (v === undefined) { | ||
cache.set(seed[0], seed[1]) | ||
} else { | ||
t.equal(v.join(':'), seed[0], 'correct get ' + cycles, { seed, v }) | ||
} | ||
if (++cycles % cycleLength === 0) { | ||
verifyCache() | ||
t.pass('cycle check ' + cycles) | ||
} | ||
} |