|
| 1 | +package ccache |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/karlseguin/expect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +type SecondaryCacheTests struct{} |
| 11 | + |
| 12 | +func Test_SecondaryCache(t *testing.T) { |
| 13 | + Expectify(new(SecondaryCacheTests), t) |
| 14 | +} |
| 15 | + |
| 16 | +func (_ SecondaryCacheTests) GetsANonExistantValue() { |
| 17 | + cache := newLayered().GetOrCreateSecondaryCache("foo") |
| 18 | + Expect(cache).Not.To.Equal(nil) |
| 19 | +} |
| 20 | + |
| 21 | +func (_ SecondaryCacheTests) SetANewValue() { |
| 22 | + cache := newLayered() |
| 23 | + cache.Set("spice", "flow", "a value", time.Minute) |
| 24 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 25 | + Expect(sCache.Get("flow").Value()).To.Equal("a value") |
| 26 | + Expect(sCache.Get("stop")).To.Equal(nil) |
| 27 | +} |
| 28 | + |
| 29 | +func (_ SecondaryCacheTests) ValueCanBeSeenInBothCaches1() { |
| 30 | + cache := newLayered() |
| 31 | + cache.Set("spice", "flow", "a value", time.Minute) |
| 32 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 33 | + sCache.Set("orinoco", "another value", time.Minute) |
| 34 | + Expect(sCache.Get("orinoco").Value()).To.Equal("another value") |
| 35 | + Expect(cache.Get("spice", "orinoco").Value()).To.Equal("another value") |
| 36 | +} |
| 37 | + |
| 38 | +func (_ SecondaryCacheTests) ValueCanBeSeenInBothCaches2() { |
| 39 | + cache := newLayered() |
| 40 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 41 | + sCache.Set("flow", "a value", time.Minute) |
| 42 | + Expect(sCache.Get("flow").Value()).To.Equal("a value") |
| 43 | + Expect(cache.Get("spice", "flow").Value()).To.Equal("a value") |
| 44 | +} |
| 45 | + |
| 46 | +func (_ SecondaryCacheTests) DeletesAreReflectedInBothCaches() { |
| 47 | + cache := newLayered() |
| 48 | + cache.Set("spice", "flow", "a value", time.Minute) |
| 49 | + cache.Set("spice", "sister", "ghanima", time.Minute) |
| 50 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 51 | + |
| 52 | + cache.Delete("spice", "flow") |
| 53 | + Expect(cache.Get("spice", "flow")).To.Equal(nil) |
| 54 | + Expect(sCache.Get("flow")).To.Equal(nil) |
| 55 | + |
| 56 | + sCache.Delete("sister") |
| 57 | + Expect(cache.Get("spice", "sister")).To.Equal(nil) |
| 58 | + Expect(sCache.Get("sister")).To.Equal(nil) |
| 59 | +} |
| 60 | + |
| 61 | +func (_ SecondaryCacheTests) ReplaceDoesNothingIfKeyDoesNotExist() { |
| 62 | + cache := newLayered() |
| 63 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 64 | + Expect(sCache.Replace("flow", "value-a")).To.Equal(false) |
| 65 | + Expect(cache.Get("spice", "flow")).To.Equal(nil) |
| 66 | +} |
| 67 | + |
| 68 | +func (_ SecondaryCacheTests) ReplaceUpdatesTheValue() { |
| 69 | + cache := newLayered() |
| 70 | + cache.Set("spice", "flow", "value-a", time.Minute) |
| 71 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 72 | + Expect(sCache.Replace("flow", "value-b")).To.Equal(true) |
| 73 | + Expect(cache.Get("spice", "flow").Value().(string)).To.Equal("value-b") |
| 74 | +} |
| 75 | + |
| 76 | +func (_ SecondaryCacheTests) FetchReturnsAnExistingValue() { |
| 77 | + cache := newLayered() |
| 78 | + cache.Set("spice", "flow", "value-a", time.Minute) |
| 79 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 80 | + val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil}) |
| 81 | + Expect(val.Value().(string)).To.Equal("value-a") |
| 82 | +} |
| 83 | + |
| 84 | +func (_ SecondaryCacheTests) FetchReturnsANewValue() { |
| 85 | + cache := newLayered() |
| 86 | + sCache := cache.GetOrCreateSecondaryCache("spice") |
| 87 | + val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil}) |
| 88 | + Expect(val.Value().(string)).To.Equal("a fetched value") |
| 89 | +} |
| 90 | + |
| 91 | +func (_ SecondaryCacheTests) TrackerDoesNotCleanupHeldInstance() { |
| 92 | + cache := Layered(Configure().ItemsToPrune(10).Track()) |
| 93 | + for i := 0; i < 10; i++ { |
| 94 | + cache.Set(strconv.Itoa(i), "a", i, time.Minute) |
| 95 | + } |
| 96 | + sCache := cache.GetOrCreateSecondaryCache("0") |
| 97 | + item := sCache.TrackingGet("a") |
| 98 | + time.Sleep(time.Millisecond * 10) |
| 99 | + cache.gc() |
| 100 | + Expect(cache.Get("0", "a").Value()).To.Equal(0) |
| 101 | + Expect(cache.Get("1", "a")).To.Equal(nil) |
| 102 | + item.Release() |
| 103 | + cache.gc() |
| 104 | + Expect(cache.Get("0", "a")).To.Equal(nil) |
| 105 | +} |
0 commit comments