| 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 "testing" | 8 "testing" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 value: []byte("cool"), | 41 value: []byte("cool"), |
| 42 expiration: time.Second, | 42 expiration: time.Second, |
| 43 } | 43 } |
| 44 So(mc.Add(itm), ShouldBeNil) | 44 So(mc.Add(itm), ShouldBeNil) |
| 45 | 45 |
| 46 testItem := &mcItem{ | 46 testItem := &mcItem{ |
| 47 key: "sup", | 47 key: "sup", |
| 48 value: []byte("cool"), | 48 value: []byte("cool"), |
| 49 CasID: 1, | 49 CasID: 1, |
| 50 } | 50 } |
| 51 » » » » getItm := &mcItem{ | 51 » » » » getItm, err := mc.Get("sup") |
| 52 » » » » » key: "sup", | 52 » » » » So(err, ShouldBeNil) |
| 53 » » » » } | |
| 54 » » » » So(mc.Get(getItm), ShouldBeNil) | |
| 55 So(getItm, ShouldResemble, testItem) | 53 So(getItm, ShouldResemble, testItem) |
| 56 | 54 |
| 57 Convey("which can expire", func() { | 55 Convey("which can expire", func() { |
| 58 tc.Add(time.Second * 4) | 56 tc.Add(time.Second * 4) |
| 59 » » » » » getItm := &mcItem{key: "sup"} | 57 » » » » » getItm, err := mc.Get("sup") |
| 60 » » » » » So(mc.Get(getItm), ShouldEqual, mcS.ErrC
acheMiss) | 58 » » » » » So(err, ShouldEqual, mcS.ErrCacheMiss) |
| 61 So(getItm, ShouldResemble, &mcItem{key:
"sup"}) | 59 So(getItm, ShouldResemble, &mcItem{key:
"sup"}) |
| 62 }) | 60 }) |
| 63 }) | 61 }) |
| 64 | 62 |
| 65 Convey("Delete", func() { | 63 Convey("Delete", func() { |
| 66 Convey("works if it's there", func() { | 64 Convey("works if it's there", func() { |
| 67 itm := &mcItem{ | 65 itm := &mcItem{ |
| 68 key: "sup", | 66 key: "sup", |
| 69 value: []byte("cool"), | 67 value: []byte("cool"), |
| 70 expiration: time.Second, | 68 expiration: time.Second, |
| 71 } | 69 } |
| 72 So(mc.Add(itm), ShouldBeNil) | 70 So(mc.Add(itm), ShouldBeNil) |
| 73 | 71 |
| 74 So(mc.Delete("sup"), ShouldBeNil) | 72 So(mc.Delete("sup"), ShouldBeNil) |
| 75 | 73 |
| 76 » » » » » So(mc.Get(mc.NewItem("sup")), ShouldEqua
l, mcS.ErrCacheMiss) | 74 » » » » » _, err := mc.Get("sup") |
| 75 » » » » » So(err, ShouldEqual, mcS.ErrCacheMiss) |
| 77 }) | 76 }) |
| 78 | 77 |
| 79 Convey("but not if it's not there", func() { | 78 Convey("but not if it's not there", func() { |
| 80 So(mc.Delete("sup"), ShouldEqual, mcS.Er
rCacheMiss) | 79 So(mc.Delete("sup"), ShouldEqual, mcS.Er
rCacheMiss) |
| 81 }) | 80 }) |
| 82 }) | 81 }) |
| 83 | 82 |
| 84 Convey("Set", func() { | 83 Convey("Set", func() { |
| 85 itm := &mcItem{ | 84 itm := &mcItem{ |
| 86 key: "sup", | 85 key: "sup", |
| 87 value: []byte("cool"), | 86 value: []byte("cool"), |
| 88 expiration: time.Second, | 87 expiration: time.Second, |
| 89 } | 88 } |
| 90 So(mc.Add(itm), ShouldBeNil) | 89 So(mc.Add(itm), ShouldBeNil) |
| 91 | 90 |
| 92 itm.SetValue([]byte("newp")) | 91 itm.SetValue([]byte("newp")) |
| 93 So(mc.Set(itm), ShouldBeNil) | 92 So(mc.Set(itm), ShouldBeNil) |
| 94 | 93 |
| 95 testItem := &mcItem{ | 94 testItem := &mcItem{ |
| 96 key: "sup", | 95 key: "sup", |
| 97 value: []byte("newp"), | 96 value: []byte("newp"), |
| 98 CasID: 2, | 97 CasID: 2, |
| 99 } | 98 } |
| 100 » » » » getItm := mc.NewItem("sup") | 99 » » » » getItm, err := mc.Get("sup") |
| 101 » » » » So(mc.Get(getItm), ShouldBeNil) | 100 » » » » So(err, ShouldBeNil) |
| 102 So(getItm, ShouldResemble, testItem) | 101 So(getItm, ShouldResemble, testItem) |
| 103 | 102 |
| 104 Convey("Flush works too", func() { | 103 Convey("Flush works too", func() { |
| 105 So(mc.Flush(), ShouldBeNil) | 104 So(mc.Flush(), ShouldBeNil) |
| 106 » » » » » So(mc.Get(getItm), ShouldEqual, mcS.ErrC
acheMiss) | 105 » » » » » _, err := mc.Get("sup") |
| 106 » » » » » So(err, ShouldEqual, mcS.ErrCacheMiss) |
| 107 }) | 107 }) |
| 108 }) | 108 }) |
| 109 | 109 |
| 110 Convey("Increment", func() { | 110 Convey("Increment", func() { |
| 111 val, err := mc.Increment("num", 7, 2) | 111 val, err := mc.Increment("num", 7, 2) |
| 112 So(err, ShouldBeNil) | 112 So(err, ShouldBeNil) |
| 113 So(val, ShouldEqual, 9) | 113 So(val, ShouldEqual, 9) |
| 114 | 114 |
| 115 Convey("IncrementExisting", func() { | 115 Convey("IncrementExisting", func() { |
| 116 val, err := mc.IncrementExisting("num",
-2) | 116 val, err := mc.IncrementExisting("num",
-2) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 133 | 133 |
| 134 Convey("CompareAndSwap", func() { | 134 Convey("CompareAndSwap", func() { |
| 135 itm := mcS.Item(&mcItem{ | 135 itm := mcS.Item(&mcItem{ |
| 136 key: "sup", | 136 key: "sup", |
| 137 value: []byte("cool"), | 137 value: []byte("cool"), |
| 138 expiration: time.Second * 2, | 138 expiration: time.Second * 2, |
| 139 }) | 139 }) |
| 140 So(mc.Add(itm), ShouldBeNil) | 140 So(mc.Add(itm), ShouldBeNil) |
| 141 | 141 |
| 142 Convey("works after a Get", func() { | 142 Convey("works after a Get", func() { |
| 143 » » » » » itm = mc.NewItem("sup") | 143 » » » » » itm, err := mc.Get("sup") |
| 144 » » » » » So(mc.Get(itm), ShouldBeNil) | 144 » » » » » So(err, ShouldBeNil) |
| 145 So(itm.(*mcItem).CasID, ShouldEqual, 1) | 145 So(itm.(*mcItem).CasID, ShouldEqual, 1) |
| 146 | 146 |
| 147 itm.SetValue([]byte("newp")) | 147 itm.SetValue([]byte("newp")) |
| 148 So(mc.CompareAndSwap(itm), ShouldBeNil) | 148 So(mc.CompareAndSwap(itm), ShouldBeNil) |
| 149 }) | 149 }) |
| 150 | 150 |
| 151 Convey("but fails if you don't", func() { | 151 Convey("but fails if you don't", func() { |
| 152 itm.SetValue([]byte("newp")) | 152 itm.SetValue([]byte("newp")) |
| 153 So(mc.CompareAndSwap(itm), ShouldEqual,
mcS.ErrCASConflict) | 153 So(mc.CompareAndSwap(itm), ShouldEqual,
mcS.ErrCASConflict) |
| 154 }) | 154 }) |
| 155 | 155 |
| 156 Convey("and fails if the item is expired/gone",
func() { | 156 Convey("and fails if the item is expired/gone",
func() { |
| 157 tc.Add(3 * time.Second) | 157 tc.Add(3 * time.Second) |
| 158 itm.SetValue([]byte("newp")) | 158 itm.SetValue([]byte("newp")) |
| 159 So(mc.CompareAndSwap(itm), ShouldEqual,
mcS.ErrNotStored) | 159 So(mc.CompareAndSwap(itm), ShouldEqual,
mcS.ErrNotStored) |
| 160 }) | 160 }) |
| 161 }) | 161 }) |
| 162 }) | 162 }) |
| 163 | 163 |
| 164 Convey("check that the internal implementation is sane", func()
{ | 164 Convey("check that the internal implementation is sane", func()
{ |
| 165 curTime := now | 165 curTime := now |
| 166 err := mc.Add(&mcItem{ | 166 err := mc.Add(&mcItem{ |
| 167 key: "sup", | 167 key: "sup", |
| 168 value: []byte("cool"), | 168 value: []byte("cool"), |
| 169 expiration: time.Second * 2, | 169 expiration: time.Second * 2, |
| 170 }) | 170 }) |
| 171 | 171 |
| 172 » » » So(mc.Get(mc.NewItem("sup")), ShouldBeNil) | 172 » » » for i := 0; i < 4; i++ { |
| 173 » » » So(mc.Get(mc.NewItem("sup")), ShouldBeNil) | 173 » » » » _, err := mc.Get("sup") |
| 174 » » » So(mc.Get(mc.NewItem("sup")), ShouldBeNil) | 174 » » » » So(err, ShouldBeNil) |
| 175 » » » So(mc.Get(mc.NewItem("sup")), ShouldBeNil) | 175 » » » } |
| 176 » » » So(mc.Get(mc.NewItem("wot")), ShouldErrLike, mcS.ErrCach
eMiss) | 176 » » » _, err = mc.Get("wot") |
| 177 » » » So(err, ShouldErrLike, mcS.ErrCacheMiss) |
| 177 | 178 |
| 178 mci := mc.Raw().(*memcacheImpl) | 179 mci := mc.Raw().(*memcacheImpl) |
| 179 | 180 |
| 180 So(err, ShouldBeNil) | |
| 181 stats, err := mc.Stats() | 181 stats, err := mc.Stats() |
| 182 So(err, ShouldBeNil) | 182 So(err, ShouldBeNil) |
| 183 So(stats.Items, ShouldEqual, 1) | 183 So(stats.Items, ShouldEqual, 1) |
| 184 So(stats.Bytes, ShouldEqual, 4) | 184 So(stats.Bytes, ShouldEqual, 4) |
| 185 So(stats.Hits, ShouldEqual, 4) | 185 So(stats.Hits, ShouldEqual, 4) |
| 186 So(stats.Misses, ShouldEqual, 1) | 186 So(stats.Misses, ShouldEqual, 1) |
| 187 So(stats.ByteHits, ShouldEqual, 4*4) | 187 So(stats.ByteHits, ShouldEqual, 4*4) |
| 188 So(mci.data.casID, ShouldEqual, 1) | 188 So(mci.data.casID, ShouldEqual, 1) |
| 189 So(mci.data.items["sup"], ShouldResemble, &mcDataItem{ | 189 So(mci.data.items["sup"], ShouldResemble, &mcDataItem{ |
| 190 value: []byte("cool"), | 190 value: []byte("cool"), |
| 191 expiration: curTime.Add(time.Second * 2).Truncat
e(time.Second), | 191 expiration: curTime.Add(time.Second * 2).Truncat
e(time.Second), |
| 192 casID: 1, | 192 casID: 1, |
| 193 }) | 193 }) |
| 194 | 194 |
| 195 » » » getItm := mc.NewItem("sup") | 195 » » » getItm, err := mc.Get("sup") |
| 196 » » » So(mc.Get(getItm), ShouldBeNil) | 196 » » » So(err, ShouldBeNil) |
| 197 So(len(mci.data.items), ShouldEqual, 1) | 197 So(len(mci.data.items), ShouldEqual, 1) |
| 198 So(mci.data.casID, ShouldEqual, 1) | 198 So(mci.data.casID, ShouldEqual, 1) |
| 199 | 199 |
| 200 testItem := &mcItem{ | 200 testItem := &mcItem{ |
| 201 key: "sup", | 201 key: "sup", |
| 202 value: []byte("cool"), | 202 value: []byte("cool"), |
| 203 CasID: 1, | 203 CasID: 1, |
| 204 } | 204 } |
| 205 So(getItm, ShouldResemble, testItem) | 205 So(getItm, ShouldResemble, testItem) |
| 206 }) | 206 }) |
| 207 | 207 |
| 208 }) | 208 }) |
| 209 } | 209 } |
| OLD | NEW |