|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Cache = require('../../lib/cache'); |
| 4 | +const { tmpdir } = require('../common'); |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | +const assert = require('assert'); |
| 8 | + |
| 9 | +describe('Cache', () => { |
| 10 | + const syncResult = 'content in sync'; |
| 11 | + const asyncResult = { |
| 12 | + results: 'content in async.json' |
| 13 | + }; |
| 14 | + |
| 15 | + class CachedClass { |
| 16 | + constructor(foo) { |
| 17 | + this.foo = foo; |
| 18 | + this.sync = 0; |
| 19 | + this.async = 0; |
| 20 | + } |
| 21 | + |
| 22 | + cachedSyncMethod(...args) { |
| 23 | + this.sync++; |
| 24 | + return syncResult; |
| 25 | + } |
| 26 | + |
| 27 | + async cachedAsyncMethod(...args) { |
| 28 | + this.async++; |
| 29 | + const p = Promise.resolve(asyncResult); |
| 30 | + const result = await p; // make sure it's async |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + getCacheKey(prefix, ...args) { |
| 35 | + return `${prefix}-${args.join('-')}-${this.foo}`; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + tmpdir.refresh(); |
| 40 | + const cache = new Cache(tmpdir.path); |
| 41 | + cache.wrap(CachedClass, { |
| 42 | + cachedSyncMethod(...args) { |
| 43 | + return { key: this.getCacheKey('sync', ...args), ext: '.txt' }; |
| 44 | + }, |
| 45 | + cachedAsyncMethod(...args) { |
| 46 | + return { key: this.getCacheKey('async', ...args), ext: '.json' }; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it('should cache sync results', () => { |
| 51 | + tmpdir.refresh(); |
| 52 | + cache.enable(); |
| 53 | + const expected = syncResult; |
| 54 | + const instance = new CachedClass('foo'); |
| 55 | + let actual = instance.cachedSyncMethod('test'); |
| 56 | + assert.strictEqual(instance.sync, 1); |
| 57 | + assert.strictEqual(actual, expected); |
| 58 | + |
| 59 | + let syncCache = path.join(tmpdir.path, 'sync-test-foo.txt'); |
| 60 | + let cached = fs.readFileSync(syncCache, 'utf8'); |
| 61 | + assert.strictEqual(cached, expected); |
| 62 | + |
| 63 | + // Call it again |
| 64 | + actual = instance.cachedSyncMethod('test'); |
| 65 | + assert.strictEqual(instance.sync, 1); |
| 66 | + assert.strictEqual(actual, expected); |
| 67 | + |
| 68 | + syncCache = path.join(tmpdir.path, 'sync-test-foo.txt'); |
| 69 | + cached = fs.readFileSync(syncCache, 'utf8'); |
| 70 | + assert.strictEqual(cached, expected); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should cache async results', async() => { |
| 74 | + tmpdir.refresh(); |
| 75 | + cache.enable(); |
| 76 | + const expected = Object.assign({}, asyncResult); |
| 77 | + const instance = new CachedClass('foo'); |
| 78 | + let actual = await instance.cachedAsyncMethod('test'); |
| 79 | + assert.strictEqual(instance.async, 1); |
| 80 | + assert.deepStrictEqual(actual, expected); |
| 81 | + |
| 82 | + let asyncCache = path.join(tmpdir.path, 'async-test-foo.json'); |
| 83 | + let cached = JSON.parse(fs.readFileSync(asyncCache, 'utf8')); |
| 84 | + assert.deepStrictEqual(cached, expected); |
| 85 | + |
| 86 | + // Call it again |
| 87 | + actual = await instance.cachedAsyncMethod('test'); |
| 88 | + assert.strictEqual(instance.async, 1); |
| 89 | + assert.deepStrictEqual(actual, expected); |
| 90 | + |
| 91 | + asyncCache = path.join(tmpdir.path, 'async-test-foo.json'); |
| 92 | + cached = JSON.parse(fs.readFileSync(asyncCache, 'utf8')); |
| 93 | + assert.deepStrictEqual(cached, expected); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should not cache if disabled', async() => { |
| 97 | + tmpdir.refresh(); |
| 98 | + cache.disable(); |
| 99 | + const expected = Object.assign({}, asyncResult); |
| 100 | + const instance = new CachedClass('foo'); |
| 101 | + let actual = await instance.cachedAsyncMethod('test'); |
| 102 | + assert.strictEqual(instance.async, 1); |
| 103 | + assert.deepStrictEqual(actual, expected); |
| 104 | + |
| 105 | + let list = fs.readdirSync(tmpdir.path); |
| 106 | + assert.deepStrictEqual(list, []); |
| 107 | + |
| 108 | + // Call it again |
| 109 | + actual = await instance.cachedAsyncMethod('test'); |
| 110 | + assert.strictEqual(instance.async, 2); |
| 111 | + assert.deepStrictEqual(actual, expected); |
| 112 | + |
| 113 | + list = fs.readdirSync(tmpdir.path); |
| 114 | + assert.deepStrictEqual(list, []); |
| 115 | + }); |
| 116 | +}); |
0 commit comments