-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringcache_test.go
75 lines (63 loc) · 1.17 KB
/
stringcache_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
package vbcore
import (
"testing"
"time"
)
func TestStringCacheAdd(t *testing.T) {
refreshFunc := func() string {
return "value"
}
c := NewStringCache()
c.Add("key1", 1, refreshFunc)
c.Add("key2", 1, refreshFunc)
c.Add("key3", 1, refreshFunc)
if _, ok := c.storage["key1"]; !ok {
t.Fail()
}
if _, ok := c.storage["key2"]; !ok {
t.Fail()
}
if _, ok := c.storage["key3"]; !ok {
t.Fail()
}
}
func TestStringCacheGet(t *testing.T) {
c := NewStringCache()
c.Add("key1", 1, func() string {
return "value1"
})
c.Add("key2", 1, func() string {
return "value2"
})
c.Add("key3", 1, func() string {
return "value3"
})
c.Get("key1")
c.Get("key2")
c.Get("key3")
time.Sleep(time.Duration(100) * time.Millisecond)
if c.Get("key1") != "value1" {
t.Fail()
}
if c.Get("key2") != "value2" {
t.Fail()
}
if c.Get("key3") != "value3" {
t.Fail()
}
}
func TestStringCacheBackgroundSync(t *testing.T) {
c := NewStringCache()
c.Add("something", 1, func() string {
return "value"
})
val := c.Get("something")
if val != "" {
t.Fail()
}
time.Sleep(time.Duration(100) * time.Millisecond)
val = c.Get("something")
if val != "value" {
t.Fail()
}
}