Skip to content

Commit

Permalink
fixed broken infinite cache tests
Browse files Browse the repository at this point in the history
they may have always been broken (aka meaningless) in master
  • Loading branch information
benmosher committed Aug 23, 2016
1 parent 3ce2344 commit 6e3b530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
24 changes: 17 additions & 7 deletions tests/src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe('resolve', function () {
'import/cache': { 'lifetime': 1 },
})

const infiniteContexts = [ '∞', 'Infinity' ].map(inf => [inf,
utils.testContext({
'import/cache': { 'lifetime': inf },
})])


const pairs = [
['./CaseyKasem.js', './CASEYKASEM2.js'],
]
Expand All @@ -62,6 +68,13 @@ describe('resolve', function () {
expect(resolve(changed, context)).not.to.exist
})

// settings are part of cache key
before('warm up infinite entries', function () {
infiniteContexts.forEach(([,c]) => {
expect(resolve(original, c)).to.exist
})
})

before('rename', function (done) {
fs.rename(
utils.testFilePath(original),
Expand All @@ -86,19 +99,16 @@ describe('resolve', function () {

// special behavior for infinity
describe('infinite cache', function () {
this.timeout(1200)
before((done) => setTimeout(done, 1100))
this.timeout(1500)

const lifetimes = [ '∞', 'Infinity' ]
lifetimes.forEach(inf => {
const infiniteContext = utils.testContext({
'import/cache': { 'lifetime': inf },
})
before((done) => setTimeout(done, 1100))

infiniteContexts.forEach(([inf, infiniteContext]) => {
it(`lifetime: ${inf} still gets cached values after ~1s`, function () {
expect(resolve(original, infiniteContext), original).to.exist
})
})

})

describe('finite cache', function () {
Expand Down
5 changes: 4 additions & 1 deletion utils/ModuleCache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict"
exports.__esModule = true

const log = require('debug')('eslint-module-utils:ModuleCache')

class ModuleCache {
constructor(map) {
this.map = map || new Map()
Expand All @@ -13,6 +15,7 @@ class ModuleCache {
*/
set(cacheKey, result) {
this.map.set(cacheKey, { result, lastSeen: Date.now() })
log('setting entry for', cacheKey)
return result
}

Expand All @@ -21,7 +24,7 @@ class ModuleCache {
const f = this.map.get(cacheKey)
// check fresness
if (Date.now() - f.lastSeen < (settings.lifetime * 1000)) return f.result
}
} else log('cache miss for', cacheKey)
// cache miss
return undefined
}
Expand Down

0 comments on commit 6e3b530

Please sign in to comment.