-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmemorycache_test.go
71 lines (50 loc) · 1.41 KB
/
memorycache_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
package memorycache
import (
"testing"
"time"
)
const (
testKey string = "cache:test"
testKeyEmpty string = "cache:empty"
testValue string = "Hello Test"
)
// AppCache init new cache
var AppCache = New(10*time.Minute, 1*time.Hour)
// AppCache init new cache
var AppCacheGC = New(10*time.Minute, 1*time.Second)
// TestGet get cache by key
func TestGet(t *testing.T) {
AppCache.Set(testKey, testValue, 1*time.Minute)
value, found := AppCache.Get(testKey)
if value != testValue {
t.Error("Error: ", "The received value: do not correspond to the expectation:", value, testValue)
}
if found != true {
t.Error("Error: ", "Could not get cache")
}
// get cache by key is empty
value, found = AppCache.Get(testKeyEmpty)
if value != nil || found != false {
t.Error("Error: ", "Value does not exist and must be empty", value)
}
}
// TestDelete delete cache by key
func TestDelete(t *testing.T) {
AppCache.Set(testKey, testValue, 1*time.Minute)
error := AppCache.Delete(testKey)
if error != nil {
t.Error("Error: ", "Cache delete failed")
}
value, found := AppCache.Get(testKey)
if found {
t.Error("Error: ", "Should not be found because it was deleted")
}
if value != nil {
t.Error("Error: ", "Value is not nil:", value)
}
// repeat deletion of an existing cache
error = AppCache.Delete(testKeyEmpty)
if error == nil {
t.Error("Error: ", "An empty cache should return an error")
}
}