-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
145 lines (101 loc) · 3.13 KB
/
cache_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
144
145
package cache_test
import (
"context"
"testing"
"time"
cache "github.com/microup/vcache"
)
func TestStartEvict(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
timeCheckNewTicker := 1 * time.Second
timeRecordEvict := 2 * time.Second
cacheInstance := cache.New(timeCheckNewTicker, timeRecordEvict)
err := cacheInstance.Add("test_key", "12345678")
if err != nil {
t.Errorf("failed add key %v", err)
}
_ = cacheInstance.StartEvict(ctx)
time.Sleep(3 * time.Second)
if _, foundKey := cacheInstance.Get("test_key"); foundKey {
t.Error("expected cache value to be evicted")
}
}
func TestCacheEvict(t *testing.T) {
t.Parallel()
ctx := context.Background()
timeCheckNewTicker := 1 * time.Second
timeRecordEvict := 1 * time.Second
cacheInstance := cache.New(timeCheckNewTicker, timeRecordEvict)
_ = cacheInstance.StartEvict(ctx)
searchValue := "12345678"
err := cacheInstance.Add("test_key", "12345678")
if err != nil {
t.Errorf("failed add key %v", err)
}
value, foundKey := cacheInstance.Get("test_key")
if !foundKey || value != searchValue {
t.Errorf("expected value in cache but got %v, %v", value, foundKey)
}
time.Sleep(2 * time.Second)
value, foundKey = cacheInstance.Get("test_key")
if foundKey || value != nil {
t.Errorf("expected value to be evicted but got %v, %v", value, foundKey)
}
}
func TestAddGetData(t *testing.T) {
t.Parallel()
timeCheckNewTicker := 1 * time.Second
timeRecordEvict := 1 * time.Second
cacheInstance := cache.New(timeCheckNewTicker, timeRecordEvict)
searchValue := "12345678"
err := cacheInstance.Add("test_key", searchValue)
if err != nil {
t.Errorf("failed add key %v", err)
}
value, foundKey := cacheInstance.Get("test_key")
if !foundKey || value != searchValue {
t.Errorf("expected value to be %s but got %s", searchValue, value)
}
value, foundKey = cacheInstance.Get("EMPTY")
if foundKey || value != nil {
t.Errorf("Expected lastUsed to be set but got nil")
}
}
func TestCache_Delete(t *testing.T) {
t.Parallel()
cacheInstance := cache.New(time.Minute, time.Hour)
// Adding a key-value pair to the cache
err := cacheInstance.Add("key1", "value1")
if err != nil {
t.Errorf("failed add key %v", err)
}
// Check if the key-value pair was added successfully
val, found := cacheInstance.Get("key1")
if !found || val != "value1" {
t.Error("Key-value pair was not added to the cache")
}
// Delete the key-value pair
cacheInstance.Delete("key1")
// Check if the key-value pair was deleted successfully
_, found = cacheInstance.Get("key1")
if found {
t.Error("Key-value pair was not deleted from the cache")
}
}
func TestDifferentTypes(t *testing.T) {
t.Parallel()
timeCheckNewTicker := 1 * time.Second
timeRecordEvict := 10 * time.Second
cacheInstance := cache.New(timeCheckNewTicker, timeRecordEvict)
searchValue := "12345678"
err := cacheInstance.Add(0.75513, searchValue)
if err != nil {
t.Errorf("failed add key %v", err)
}
value, foundKey := cacheInstance.Get(0.75513)
if !foundKey || value != searchValue {
t.Errorf("expected value to be %s but got %s", searchValue, value)
}
}