|
| 1 | +package cache_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + "github.com/spf13/afero" |
| 10 | + |
| 11 | + "github.com/achannarasappa/ticker/v5/internal/cache" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + cachePath = "/cache/ticker/cache.json" |
| 16 | + testTTL = time.Hour |
| 17 | +) |
| 18 | + |
| 19 | +type payload struct { |
| 20 | + Name string `json:"name"` |
| 21 | + Count int `json:"count"` |
| 22 | +} |
| 23 | + |
| 24 | +var _ = Describe("Cache", func() { |
| 25 | + |
| 26 | + var fs afero.Fs |
| 27 | + |
| 28 | + BeforeEach(func() { |
| 29 | + fs = afero.NewMemMapFs() |
| 30 | + }) |
| 31 | + |
| 32 | + Describe("FilePath", func() { |
| 33 | + It("returns a path under the ticker cache directory", func() { |
| 34 | + Expect(cache.FilePath()).To(HaveSuffix("ticker/cache.json")) |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + Describe("Get and Set", func() { |
| 39 | + |
| 40 | + When("the cache is enabled", func() { |
| 41 | + |
| 42 | + It("round-trips a stored value", func() { |
| 43 | + cache := cache.New(fs, cachePath, true) |
| 44 | + cache.Set("key", payload{Name: "abc", Count: 3}, testTTL) |
| 45 | + |
| 46 | + var out payload |
| 47 | + hit := cache.Get("key", &out) |
| 48 | + |
| 49 | + Expect(hit).To(BeTrue()) |
| 50 | + Expect(out).To(Equal(payload{Name: "abc", Count: 3})) |
| 51 | + }) |
| 52 | + |
| 53 | + It("returns false for a key that was never set", func() { |
| 54 | + cache := cache.New(fs, cachePath, true) |
| 55 | + |
| 56 | + var out payload |
| 57 | + Expect(cache.Get("missing", &out)).To(BeFalse()) |
| 58 | + }) |
| 59 | + |
| 60 | + It("returns false for an entry older than its TTL", func() { |
| 61 | + cache := cache.New(fs, cachePath, true) |
| 62 | + cache.Set("key", payload{Name: "stale"}, -time.Minute) |
| 63 | + |
| 64 | + var out payload |
| 65 | + Expect(cache.Get("key", &out)).To(BeFalse()) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + When("the cache is disabled", func() { |
| 70 | + |
| 71 | + It("does not store values and always misses", func() { |
| 72 | + cache := cache.New(fs, cachePath, false) |
| 73 | + cache.Set("key", payload{Name: "abc"}, testTTL) |
| 74 | + |
| 75 | + var out payload |
| 76 | + Expect(cache.Get("key", &out)).To(BeFalse()) |
| 77 | + }) |
| 78 | + |
| 79 | + It("does not write a cache file", func() { |
| 80 | + cache := cache.New(fs, cachePath, false) |
| 81 | + cache.Set("key", payload{Name: "abc"}, testTTL) |
| 82 | + |
| 83 | + exists, _ := afero.Exists(fs, cachePath) |
| 84 | + Expect(exists).To(BeFalse()) |
| 85 | + }) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + Describe("persistence across instances", func() { |
| 90 | + |
| 91 | + It("reads values written by a previous instance sharing the same file", func() { |
| 92 | + writer := cache.New(fs, cachePath, true) |
| 93 | + writer.Set("key", payload{Name: "persisted", Count: 7}, testTTL) |
| 94 | + |
| 95 | + reader := cache.New(fs, cachePath, true) |
| 96 | + var out payload |
| 97 | + hit := reader.Get("key", &out) |
| 98 | + |
| 99 | + Expect(hit).To(BeTrue()) |
| 100 | + Expect(out).To(Equal(payload{Name: "persisted", Count: 7})) |
| 101 | + }) |
| 102 | + |
| 103 | + It("merges entries written by separate instances rather than clobbering", func() { |
| 104 | + a := cache.New(fs, cachePath, true) |
| 105 | + a.Set("a", payload{Name: "first"}, testTTL) |
| 106 | + |
| 107 | + b := cache.New(fs, cachePath, true) |
| 108 | + b.Set("b", payload{Name: "second"}, testTTL) |
| 109 | + |
| 110 | + reader := cache.New(fs, cachePath, true) |
| 111 | + var outA, outB payload |
| 112 | + Expect(reader.Get("a", &outA)).To(BeTrue()) |
| 113 | + Expect(reader.Get("b", &outB)).To(BeTrue()) |
| 114 | + Expect(outA.Name).To(Equal("first")) |
| 115 | + Expect(outB.Name).To(Equal("second")) |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + Describe("cache schema versioning", func() { |
| 120 | + |
| 121 | + // writeRawEntry writes a cache file containing a single entry under the |
| 122 | + // given raw (already-namespaced) key, simulating data written by a |
| 123 | + // ticker version using a different cache schema. |
| 124 | + writeRawEntry := func(rawKey string, p payload) { |
| 125 | + type rawEntry struct { |
| 126 | + ExpiresAt time.Time `json:"expires_at"` |
| 127 | + Payload json.RawMessage `json:"payload"` |
| 128 | + } |
| 129 | + encodedPayload, _ := json.Marshal(p) |
| 130 | + data, _ := json.Marshal(map[string]rawEntry{ |
| 131 | + rawKey: {ExpiresAt: time.Now().Add(time.Hour), Payload: encodedPayload}, |
| 132 | + }) |
| 133 | + afero.WriteFile(fs, cachePath, data, 0600) //nolint:errcheck |
| 134 | + } |
| 135 | + |
| 136 | + It("ignores entries written under a different schema version", func() { |
| 137 | + writeRawEntry("v0:key", payload{Name: "old"}) |
| 138 | + |
| 139 | + var out payload |
| 140 | + Expect(cache.New(fs, cachePath, true).Get("key", &out)).To(BeFalse()) |
| 141 | + }) |
| 142 | + |
| 143 | + It("prunes entries from other schema versions when writing", func() { |
| 144 | + writeRawEntry("v0:key", payload{Name: "old"}) |
| 145 | + |
| 146 | + cache.New(fs, cachePath, true).Set("key", payload{Name: "new"}, testTTL) |
| 147 | + |
| 148 | + var onDisk map[string]json.RawMessage |
| 149 | + data, _ := afero.ReadFile(fs, cachePath) |
| 150 | + Expect(json.Unmarshal(data, &onDisk)).To(Succeed()) |
| 151 | + Expect(onDisk).NotTo(HaveKey("v0:key")) |
| 152 | + Expect(onDisk).To(HaveLen(1)) |
| 153 | + }) |
| 154 | + }) |
| 155 | +}) |
0 commit comments