|
| 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 | + |
| 8 | + const result = cache.addOrCreate(`a`, () => { |
| 9 | + const result = `create a-1`; actions.push(result); return result; |
| 10 | + }); |
| 11 | + |
| 12 | + expect(result.value).toBe(`create a-1`); |
| 13 | + expect(actions).toStrictEqual([`create a-1`]); |
| 14 | + }); |
| 15 | + |
| 16 | + it(`should release single value`, () => { |
| 17 | + const actions: Array<string> = []; |
| 18 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 19 | + |
| 20 | + const result = cache.addOrCreate(`a`, () => { |
| 21 | + const result = `create a-1`; actions.push(result); return result; |
| 22 | + }); |
| 23 | + |
| 24 | + result.release(); |
| 25 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`]); |
| 26 | + }); |
| 27 | + |
| 28 | + it(`should return first created value and only release on the last value`, () => { |
| 29 | + const actions: Array<string> = []; |
| 30 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 31 | + |
| 32 | + const result1 = cache.addOrCreate(`a`, () => { |
| 33 | + const result = `create a-1`; actions.push(result); return result; |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result1.value).toBe(`create a-1`); |
| 37 | + expect(actions).toStrictEqual([`create a-1`]); |
| 38 | + |
| 39 | + // Creating new value with same key should reuse the previous value. |
| 40 | + const result2 = cache.addOrCreate(`a`, () => { |
| 41 | + const result = `create a-2`; actions.push(result); return result; |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result2.value).toBe(`create a-1`); |
| 45 | + expect(actions).toStrictEqual([`create a-1`]); |
| 46 | + |
| 47 | + // releasing one should not call release function |
| 48 | + result1.release(); |
| 49 | + expect(actions).toStrictEqual([`create a-1`]); |
| 50 | + |
| 51 | + // releasing second should call release, but on the first created instance. |
| 52 | + result2.release(); |
| 53 | + expect(actions).toStrictEqual([`create a-1`, `release create a-1`]); |
| 54 | + }); |
| 55 | + |
| 56 | + it(`should handle multiple keys single value`, () => { |
| 57 | + const actions: Array<string> = []; |
| 58 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 59 | + |
| 60 | + const result1 = cache.addOrCreate(`a`, () => { |
| 61 | + const result = `create a-1`; actions.push(result); return result; |
| 62 | + }); |
| 63 | + |
| 64 | + result1.release(); |
| 65 | + |
| 66 | + const result2 = cache.addOrCreate(`b`, () => { |
| 67 | + const result = `create b-2`; actions.push(result); return result; |
| 68 | + }); |
| 69 | + |
| 70 | + result2.release(); |
| 71 | + |
| 72 | + const result3 = cache.addOrCreate(`c`, () => { |
| 73 | + const result = `create c-3`; actions.push(result); return result; |
| 74 | + }); |
| 75 | + |
| 76 | + cache.addOrCreate(`d`, () => { |
| 77 | + const result = `create d-4`; actions.push(result); return result; |
| 78 | + }); |
| 79 | + |
| 80 | + const result5 = cache.addOrCreate(`e`, () => { |
| 81 | + const result = `create e-5`; actions.push(result); return result; |
| 82 | + }); |
| 83 | + |
| 84 | + result5.release(); |
| 85 | + // skipping release 4 release |
| 86 | + result3.release(); |
| 87 | + |
| 88 | + expect(actions).toStrictEqual([ |
| 89 | + `create a-1`, |
| 90 | + `release create a-1`, |
| 91 | + `create b-2`, |
| 92 | + `release create b-2`, |
| 93 | + `create c-3`, |
| 94 | + `create d-4`, |
| 95 | + `create e-5`, |
| 96 | + `release create e-5`, |
| 97 | + `release create c-3`, |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it(`should can create new instances after removing releasing value`, () => { |
| 102 | + const actions: Array<string> = []; |
| 103 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 104 | + |
| 105 | + const result1 = cache.addOrCreate(`a`, () => { |
| 106 | + const result = `create a-1`; actions.push(result); return result; |
| 107 | + }); |
| 108 | + |
| 109 | + const result2 = cache.addOrCreate(`a`, () => { |
| 110 | + const result = `create a-2`; actions.push(result); return result; |
| 111 | + }); |
| 112 | + |
| 113 | + result1.release(); |
| 114 | + result2.release(); |
| 115 | + |
| 116 | + const result3 = cache.addOrCreate(`a`, () => { |
| 117 | + const result = `create a-3`; actions.push(result); return result; |
| 118 | + }); |
| 119 | + |
| 120 | + const result4 = cache.addOrCreate(`a`, () => { |
| 121 | + const result = `create a-4`; actions.push(result); return result; |
| 122 | + }); |
| 123 | + |
| 124 | + result4.release(); |
| 125 | + result3.release(); |
| 126 | + |
| 127 | + expect(actions).toStrictEqual([ |
| 128 | + `create a-1`, |
| 129 | + `release create a-1`, |
| 130 | + `create a-3`, |
| 131 | + `release create a-3`, |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it(`should throw when releasing too many times`, () => { |
| 136 | + const actions: Array<string> = []; |
| 137 | + const cache = new RefCountedCache<string, string>((id => actions.push(`release ${id}`))); |
| 138 | + |
| 139 | + const result1 = cache.addOrCreate(`a`, () => { |
| 140 | + const result = `create a-1`; actions.push(result); return result; |
| 141 | + }); |
| 142 | + |
| 143 | + result1.release(); |
| 144 | + |
| 145 | + expect(() => { |
| 146 | + result1.release(); |
| 147 | + }).toThrow(/No known instances of: "a"/); |
| 148 | + }); |
| 149 | +}); |
0 commit comments