From 1f5473b2ddea515a85ed647d93f668600dbe2c0f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 11 Dec 2015 15:37:56 +0100 Subject: [PATCH] :white_check_mark: Test cache invalidation --- spec/native-compile-cache-spec.coffee | 44 ++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/spec/native-compile-cache-spec.coffee b/spec/native-compile-cache-spec.coffee index fceeba7c8..52571ddcb 100644 --- a/spec/native-compile-cache-spec.coffee +++ b/spec/native-compile-cache-spec.coffee @@ -1,3 +1,6 @@ +fs = require 'fs' +path = require 'path' + describe "NativeCompileCache", -> nativeCompileCache = require '../src/native-compile-cache' [fakeCacheStore, cachedFiles] = [] @@ -8,15 +11,30 @@ describe "NativeCompileCache", -> nativeCompileCache.setCacheStore(fakeCacheStore) nativeCompileCache.install() + fs.writeFileSync path.resolve('./spec/fixtures/native-cache/file-4'), """ + module.exports = function () { return "file-4" } + """ + + afterEach -> + fs.unlinkSync path.resolve('./spec/fixtures/native-cache/file-4') + it "writes and reads from the cache storage when requiring files", -> - fakeCacheStore.has.andReturn(false) + fakeCacheStore.has.andCallFake (cacheKey, invalidationKey) -> + fakeCacheStore.get(cacheKey, invalidationKey)? + fakeCacheStore.get.andCallFake (cacheKey, invalidationKey) -> + for entry in cachedFiles + continue if entry.cacheKey isnt cacheKey + continue if entry.invalidationKey isnt invalidationKey + return entry.cacheBuffer + return fakeCacheStore.set.andCallFake (cacheKey, invalidationKey, cacheBuffer) -> - cachedFiles.push({cacheKey, cacheBuffer}) + cachedFiles.push({cacheKey, invalidationKey, cacheBuffer}) fn1 = require('./fixtures/native-cache/file-1') fn2 = require('./fixtures/native-cache/file-2') + fn4 = require('./fixtures/native-cache/file-4') - expect(cachedFiles.length).toBe(2) + expect(cachedFiles.length).toBe(3) expect(cachedFiles[0].cacheKey).toBe(require.resolve('./fixtures/native-cache/file-1')) expect(cachedFiles[0].cacheBuffer).toBeInstanceOf(Uint8Array) @@ -28,14 +46,26 @@ describe "NativeCompileCache", -> expect(cachedFiles[1].cacheBuffer.length).toBeGreaterThan(0) expect(fn2()).toBe(2) - fakeCacheStore.has.andReturn(true) - fakeCacheStore.get.andReturn(cachedFiles[0].cacheBuffer) - fakeCacheStore.set.reset() + expect(cachedFiles[2].cacheKey).toBe(require.resolve('./fixtures/native-cache/file-4')) + expect(cachedFiles[2].cacheBuffer).toBeInstanceOf(Uint8Array) + expect(cachedFiles[2].cacheBuffer.length).toBeGreaterThan(0) + expect(fn4()).toBe("file-4") + fs.appendFileSync(require.resolve('./fixtures/native-cache/file-4'), "\n") + delete require('module')._cache[require.resolve('./fixtures/native-cache/file-1')] + delete require('module')._cache[require.resolve('./fixtures/native-cache/file-4')] fn1 = require('./fixtures/native-cache/file-1') + fn4 = require('./fixtures/native-cache/file-4') + + # file content has changed, ensure we create a new cache entry + expect(cachedFiles.length).toBe(4) + expect(cachedFiles[3].cacheKey).toBe(require.resolve('./fixtures/native-cache/file-4')) + expect(cachedFiles[3].invalidationKey).not.toBe(cachedFiles[2].invalidationKey) + expect(cachedFiles[3].cacheBuffer).toBeInstanceOf(Uint8Array) + expect(cachedFiles[3].cacheBuffer.length).toBeGreaterThan(0) - expect(fakeCacheStore.set).not.toHaveBeenCalled() expect(fn1()).toBe(1) + expect(fn4()).toBe("file-4") it "deletes previously cached code when the cache is an invalid file", -> fakeCacheStore.has.andReturn(true)