Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: go/src/infra/gae/libs/wrapper/memory/memcache_test.go

Issue 1152383003: Simple memory testing for gae/wrapper (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@better_context_lite
Patch Set: add go-slab dependency Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package memory
6
7 import (
8 "infra/gae/libs/wrapper"
9 "infra/gae/libs/wrapper/unsafe"
10 "testing"
11 "time"
12
13 . "github.com/smartystreets/goconvey/convey"
14 "golang.org/x/net/context"
15
16 "appengine/memcache"
17 )
18
19 func TestMemcache(t *testing.T) {
20 t.Parallel()
21
22 Convey("memcache", t, func() {
23 now := time.Now()
24 timeNow := func(context.Context) time.Time {
25 ret := now
26 now = now.Add(time.Second)
27 return ret
28 }
29 c := UseMC(UseGI(wrapper.SetTimeNowFactory(Enable(context.Backgr ound()), timeNow)))
30 mc := wrapper.GetMC(c)
31 mci := mc.(*memcacheImpl)
32
33 Convey("implements MCSingleReadWriter", func() {
34 Convey("Add", func() {
35 itm := &memcache.Item{
36 Key: "sup",
37 Value: []byte("cool"),
38 Expiration: time.Second,
39 }
40 err := mc.Add(itm)
41 So(err, ShouldBeNil)
42 Convey("which rejects objects already there", fu nc() {
43 err := mc.Add(itm)
44 So(err, ShouldEqual, memcache.ErrNotStor ed)
45 })
46
47 Convey("which can be broken intentionally", func () {
48 mci.BreakFeatures(nil, "Add")
49 err := mc.Add(itm)
50 So(err, ShouldEqual, memcache.ErrServerE rror)
51 })
52 })
53
54 Convey("Get", func() {
55 itm := &memcache.Item{
56 Key: "sup",
57 Value: []byte("cool"),
58 Expiration: time.Second,
59 }
60 err := mc.Add(itm)
61 So(err, ShouldBeNil)
62
63 testItem := &memcache.Item{
64 Key: "sup",
65 Value: []byte("cool"),
66 }
67 unsafe.MCSetCasID(testItem, 1)
68 i, err := mc.Get("sup")
69 So(err, ShouldBeNil)
70 So(i, ShouldResemble, testItem)
71
72 Convey("which can expire", func() {
73 now = now.Add(time.Second * 4)
74 i, err := mc.Get("sup")
75 So(err, ShouldEqual, memcache.ErrCacheMi ss)
76 So(i, ShouldBeNil)
77 })
78
79 Convey("which can be broken intentionally", func () {
80 mci.BreakFeatures(nil, "Get")
81 _, err := mc.Get("sup")
82 So(err, ShouldEqual, memcache.ErrServerE rror)
83 })
84 })
85
86 Convey("Delete", func() {
87 Convey("works if it's there", func() {
88 itm := &memcache.Item{
89 Key: "sup",
90 Value: []byte("cool"),
91 Expiration: time.Second,
92 }
93 err := mc.Add(itm)
94 So(err, ShouldBeNil)
95
96 err = mc.Delete("sup")
97 So(err, ShouldBeNil)
98
99 i, err := mc.Get("sup")
100 So(err, ShouldEqual, memcache.ErrCacheMi ss)
101 So(i, ShouldBeNil)
102 })
103
104 Convey("but not if it's not there", func() {
105 err := mc.Delete("sup")
106 So(err, ShouldEqual, memcache.ErrCacheMi ss)
107 })
108
109 Convey("and can be broken", func() {
110 mci.BreakFeatures(nil, "Delete")
111 err := mc.Delete("sup")
112 So(err, ShouldEqual, memcache.ErrServerE rror)
113 })
114 })
115
116 Convey("Set", func() {
117 itm := &memcache.Item{
118 Key: "sup",
119 Value: []byte("cool"),
120 Expiration: time.Second,
121 }
122 err := mc.Add(itm)
123 So(err, ShouldBeNil)
124
125 itm.Value = []byte("newp")
126 err = mc.Set(itm)
127 So(err, ShouldBeNil)
128
129 testItem := &memcache.Item{
130 Key: "sup",
131 Value: []byte("newp"),
132 }
133 unsafe.MCSetCasID(testItem, 2)
134 i, err := mc.Get("sup")
135 So(err, ShouldBeNil)
136 So(i, ShouldResemble, testItem)
137
138 Convey("and can be broken", func() {
139 mci.BreakFeatures(nil, "Set")
140 err := mc.Set(itm)
141 So(err, ShouldEqual, memcache.ErrServerE rror)
142 })
143 })
144
145 Convey("CompareAndSwap", func() {
146 itm := &memcache.Item{
147 Key: "sup",
148 Value: []byte("cool"),
149 Expiration: time.Second * 2,
150 }
151 err := mc.Add(itm)
152 So(err, ShouldBeNil)
153
154 Convey("works after a Get", func() {
155 itm, err = mc.Get("sup")
156 So(err, ShouldBeNil)
157 So(unsafe.MCGetCasID(itm), ShouldEqual, 1)
158
159 itm.Value = []byte("newp")
160 err = mc.CompareAndSwap(itm)
161 So(err, ShouldBeNil)
162 })
163
164 Convey("but fails if you don't", func() {
165 itm.Value = []byte("newp")
166 err = mc.CompareAndSwap(itm)
167 So(err, ShouldEqual, memcache.ErrCASConf lict)
168 })
169
170 Convey("and fails if the item is expired/gone", func() {
171 // run the clock forward
172 wrapper.GetTimeNow(c)
173 wrapper.GetTimeNow(c)
174 itm.Value = []byte("newp")
175 err = mc.CompareAndSwap(itm)
176 So(err, ShouldEqual, memcache.ErrNotStor ed)
177 })
178
179 Convey("and can be broken", func() {
180 mci.BreakFeatures(nil, "CompareAndSwap")
181 err = mc.CompareAndSwap(itm)
182 So(err, ShouldEqual, memcache.ErrServerE rror)
183 })
184 })
185 })
186
187 Convey("check that the internal implementation is sane", func() {
188 curTime := now
189 err := mc.Add(&memcache.Item{
190 Key: "sup",
191 Value: []byte("cool"),
192 Expiration: time.Second * 2,
193 })
194
195 So(err, ShouldBeNil)
196 So(len(mci.data.items), ShouldEqual, 1)
197 So(mci.data.casID, ShouldEqual, 1)
198 So(mci.data.items["sup"], ShouldResemble, &unsafe.Item{
199 Key: "sup",
200 Value: []byte("cool"),
201 CasID: 1,
202 Expiration: time.Duration(curTime.Add(time.Secon d * 2).UnixNano()),
203 })
204
205 el, err := mc.Get("sup")
206 So(err, ShouldBeNil)
207 So(len(mci.data.items), ShouldEqual, 1)
208 So(mci.data.casID, ShouldEqual, 1)
209
210 testItem := &memcache.Item{
211 Key: "sup",
212 Value: []byte("cool"),
213 }
214 unsafe.MCSetCasID(testItem, 1)
215 So(el, ShouldResemble, testItem)
216 })
217
218 })
219 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/libs/wrapper/memory/memcache.go ('k') | go/src/infra/gae/libs/wrapper/memory/memory.infra_testing » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698