-
Notifications
You must be signed in to change notification settings - Fork 2
/
localmemcache_test.go
105 lines (85 loc) · 3.5 KB
/
localmemcache_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
package appwrap
import (
"time"
//"fmt"
. "gopkg.in/check.v1"
)
func (dsit *AppengineInterfacesTest) TestMemCache(c *C) {
cache := NewLocalMemcache()
keys := []string{"k0", "k1", "k2"}
values := [][]byte{[]byte("zero"), []byte("one"), []byte("two")}
c.Assert(cache.Add(&CacheItem{Key: keys[0], Value: values[0]}), IsNil)
c.Assert(cache.Add(&CacheItem{Key: keys[0], Value: values[0]}), Equals, CacheErrNotStored)
c.Assert(cache.Set(&CacheItem{Key: keys[1], Value: values[1]}), IsNil)
item, err := cache.Get(keys[0])
c.Assert(err, IsNil)
c.Assert(item.Value, DeepEquals, values[0])
items, err := cache.GetMulti(keys)
c.Assert(err, IsNil)
c.Assert(items[keys[0]].Value, DeepEquals, values[0])
c.Assert(items[keys[1]].Value, DeepEquals, values[1])
c.Assert(items[keys[2]], IsNil)
c.Assert(cache.DeleteMulti(keys[0:1]), Equals, nil)
items, err = cache.GetMulti(keys)
c.Assert(err, IsNil)
c.Assert(items[keys[0]], IsNil)
c.Assert(items[keys[1]].Value, DeepEquals, values[1])
c.Assert(items[keys[2]], IsNil)
c.Assert(cache.Flush(), IsNil)
items, err = cache.GetMulti(keys)
c.Assert(err, IsNil)
c.Assert(len(items), Equals, 0)
c.Assert(cache.SetMulti([]*CacheItem{{Key: keys[0], Value: values[0]}, {Key: keys[1], Value: values[1]}}), IsNil)
err = cache.AddMulti([]*CacheItem{{Key: keys[0], Value: values[0]}, {Key: keys[2], Value: values[2]}})
c.Assert(err, DeepEquals, MultiError{CacheErrNotStored, nil})
c.Assert(cache.DeleteMulti(keys[0:1]), IsNil)
c.Assert(cache.DeleteMulti(keys), DeepEquals, MultiError{ErrCacheMiss, nil, nil})
c.Assert(cache.Add(&CacheItem{Key: keys[0], Value: values[0]}), IsNil)
_, err = cache.Get(keys[0])
c.Assert(err, IsNil)
c.Assert(cache.Delete(keys[0]), IsNil)
_, err = cache.Get(keys[0])
c.Assert(err, Equals, ErrCacheMiss)
c.Assert(cache.Delete(keys[0]), Equals, ErrCacheMiss)
// Make sure zero-value expires != very old expires. Zero value
// means "never expire".
c.Assert(cache.Add(&CacheItem{Key: "neverexpire", Value: []byte("foo"), Expiration: 0}), IsNil)
c.Assert(cache.Add(&CacheItem{Key: "alreadyexpired", Value: []byte("bar"), Expiration: time.Duration(5)}), IsNil)
_, err = cache.Get("neverexpire")
c.Assert(err, IsNil)
_, err = cache.Get("alreadyexpired")
c.Assert(err, Equals, ErrCacheMiss)
}
func (dsit *AppengineInterfacesTest) TestMemCacheIncrement(c *C) {
cache := NewLocalMemcache()
_, err := cache.IncrementExisting("k", 15)
c.Assert(err, Equals, ErrCacheMiss)
v, err := cache.Increment("k", 15, 10, time.Minute)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(25))
v, err = cache.IncrementExisting("k", -10)
c.Assert(v, Equals, uint64(15))
// specifying a TTL after the key is set for the first time should have no effect on the TTL
v, err = cache.Increment("k", 15, 15, time.Second)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(30))
item, err := cache.Get("k")
c.Assert(err, IsNil)
c.Assert(item.Expiration, Equals, time.Minute)
}
func (dsit *AppengineInterfacesTest) TestMemCacheCAS(c *C) {
cache := NewLocalMemcache()
c.Assert(cache.Add(&CacheItem{Key: "k", Value: []byte("first")}), IsNil)
item, err := cache.Get("k")
c.Assert(err, IsNil)
c.Assert(cache.Set(&CacheItem{Key: "k", Value: []byte("second")}), IsNil)
item.Value = []byte("third")
c.Assert(cache.CompareAndSwap(item), Equals, CacheErrCASConflict)
item, err = cache.Get("k")
c.Assert(err, IsNil)
item.Value = []byte("third")
c.Assert(cache.CompareAndSwap(item), IsNil)
newItem, err := cache.Get("k")
c.Assert(err, IsNil)
c.Assert(string(newItem.Value), Equals, "third")
}