|
| 1 | +import { VaaConfig, VaaCache } from "../listen"; |
| 2 | + |
| 3 | +describe("VAA Cache works", () => { |
| 4 | + test("Setting and getting works as expected", async () => { |
| 5 | + const cache = new VaaCache(); |
| 6 | + |
| 7 | + expect(cache.get("a", 3)).toBeUndefined(); |
| 8 | + |
| 9 | + cache.set("a", 1, "a-1"); |
| 10 | + |
| 11 | + expect(cache.get("a", 3)).toBeUndefined(); |
| 12 | + |
| 13 | + cache.set("a", 4, "a-2"); |
| 14 | + |
| 15 | + expect(cache.get("a", 3)).toEqual<VaaConfig>({ |
| 16 | + publishTime: 4, |
| 17 | + vaa: "a-2", |
| 18 | + }); |
| 19 | + |
| 20 | + cache.set("a", 10, "a-3"); |
| 21 | + |
| 22 | + // Adding some elements with other keys to make sure |
| 23 | + // they are not stored separately. |
| 24 | + cache.set("b", 3, "b-1"); |
| 25 | + cache.set("b", 7, "b-2"); |
| 26 | + cache.set("b", 9, "b-3"); |
| 27 | + |
| 28 | + expect(cache.get("a", 3)).toEqual<VaaConfig>({ |
| 29 | + publishTime: 4, |
| 30 | + vaa: "a-2", |
| 31 | + }); |
| 32 | + expect(cache.get("a", 4)).toEqual<VaaConfig>({ |
| 33 | + publishTime: 4, |
| 34 | + vaa: "a-2", |
| 35 | + }); |
| 36 | + expect(cache.get("a", 5)).toEqual<VaaConfig>({ |
| 37 | + publishTime: 10, |
| 38 | + vaa: "a-3", |
| 39 | + }); |
| 40 | + expect(cache.get("a", 10)).toEqual<VaaConfig>({ |
| 41 | + publishTime: 10, |
| 42 | + vaa: "a-3", |
| 43 | + }); |
| 44 | + |
| 45 | + expect(cache.get("b", 3)).toEqual<VaaConfig>({ |
| 46 | + publishTime: 3, |
| 47 | + vaa: "b-1", |
| 48 | + }); |
| 49 | + expect(cache.get("b", 4)).toEqual<VaaConfig>({ |
| 50 | + publishTime: 7, |
| 51 | + vaa: "b-2", |
| 52 | + }); |
| 53 | + |
| 54 | + // When no item item more recent than asked pubTime is asked it should return undefined |
| 55 | + expect(cache.get("a", 11)).toBeUndefined(); |
| 56 | + expect(cache.get("b", 10)).toBeUndefined(); |
| 57 | + |
| 58 | + // When the asked pubTime is less than the first existing pubTime we are not sure that |
| 59 | + // this is the first vaa after that time, so we should return undefined. |
| 60 | + expect(cache.get("a", 0)).toBeUndefined(); |
| 61 | + expect(cache.get("b", 1)).toBeUndefined(); |
| 62 | + expect(cache.get("b", 2)).toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + test("removeExpiredValues clears the old values", async () => { |
| 66 | + jest.useFakeTimers(); |
| 67 | + |
| 68 | + // TTL of 500 seconds for the cache |
| 69 | + const cache = new VaaCache(500); |
| 70 | + |
| 71 | + cache.set("a", 300, "a-1"); |
| 72 | + cache.set("a", 700, "a-2"); |
| 73 | + cache.set("a", 900, "a-3"); |
| 74 | + |
| 75 | + expect(cache.get("a", 300)).toEqual<VaaConfig>({ |
| 76 | + publishTime: 300, |
| 77 | + vaa: "a-1", |
| 78 | + }); |
| 79 | + |
| 80 | + expect(cache.get("a", 500)).toEqual<VaaConfig>({ |
| 81 | + publishTime: 700, |
| 82 | + vaa: "a-2", |
| 83 | + }); |
| 84 | + |
| 85 | + // Set time to second 1000 |
| 86 | + jest.setSystemTime(1000 * 1000); |
| 87 | + |
| 88 | + cache.removeExpiredValues(); |
| 89 | + |
| 90 | + expect(cache.get("a", 300)).toBeUndefined(); |
| 91 | + expect(cache.get("a", 500)).toBeUndefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + test("the cache clean loop works", async () => { |
| 95 | + jest.useFakeTimers(); |
| 96 | + |
| 97 | + // TTL of 500 seconds for the cache and cleanup of every 100 seconds |
| 98 | + const cache = new VaaCache(500, 100); |
| 99 | + cache.runRemoveExpiredValuesLoop(); |
| 100 | + |
| 101 | + cache.set("a", 300, "a-1"); |
| 102 | + cache.set("a", 700, "a-2"); |
| 103 | + cache.set("a", 900, "a-3"); |
| 104 | + |
| 105 | + expect(cache.get("a", 900)).toEqual<VaaConfig>({ |
| 106 | + publishTime: 900, |
| 107 | + vaa: "a-3", |
| 108 | + }); |
| 109 | + |
| 110 | + // Set time to second 2000. Everything should be evicted from cache now. |
| 111 | + jest.setSystemTime(2000 * 1000); |
| 112 | + jest.advanceTimersToNextTimer(); |
| 113 | + |
| 114 | + expect(cache.get("a", 900)).toBeUndefined(); |
| 115 | + }); |
| 116 | +}); |
0 commit comments