File: cache_test.go

package info (click to toggle)
golang-github-karlseguin-ccache 2.0.2%2Bgit20161222.2.12c7ffd-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 132 kB
  • ctags: 161
  • sloc: makefile: 6
file content (157 lines) | stat: -rw-r--r-- 4,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ccache

import (
	"strconv"
	"testing"
	"time"

	. "github.com/karlseguin/expect"
)

type CacheTests struct{}

func Test_Cache(t *testing.T) {
	Expectify(new(CacheTests), t)
}

func (_ CacheTests) DeletesAValue() {
	cache := New(Configure())
	cache.Set("spice", "flow", time.Minute)
	cache.Set("worm", "sand", time.Minute)
	cache.Delete("spice")
	Expect(cache.Get("spice")).To.Equal(nil)
	Expect(cache.Get("worm").Value()).To.Equal("sand")
}

func (_ CacheTests) FetchesExpiredItems() {
	cache := New(Configure())
	fn := func() (interface{}, error) { return "moo-moo", nil }

	cache.Set("beef", "moo", time.Second*-1)
	Expect(cache.Get("beef").Value()).To.Equal("moo")

	out, _ := cache.Fetch("beef", time.Second, fn)
	Expect(out.Value()).To.Equal("moo-moo")
}

func (_ CacheTests) GCsTheOldestItems() {
	cache := New(Configure().ItemsToPrune(10))
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	//let the items get promoted (and added to our list)
	time.Sleep(time.Millisecond * 10)
	cache.gc()
	Expect(cache.Get("9")).To.Equal(nil)
	Expect(cache.Get("10").Value()).To.Equal(10)
}

func (_ CacheTests) PromotedItemsDontGetPruned() {
	cache := New(Configure().ItemsToPrune(10).GetsPerPromote(1))
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	time.Sleep(time.Millisecond * 10) //run the worker once to init the list
	cache.Get("9")
	time.Sleep(time.Millisecond * 10)
	cache.gc()
	Expect(cache.Get("9").Value()).To.Equal(9)
	Expect(cache.Get("10")).To.Equal(nil)
	Expect(cache.Get("11").Value()).To.Equal(11)
}

func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
	cache := New(Configure().ItemsToPrune(10).Track())
	for i := 0; i < 10; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	item := cache.TrackingGet("0")
	time.Sleep(time.Millisecond * 10)
	cache.gc()
	Expect(cache.Get("0").Value()).To.Equal(0)
	Expect(cache.Get("1")).To.Equal(nil)
	item.Release()
	cache.gc()
	Expect(cache.Get("0")).To.Equal(nil)
}

func (_ CacheTests) RemovesOldestItemWhenFull() {
	cache := New(Configure().MaxSize(5).ItemsToPrune(1))
	for i := 0; i < 7; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	time.Sleep(time.Millisecond * 10)
	Expect(cache.Get("0")).To.Equal(nil)
	Expect(cache.Get("1")).To.Equal(nil)
	Expect(cache.Get("2").Value()).To.Equal(2)
}

func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {
	cache := New(Configure().MaxSize(9).ItemsToPrune(2))
	for i := 0; i < 7; i++ {
		cache.Set(strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
	}
	time.Sleep(time.Millisecond * 10)
	Expect(cache.Get("0")).To.Equal(nil)
	Expect(cache.Get("1")).To.Equal(nil)
	Expect(cache.Get("2")).To.Equal(nil)
	Expect(cache.Get("3")).To.Equal(nil)
	Expect(cache.Get("4").Value().(*SizedItem).id).To.Equal(4)
}

func (_ CacheTests) SetUpdatesSizeOnDelta() {
	cache := New(Configure())
	cache.Set("a", &SizedItem{0, 2}, time.Minute)
	cache.Set("b", &SizedItem{0, 3}, time.Minute)
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(5))
	cache.Set("b", &SizedItem{0, 3}, time.Minute)
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(5))
	cache.Set("b", &SizedItem{0, 4}, time.Minute)
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(6))
	cache.Set("b", &SizedItem{0, 2}, time.Minute)
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(4))
	cache.Delete("b")
	time.Sleep(time.Millisecond * 100)
	Expect(cache.size).To.Equal(int64(2))
}

func (_ CacheTests) ReplaceDoesNotchangeSizeIfNotSet() {
	cache := New(Configure())
	cache.Set("1", &SizedItem{1, 2}, time.Minute)
	cache.Set("2", &SizedItem{1, 2}, time.Minute)
	cache.Set("3", &SizedItem{1, 2}, time.Minute)
	cache.Replace("4", &SizedItem{1, 2})
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(6))
}

func (_ CacheTests) ReplaceChangesSize() {
	cache := New(Configure())
	cache.Set("1", &SizedItem{1, 2}, time.Minute)
	cache.Set("2", &SizedItem{1, 2}, time.Minute)

	cache.Replace("2", &SizedItem{1, 2})
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(4))

	cache.Replace("2", &SizedItem{1, 1})
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(3))

	cache.Replace("2", &SizedItem{1, 3})
	time.Sleep(time.Millisecond * 5)
	Expect(cache.size).To.Equal(int64(5))
}

type SizedItem struct {
	id int
	s  int64
}

func (s *SizedItem) Size() int64 {
	return s.s
}