-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemCache_test.go
143 lines (108 loc) · 3.39 KB
/
memCache_test.go
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
package simplegocache
import (
"testing"
"time"
"gotest.tools/assert"
)
func TestMemCache_AddToCache(t *testing.T) {
var testContent = []byte("This is some great test content")
var testContentLen = len(testContent)
var key = "test"
memCache := NewMemCache(60, 3, testContentLen*2)
err := memCache.AddToCache(testContent, key)
assert.NilError(t, err)
// Check for single entry
_, ok := memCache.Entries[key]
assert.Equal(t, ok, true)
// Verify second entry is rejected
err = memCache.AddToCache(testContent, key)
assert.Error(t, err, ErrorAlreadyExists)
// Check for too many items error
err = memCache.AddToCache(testContent, key+"1")
assert.NilError(t, err)
err = memCache.AddToCache(testContent, key+"2")
assert.NilError(t, err)
err = memCache.AddToCache(testContent, key+"3")
assert.Error(t, err, ErrorTooManyEntries)
// Clean before next test
delete(memCache.Entries, key)
// Verify content size error
err = memCache.AddToCache(append(append(testContent, testContent...), byte(0)), key)
assert.Error(t, err, ErrorTooLarge)
}
func TestMemCache_Close(t *testing.T) {
var testContent = []byte("This is some great test content")
var testContentLen = len(testContent)
var key = "test"
memCache := NewMemCache(60, 3, testContentLen*2)
// Setup test data
memCache.Entries[key] = Entry{
Content: testContent,
Created: time.Now().Unix(),
}
memCache.Entries[key+"1"] = Entry{
Content: testContent,
Created: time.Now().Unix(),
}
memCache.Close()
// Verify that both items are gone
assert.DeepEqual(t, memCache.Entries[key], Entry{})
assert.DeepEqual(t, memCache.Entries[key+"1"], Entry{})
}
func TestMemCache_DeleteFromCache(t *testing.T) {
var testContent = []byte("This is some great test content")
var testContentLen = len(testContent)
var key = "test"
memCache := NewMemCache(60, 3, testContentLen*2)
// Setup test data
memCache.Entries[key] = Entry{
Content: testContent,
Created: 10,
}
memCache.Entries[key+"1"] = Entry{
Content: testContent,
Created: 10,
}
memCache.DeleteFromCache(key)
// Verify that one item is gone
assert.DeepEqual(t, memCache.Entries[key], Entry{})
assert.DeepEqual(t, memCache.Entries[key+"1"], Entry{Content: testContent, Created: 10})
}
func TestMemCache_InCache(t *testing.T) {
var testContent = []byte("This is some great test content")
var testContentLen = len(testContent)
var key = "test"
memCache := NewMemCache(60, 3, testContentLen*2)
err := memCache.AddToCache(testContent, key)
assert.NilError(t, err)
// Verify invalid key does not exist
assert.Equal(t, memCache.InCache("invalid"), false)
// Verify valid key exists
assert.Equal(t, memCache.InCache(key), true)
}
func TestMemCache_Prune(t *testing.T) {
var testContent = []byte("This is some great test content")
var testContentLen = len(testContent)
var key = "test"
var now = time.Now().Unix()
memCache := NewMemCache(5, 3, testContentLen*2)
// Setup test data
memCache.Entries[key] = Entry{
Content: testContent,
Created: now + 10,
}
memCache.Entries[key+"1"] = Entry{
Content: testContent,
Created: now - 10,
}
memCache.Prune()
// Verify that both items are gone
assert.DeepEqual(t, memCache.Entries[key], Entry{Content: testContent, Created: now + 10})
assert.DeepEqual(t, memCache.Entries[key+"1"], Entry{})
}
func TestMemCache_ReadFromCache(t *testing.T) {
}
func TestMemCache_UpdateCache(t *testing.T) {
}
func TestNewMemCache(t *testing.T) {
}