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