-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
57 lines (54 loc) · 1.27 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
package atm
import (
"testing"
"time"
)
func TestExpiringCache(t *testing.T) {
c := NewExpiringCache(1 * time.Second)
c.Set("a", "one", 2*time.Second)
a, found := c.Get("a")
if !found {
t.Error("Was not able to find a right after being set")
}
if "one" != a.(string) {
t.Error("Found a was not the exected a", a)
}
<-time.After(5 * time.Second)
a, found = c.Get("a")
if found {
t.Error("Should not have found a after expiration", a)
}
c.Set("a", "three", 5*time.Minute)
a, found = c.Get("a")
if !found {
t.Error("Was not able to find a after being reset")
}
if "three" != a.(string) {
t.Error("Found a was not the exected a", a)
}
}
func TestExpiringCacheLong(t *testing.T) {
c := NewExpiringCache(1 * time.Hour)
c.Set("a", "one", 1*time.Second)
c.Set("b", "two", 1*time.Hour)
a, found := c.Get("a")
if !found {
t.Error("Was not able to find a right after being set")
}
if "one" != a.(string) {
t.Error("Found a was not the exected a", a)
}
<-time.After(5 * time.Second)
a, found = c.Get("a")
if found {
t.Error("Should not have found a after expiration", a)
}
var b interface{}
b, found = c.Get("b")
if !found {
t.Error("Was not able to find b after being set")
}
if "two" != b.(string) {
t.Error("Found b was not the exected b", b)
}
}