-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcache_test.go
84 lines (76 loc) · 1.45 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
package memcache
import (
"testing"
"time"
)
func TestFull(t *testing.T) {
mc := NewCache[string, string]()
mc.SetUntil("key", "value", 0)
mc.Set("key", "value")
v := mc.Get("key")
if "value" != v.(string) {
t.Errorf("Get failed")
}
mc.Del("key")
v = mc.Get("key")
if v != nil {
t.Errorf("Get failed")
}
mc.Set("key", "value")
v = mc.Take("key")
if "value" != v.(string) {
t.Errorf("Get failed")
}
v = mc.Get("key")
if v != nil {
t.Errorf("Get failed")
}
mc.SetUntil("key", "value", 1*time.Millisecond)
time.Sleep(2 * time.Millisecond)
v = mc.Get("key")
if v != nil {
t.Errorf("Get failed")
}
v = mc.Take("key")
if v != nil {
t.Errorf("Get failed")
}
mc.SetUntil("key", "value", 1*time.Millisecond)
time.Sleep(2 * time.Millisecond)
v = mc.Take("key")
if v != nil {
t.Errorf("Get failed")
}
}
func BenchmarkSet(b *testing.B) {
mc := NewCache[string, string]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mc.Set("key", "value")
}
}
func BenchmarkGet(b *testing.B) {
mc := NewCache[string, string]()
mc.Set("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mc.Get("key")
}
}
func BenchmarkSetTake(b *testing.B) {
mc := NewCache[string, string]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mc.Set("key", "value")
mc.Take("key")
}
}
func BenchmarkSetGetDel(b *testing.B) {
mc := NewCache[string, string]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mc.Set("key", "value")
mc.Get("key")
mc.Del("key")
}
}