OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "infra/gae/libs/wrapper" | 8 "infra/gae/libs/wrapper" |
9 "infra/gae/libs/wrapper/unsafe" | 9 "infra/gae/libs/wrapper/unsafe" |
| 10 "infra/libs/clock/testclock" |
10 "testing" | 11 "testing" |
11 "time" | 12 "time" |
12 | 13 |
13 . "github.com/smartystreets/goconvey/convey" | 14 . "github.com/smartystreets/goconvey/convey" |
14 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
15 | 16 |
16 "appengine/memcache" | 17 "appengine/memcache" |
17 ) | 18 ) |
18 | 19 |
19 func TestMemcache(t *testing.T) { | 20 func TestMemcache(t *testing.T) { |
20 t.Parallel() | 21 t.Parallel() |
21 | 22 |
22 Convey("memcache", t, func() { | 23 Convey("memcache", t, func() { |
23 » » now := time.Now() | 24 » » now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC) |
24 » » timeNow := func(context.Context) time.Time { | 25 » » c, tc := testclock.UseTime(context.Background(), now) |
25 » » » ret := now | 26 » » c = Use(c) |
26 » » » now = now.Add(time.Second) | |
27 » » » return ret | |
28 » » } | |
29 » » c := Use(wrapper.SetTimeNowFactory(context.Background(), timeNow
)) | |
30 mc := wrapper.GetMC(c) | 27 mc := wrapper.GetMC(c) |
31 mci := wrapper.GetMC(c).(*memcacheImpl) | 28 mci := wrapper.GetMC(c).(*memcacheImpl) |
32 So(mc, ShouldNotEqual, mci) // two impls with the same memcacheD
ata | 29 So(mc, ShouldNotEqual, mci) // two impls with the same memcacheD
ata |
33 | 30 |
34 Convey("implements MCSingleReadWriter", func() { | 31 Convey("implements MCSingleReadWriter", func() { |
35 Convey("Add", func() { | 32 Convey("Add", func() { |
36 itm := &memcache.Item{ | 33 itm := &memcache.Item{ |
37 Key: "sup", | 34 Key: "sup", |
38 Value: []byte("cool"), | 35 Value: []byte("cool"), |
39 Expiration: time.Second, | 36 Expiration: time.Second, |
(...skipping 24 matching lines...) Expand all Loading... |
64 testItem := &memcache.Item{ | 61 testItem := &memcache.Item{ |
65 Key: "sup", | 62 Key: "sup", |
66 Value: []byte("cool"), | 63 Value: []byte("cool"), |
67 } | 64 } |
68 unsafe.MCSetCasID(testItem, 1) | 65 unsafe.MCSetCasID(testItem, 1) |
69 i, err := mc.Get("sup") | 66 i, err := mc.Get("sup") |
70 So(err, ShouldBeNil) | 67 So(err, ShouldBeNil) |
71 So(i, ShouldResemble, testItem) | 68 So(i, ShouldResemble, testItem) |
72 | 69 |
73 Convey("which can expire", func() { | 70 Convey("which can expire", func() { |
74 » » » » » now = now.Add(time.Second * 4) | 71 » » » » » tc.Add(time.Second * 4) |
75 i, err := mc.Get("sup") | 72 i, err := mc.Get("sup") |
76 So(err, ShouldEqual, memcache.ErrCacheMi
ss) | 73 So(err, ShouldEqual, memcache.ErrCacheMi
ss) |
77 So(i, ShouldBeNil) | 74 So(i, ShouldBeNil) |
78 }) | 75 }) |
79 | 76 |
80 Convey("which can be broken intentionally", func
() { | 77 Convey("which can be broken intentionally", func
() { |
81 mci.BreakFeatures(nil, "Get") | 78 mci.BreakFeatures(nil, "Get") |
82 _, err := mc.Get("sup") | 79 _, err := mc.Get("sup") |
83 So(err, ShouldEqual, memcache.ErrServerE
rror) | 80 So(err, ShouldEqual, memcache.ErrServerE
rror) |
84 }) | 81 }) |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 So(err, ShouldBeNil) | 159 So(err, ShouldBeNil) |
163 }) | 160 }) |
164 | 161 |
165 Convey("but fails if you don't", func() { | 162 Convey("but fails if you don't", func() { |
166 itm.Value = []byte("newp") | 163 itm.Value = []byte("newp") |
167 err = mc.CompareAndSwap(itm) | 164 err = mc.CompareAndSwap(itm) |
168 So(err, ShouldEqual, memcache.ErrCASConf
lict) | 165 So(err, ShouldEqual, memcache.ErrCASConf
lict) |
169 }) | 166 }) |
170 | 167 |
171 Convey("and fails if the item is expired/gone",
func() { | 168 Convey("and fails if the item is expired/gone",
func() { |
172 » » » » » // run the clock forward | 169 » » » » » tc.Add(3 * time.Second) |
173 » » » » » wrapper.GetTimeNow(c) | |
174 » » » » » wrapper.GetTimeNow(c) | |
175 itm.Value = []byte("newp") | 170 itm.Value = []byte("newp") |
176 err = mc.CompareAndSwap(itm) | 171 err = mc.CompareAndSwap(itm) |
177 So(err, ShouldEqual, memcache.ErrNotStor
ed) | 172 So(err, ShouldEqual, memcache.ErrNotStor
ed) |
178 }) | 173 }) |
179 | 174 |
180 Convey("and can be broken", func() { | 175 Convey("and can be broken", func() { |
181 mci.BreakFeatures(nil, "CompareAndSwap") | 176 mci.BreakFeatures(nil, "CompareAndSwap") |
182 err = mc.CompareAndSwap(itm) | 177 err = mc.CompareAndSwap(itm) |
183 So(err, ShouldEqual, memcache.ErrServerE
rror) | 178 So(err, ShouldEqual, memcache.ErrServerE
rror) |
184 }) | 179 }) |
(...skipping 26 matching lines...) Expand all Loading... |
211 testItem := &memcache.Item{ | 206 testItem := &memcache.Item{ |
212 Key: "sup", | 207 Key: "sup", |
213 Value: []byte("cool"), | 208 Value: []byte("cool"), |
214 } | 209 } |
215 unsafe.MCSetCasID(testItem, 1) | 210 unsafe.MCSetCasID(testItem, 1) |
216 So(el, ShouldResemble, testItem) | 211 So(el, ShouldResemble, testItem) |
217 }) | 212 }) |
218 | 213 |
219 }) | 214 }) |
220 } | 215 } |
OLD | NEW |