|
| 1 | +import {RefCountedCache} from '../sources/RefCountedCache'; |
| 2 | + |
| 3 | +describe(`RefCountedCache`, () => { |
| 4 | + it(`should create value on first create`, () => { |
| 5 | + const actions: Array<string> = []; |
| 6 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 7 | + const result = cache.addOrCreate(`a`, () => { |
| 8 | + const result = `create a-1`; actions.push(result); return result; |
| 9 | + }) ; |
| 10 | + expect(result.value).toBe(`create a-1`); |
| 11 | + expect(actions).toStrictEqual([`create a-1`]); |
| 12 | + }); |
| 13 | + |
| 14 | + it(`should release single value`, () => { |
| 15 | + const actions: Array<string> = []; |
| 16 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 17 | + const result = cache.addOrCreate(`a`, () => { |
| 18 | + const result = `create a-1`; actions.push(result); return result; |
| 19 | + }) ; |
| 20 | + result.release(); |
| 21 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`]); |
| 22 | + }); |
| 23 | + |
| 24 | + it(`should return first created value and only release on the last value`, () => { |
| 25 | + const actions: Array<string> = []; |
| 26 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 27 | + const result1 = cache.addOrCreate(`a`, () => { |
| 28 | + const result = `create a-1`; actions.push(result); return result; |
| 29 | + }) ; |
| 30 | + expect(result1.value).toBe(`create a-1`); |
| 31 | + expect(actions).toStrictEqual([`create a-1`]); |
| 32 | + |
| 33 | + // Creating new value with same key should reuse the previous value. |
| 34 | + const result2 = cache.addOrCreate(`a`, () => { |
| 35 | + const result = `create a-2`; actions.push(result); return result; |
| 36 | + }) ; |
| 37 | + expect(result2.value).toBe(`create a-1`); |
| 38 | + expect(actions).toStrictEqual([`create a-1`]); |
| 39 | + |
| 40 | + // releasing one should not call release function |
| 41 | + result1.release(); |
| 42 | + expect(actions).toStrictEqual([`create a-1`]); |
| 43 | + |
| 44 | + // releasing second should call release, but on the first created instance. |
| 45 | + result2.release(); |
| 46 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`]); |
| 47 | + }); |
| 48 | + |
| 49 | + it(`should handle multiple keys single value`, () => { |
| 50 | + const actions: Array<string> = []; |
| 51 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 52 | + const result1 = cache.addOrCreate(`a`, () => { |
| 53 | + const result = `create a-1`; actions.push(result); return result; |
| 54 | + }) ; |
| 55 | + result1.release(); |
| 56 | + const result2 = cache.addOrCreate(`b`, () => { |
| 57 | + const result = `create b-2`; actions.push(result); return result; |
| 58 | + }) ; |
| 59 | + result2.release(); |
| 60 | + const result3 = cache.addOrCreate(`c`, () => { |
| 61 | + const result = `create c-3`; actions.push(result); return result; |
| 62 | + }) ; |
| 63 | + cache.addOrCreate(`d`, () => { |
| 64 | + const result = `create d-4`; actions.push(result); return result; |
| 65 | + }) ; |
| 66 | + const result5 = cache.addOrCreate(`e`, () => { |
| 67 | + const result = `create e-5`; actions.push(result); return result; |
| 68 | + }) ; |
| 69 | + result5.release(); |
| 70 | + // skipping release 4 release |
| 71 | + result3.release(); |
| 72 | + |
| 73 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`, `create b-2`, `release create b-2`, `create c-3`, `create d-4`, `create e-5`, `release create e-5`, `release create c-3`]); |
| 74 | + }); |
| 75 | + |
| 76 | + it(`should can create new instances after removing releasing value`, () => { |
| 77 | + const actions: Array<string> = []; |
| 78 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 79 | + const result1 = cache.addOrCreate(`a`, () => { |
| 80 | + const result = `create a-1`; actions.push(result); return result; |
| 81 | + }) ; |
| 82 | + const result2 = cache.addOrCreate(`a`, () => { |
| 83 | + const result = `create a-2`; actions.push(result); return result; |
| 84 | + }) ; |
| 85 | + result1.release(); |
| 86 | + result2.release(); |
| 87 | + const result3 = cache.addOrCreate(`a`, () => { |
| 88 | + const result = `create a-3`; actions.push(result); return result; |
| 89 | + }) ; |
| 90 | + const result4 = cache.addOrCreate(`a`, () => { |
| 91 | + const result = `create a-4`; actions.push(result); return result; |
| 92 | + }) ; |
| 93 | + result4.release(); |
| 94 | + result3.release(); |
| 95 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`, `create a-3`, `release create a-3`]); |
| 96 | + }); |
| 97 | + |
| 98 | + it(`should throw when releasing too many times`, () => { |
| 99 | + const actions: Array<string> = []; |
| 100 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 101 | + const result1 = cache.addOrCreate(`a`, () => { |
| 102 | + const result = `create a-1`; actions.push(result); return result; |
| 103 | + }) ; |
| 104 | + result1.release(); |
| 105 | + expect(result1.release).toThrow(/No known instances of: "a"/); |
| 106 | + }); |
| 107 | +}); |
0 commit comments