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