-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathchain_test.go
74 lines (55 loc) · 1.65 KB
/
chain_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
package chain
import (
"testing"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/faabiosr/cachego/memcached"
"github.com/faabiosr/cachego/sync"
)
const (
testKey = "foo"
testValue = "bar"
)
func TestChain(t *testing.T) {
c := New(sync.New())
if err := c.Save(testKey, testValue, 1*time.Nanosecond); err != nil {
t.Errorf("save fail: expected nil, got %v", err)
}
if _, err := c.Fetch(testKey); err == nil {
t.Errorf("fetch fail: expected an error, got %v", err)
}
_ = c.Save(testKey, testValue, 10*time.Second)
if res, _ := c.Fetch(testKey); res != testValue {
t.Errorf("fetch fail, wrong value: expected %s, got %s", testValue, res)
}
_ = c.Save(testKey, testValue, 0)
if !c.Contains(testKey) {
t.Errorf("contains failed: the key %s should be exist", testKey)
}
_ = c.Save("bar", testValue, 0)
if values := c.FetchMulti([]string{testKey, "bar"}); len(values) == 0 {
t.Errorf("fetch multi failed: expected %d, got %d", 2, len(values))
}
if err := c.Delete(testKey); err != nil {
t.Errorf("delete failed: expected nil, got %v", err)
}
if err := c.Flush(); err != nil {
t.Errorf("flush failed: expected nil, got %v", err)
}
if c.Contains(testKey) {
t.Errorf("contains failed: the key %s should not be exist", testKey)
}
c = New(
sync.New(),
memcached.New(memcache.New("127.0.0.1:22222")),
)
if err := c.Save(testKey, testValue, 0); err == nil {
t.Errorf("save failed: expected an error, got %v", err)
}
if err := c.Delete(testKey); err == nil {
t.Errorf("delete failed: expected an error, got %v", err)
}
if err := c.Flush(); err == nil {
t.Errorf("flush failed: expected an error, got %v", err)
}
}