OLD | NEW |
(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 Convey("memcache", t, func() { |
| 21 now := time.Now() |
| 22 timeNow := func(context.Context) time.Time { |
| 23 ret := now |
| 24 now = now.Add(time.Second) |
| 25 return ret |
| 26 } |
| 27 c := UseMC(UseGI(wrapper.SetTimeNowFactory(Enable(context.Backgr
ound()), timeNow))) |
| 28 mc := wrapper.GetMC(c) |
| 29 mci := mc.(*memcacheImpl) |
| 30 |
| 31 Convey("implements MCSingleReadWriter", func() { |
| 32 Convey("Add", func() { |
| 33 itm := &memcache.Item{ |
| 34 Key: "sup", |
| 35 Value: []byte("cool"), |
| 36 Expiration: time.Second, |
| 37 } |
| 38 err := mc.Add(itm) |
| 39 So(err, ShouldBeNil) |
| 40 Convey("which rejects objects already there", fu
nc() { |
| 41 err := mc.Add(itm) |
| 42 So(err, ShouldEqual, memcache.ErrNotStor
ed) |
| 43 }) |
| 44 |
| 45 Convey("which can be broken intentionally", func
() { |
| 46 mci.BreakFeatures(nil, "Add") |
| 47 err := mc.Add(itm) |
| 48 So(err, ShouldEqual, memcache.ErrServerE
rror) |
| 49 }) |
| 50 }) |
| 51 |
| 52 Convey("Get", func() { |
| 53 itm := &memcache.Item{ |
| 54 Key: "sup", |
| 55 Value: []byte("cool"), |
| 56 Expiration: time.Second, |
| 57 } |
| 58 err := mc.Add(itm) |
| 59 So(err, ShouldBeNil) |
| 60 |
| 61 testItem := &memcache.Item{ |
| 62 Key: "sup", |
| 63 Value: []byte("cool"), |
| 64 } |
| 65 unsafe.MCSetCasID(testItem, 1) |
| 66 i, err := mc.Get("sup") |
| 67 So(err, ShouldBeNil) |
| 68 So(i, ShouldResemble, testItem) |
| 69 |
| 70 Convey("which can expire", func() { |
| 71 now = now.Add(time.Second * 4) |
| 72 i, err := mc.Get("sup") |
| 73 So(err, ShouldEqual, memcache.ErrCacheMi
ss) |
| 74 So(i, ShouldBeNil) |
| 75 }) |
| 76 |
| 77 Convey("which can be broken intentionally", func
() { |
| 78 mci.BreakFeatures(nil, "Get") |
| 79 _, err := mc.Get("sup") |
| 80 So(err, ShouldEqual, memcache.ErrServerE
rror) |
| 81 }) |
| 82 }) |
| 83 |
| 84 Convey("Delete", func() { |
| 85 Convey("works if it's there", func() { |
| 86 itm := &memcache.Item{ |
| 87 Key: "sup", |
| 88 Value: []byte("cool"), |
| 89 Expiration: time.Second, |
| 90 } |
| 91 err := mc.Add(itm) |
| 92 So(err, ShouldBeNil) |
| 93 |
| 94 err = mc.Delete("sup") |
| 95 So(err, ShouldBeNil) |
| 96 |
| 97 i, err := mc.Get("sup") |
| 98 So(err, ShouldEqual, memcache.ErrCacheMi
ss) |
| 99 So(i, ShouldBeNil) |
| 100 }) |
| 101 |
| 102 Convey("but not if it's not there", func() { |
| 103 err := mc.Delete("sup") |
| 104 So(err, ShouldEqual, memcache.ErrCacheMi
ss) |
| 105 }) |
| 106 |
| 107 Convey("and can be broken", func() { |
| 108 mci.BreakFeatures(nil, "Delete") |
| 109 err := mc.Delete("sup") |
| 110 So(err, ShouldEqual, memcache.ErrServerE
rror) |
| 111 }) |
| 112 }) |
| 113 |
| 114 Convey("Set", func() { |
| 115 itm := &memcache.Item{ |
| 116 Key: "sup", |
| 117 Value: []byte("cool"), |
| 118 Expiration: time.Second, |
| 119 } |
| 120 err := mc.Add(itm) |
| 121 So(err, ShouldBeNil) |
| 122 |
| 123 itm.Value = []byte("newp") |
| 124 err = mc.Set(itm) |
| 125 So(err, ShouldBeNil) |
| 126 |
| 127 testItem := &memcache.Item{ |
| 128 Key: "sup", |
| 129 Value: []byte("newp"), |
| 130 } |
| 131 unsafe.MCSetCasID(testItem, 2) |
| 132 i, err := mc.Get("sup") |
| 133 So(err, ShouldBeNil) |
| 134 So(i, ShouldResemble, testItem) |
| 135 |
| 136 Convey("and can be broken", func() { |
| 137 mci.BreakFeatures(nil, "Set") |
| 138 err := mc.Set(itm) |
| 139 So(err, ShouldEqual, memcache.ErrServerE
rror) |
| 140 }) |
| 141 }) |
| 142 |
| 143 Convey("CompareAndSwap", func() { |
| 144 itm := &memcache.Item{ |
| 145 Key: "sup", |
| 146 Value: []byte("cool"), |
| 147 Expiration: time.Second * 2, |
| 148 } |
| 149 err := mc.Add(itm) |
| 150 So(err, ShouldBeNil) |
| 151 |
| 152 Convey("works after a Get", func() { |
| 153 itm, err = mc.Get("sup") |
| 154 So(err, ShouldBeNil) |
| 155 So(unsafe.MCGetCasID(itm), ShouldEqual,
1) |
| 156 |
| 157 itm.Value = []byte("newp") |
| 158 err = mc.CompareAndSwap(itm) |
| 159 So(err, ShouldBeNil) |
| 160 }) |
| 161 |
| 162 Convey("but fails if you don't", func() { |
| 163 itm.Value = []byte("newp") |
| 164 err = mc.CompareAndSwap(itm) |
| 165 So(err, ShouldEqual, memcache.ErrCASConf
lict) |
| 166 }) |
| 167 |
| 168 Convey("and fails if the item is expired/gone",
func() { |
| 169 // run the clock forward |
| 170 wrapper.GetTimeNow(c) |
| 171 wrapper.GetTimeNow(c) |
| 172 itm.Value = []byte("newp") |
| 173 err = mc.CompareAndSwap(itm) |
| 174 So(err, ShouldEqual, memcache.ErrNotStor
ed) |
| 175 }) |
| 176 |
| 177 Convey("and can be broken", func() { |
| 178 mci.BreakFeatures(nil, "CompareAndSwap") |
| 179 err = mc.CompareAndSwap(itm) |
| 180 So(err, ShouldEqual, memcache.ErrServerE
rror) |
| 181 }) |
| 182 }) |
| 183 }) |
| 184 |
| 185 Convey("check that the internal implementation is sane", func()
{ |
| 186 curTime := now |
| 187 err := mc.Add(&memcache.Item{ |
| 188 Key: "sup", |
| 189 Value: []byte("cool"), |
| 190 Expiration: time.Second * 2, |
| 191 }) |
| 192 |
| 193 So(err, ShouldBeNil) |
| 194 So(len(mci.data.items), ShouldEqual, 1) |
| 195 So(mci.data.casID, ShouldEqual, 1) |
| 196 So(mci.data.items["sup"], ShouldResemble, &unsafe.Item{ |
| 197 Key: "sup", |
| 198 Value: []byte("cool"), |
| 199 CasID: 1, |
| 200 Expiration: time.Duration(curTime.Add(time.Secon
d * 2).UnixNano()), |
| 201 }) |
| 202 |
| 203 el, err := mc.Get("sup") |
| 204 So(err, ShouldBeNil) |
| 205 So(len(mci.data.items), ShouldEqual, 1) |
| 206 So(mci.data.casID, ShouldEqual, 1) |
| 207 |
| 208 testItem := &memcache.Item{ |
| 209 Key: "sup", |
| 210 Value: []byte("cool"), |
| 211 } |
| 212 unsafe.MCSetCasID(testItem, 1) |
| 213 So(el, ShouldResemble, testItem) |
| 214 }) |
| 215 |
| 216 }) |
| 217 } |
OLD | NEW |