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

Unified Diff: impl/memory/memcache_test.go

Issue 2302743002: Interface update, per-method Contexts. (Closed)
Patch Set: Lightning talk licenses. Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « impl/memory/memcache.go ('k') | impl/memory/module.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: impl/memory/memcache_test.go
diff --git a/impl/memory/memcache_test.go b/impl/memory/memcache_test.go
index c679eb07d0af64676f6b2fa1753ff5ef642b91a0..71d3db987d69b84c767d1ea30628b9edc0400307 100644
--- a/impl/memory/memcache_test.go
+++ b/impl/memory/memcache_test.go
@@ -8,12 +8,15 @@ import (
"testing"
"time"
- infoS "github.com/luci/gae/service/info"
- mcS "github.com/luci/gae/service/memcache"
+ "github.com/luci/gae/service/info"
+ mc "github.com/luci/gae/service/memcache"
+
"github.com/luci/luci-go/common/clock/testclock"
. "github.com/luci/luci-go/common/testing/assertions"
- . "github.com/smartystreets/goconvey/convey"
+
"golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
)
func TestMemcache(t *testing.T) {
@@ -23,16 +26,15 @@ func TestMemcache(t *testing.T) {
now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
c, tc := testclock.UseTime(context.Background(), now)
c = Use(c)
- mc := mcS.Get(c)
Convey("implements MCSingleReadWriter", func() {
Convey("Add", func() {
- itm := (mc.NewItem("sup").
+ itm := (mc.NewItem(c, "sup").
SetValue([]byte("cool")).
SetExpiration(time.Second))
- So(mc.Add(itm), ShouldBeNil)
+ So(mc.Add(c, itm), ShouldBeNil)
Convey("which rejects objects already there", func() {
- So(mc.Add(itm), ShouldEqual, mcS.ErrNotStored)
+ So(mc.Add(c, itm), ShouldEqual, mc.ErrNotStored)
})
})
@@ -42,21 +44,21 @@ func TestMemcache(t *testing.T) {
value: []byte("cool"),
expiration: time.Second,
}
- So(mc.Add(itm), ShouldBeNil)
+ So(mc.Add(c, itm), ShouldBeNil)
testItem := &mcItem{
key: "sup",
value: []byte("cool"),
CasID: 1,
}
- getItm, err := mc.Get("sup")
+ getItm, err := mc.GetKey(c, "sup")
So(err, ShouldBeNil)
So(getItm, ShouldResemble, testItem)
Convey("which can expire", func() {
tc.Add(time.Second * 4)
- getItm, err := mc.Get("sup")
- So(err, ShouldEqual, mcS.ErrCacheMiss)
+ getItm, err := mc.GetKey(c, "sup")
+ So(err, ShouldEqual, mc.ErrCacheMiss)
So(getItm, ShouldResemble, &mcItem{key: "sup"})
})
})
@@ -68,16 +70,16 @@ func TestMemcache(t *testing.T) {
value: []byte("cool"),
expiration: time.Second,
}
- So(mc.Add(itm), ShouldBeNil)
+ So(mc.Add(c, itm), ShouldBeNil)
- So(mc.Delete("sup"), ShouldBeNil)
+ So(mc.Delete(c, "sup"), ShouldBeNil)
- _, err := mc.Get("sup")
- So(err, ShouldEqual, mcS.ErrCacheMiss)
+ _, err := mc.GetKey(c, "sup")
+ So(err, ShouldEqual, mc.ErrCacheMiss)
})
Convey("but not if it's not there", func() {
- So(mc.Delete("sup"), ShouldEqual, mcS.ErrCacheMiss)
+ So(mc.Delete(c, "sup"), ShouldEqual, mc.ErrCacheMiss)
})
})
@@ -87,107 +89,107 @@ func TestMemcache(t *testing.T) {
value: []byte("cool"),
expiration: time.Second,
}
- So(mc.Add(itm), ShouldBeNil)
+ So(mc.Add(c, itm), ShouldBeNil)
itm.SetValue([]byte("newp"))
- So(mc.Set(itm), ShouldBeNil)
+ So(mc.Set(c, itm), ShouldBeNil)
testItem := &mcItem{
key: "sup",
value: []byte("newp"),
CasID: 2,
}
- getItm, err := mc.Get("sup")
+ getItm, err := mc.GetKey(c, "sup")
So(err, ShouldBeNil)
So(getItm, ShouldResemble, testItem)
Convey("Flush works too", func() {
- So(mc.Flush(), ShouldBeNil)
- _, err := mc.Get("sup")
- So(err, ShouldEqual, mcS.ErrCacheMiss)
+ So(mc.Flush(c), ShouldBeNil)
+ _, err := mc.GetKey(c, "sup")
+ So(err, ShouldEqual, mc.ErrCacheMiss)
})
})
Convey("Set (nil) is equivalent to Set([]byte{})", func() {
- So(mc.Set(mc.NewItem("bob")), ShouldBeNil)
+ So(mc.Set(c, mc.NewItem(c, "bob")), ShouldBeNil)
- bob, err := mc.Get("bob")
+ bob, err := mc.GetKey(c, "bob")
So(err, ShouldBeNil)
So(bob.Value(), ShouldResemble, []byte{})
})
Convey("Increment", func() {
- val, err := mc.Increment("num", 7, 2)
+ val, err := mc.Increment(c, "num", 7, 2)
So(err, ShouldBeNil)
So(val, ShouldEqual, 9)
Convey("IncrementExisting", func() {
- val, err := mc.IncrementExisting("num", -2)
+ val, err := mc.IncrementExisting(c, "num", -2)
So(err, ShouldBeNil)
So(val, ShouldEqual, 7)
- val, err = mc.IncrementExisting("num", -100)
+ val, err = mc.IncrementExisting(c, "num", -100)
So(err, ShouldBeNil)
So(val, ShouldEqual, 0)
- _, err = mc.IncrementExisting("noexist", 2)
- So(err, ShouldEqual, mcS.ErrCacheMiss)
+ _, err = mc.IncrementExisting(c, "noexist", 2)
+ So(err, ShouldEqual, mc.ErrCacheMiss)
- So(mc.Set(mc.NewItem("text").SetValue([]byte("hello world, hooman!"))), ShouldBeNil)
+ So(mc.Set(c, mc.NewItem(c, "text").SetValue([]byte("hello world, hooman!"))), ShouldBeNil)
- _, err = mc.IncrementExisting("text", 2)
+ _, err = mc.IncrementExisting(c, "text", 2)
So(err.Error(), ShouldContainSubstring, "got invalid current value")
})
})
Convey("CompareAndSwap", func() {
- itm := mcS.Item(&mcItem{
+ itm := mc.Item(&mcItem{
key: "sup",
value: []byte("cool"),
expiration: time.Second * 2,
})
- So(mc.Add(itm), ShouldBeNil)
+ So(mc.Add(c, itm), ShouldBeNil)
Convey("works after a Get", func() {
- itm, err := mc.Get("sup")
+ itm, err := mc.GetKey(c, "sup")
So(err, ShouldBeNil)
So(itm.(*mcItem).CasID, ShouldEqual, 1)
itm.SetValue([]byte("newp"))
- So(mc.CompareAndSwap(itm), ShouldBeNil)
+ So(mc.CompareAndSwap(c, itm), ShouldBeNil)
})
Convey("but fails if you don't", func() {
itm.SetValue([]byte("newp"))
- So(mc.CompareAndSwap(itm), ShouldEqual, mcS.ErrCASConflict)
+ So(mc.CompareAndSwap(c, itm), ShouldEqual, mc.ErrCASConflict)
})
Convey("and fails if the item is expired/gone", func() {
tc.Add(3 * time.Second)
itm.SetValue([]byte("newp"))
- So(mc.CompareAndSwap(itm), ShouldEqual, mcS.ErrNotStored)
+ So(mc.CompareAndSwap(c, itm), ShouldEqual, mc.ErrNotStored)
})
})
})
Convey("check that the internal implementation is sane", func() {
curTime := now
- err := mc.Add(&mcItem{
+ err := mc.Add(c, &mcItem{
key: "sup",
value: []byte("cool"),
expiration: time.Second * 2,
})
for i := 0; i < 4; i++ {
- _, err := mc.Get("sup")
+ _, err := mc.GetKey(c, "sup")
So(err, ShouldBeNil)
}
- _, err = mc.Get("wot")
- So(err, ShouldErrLike, mcS.ErrCacheMiss)
+ _, err = mc.GetKey(c, "wot")
+ So(err, ShouldErrLike, mc.ErrCacheMiss)
- mci := mc.Raw().(*memcacheImpl)
+ mci := mc.Raw(c).(*memcacheImpl)
- stats, err := mc.Stats()
+ stats, err := mc.Stats(c)
So(err, ShouldBeNil)
So(stats.Items, ShouldEqual, 1)
So(stats.Bytes, ShouldEqual, 4)
@@ -201,7 +203,7 @@ func TestMemcache(t *testing.T) {
casID: 1,
})
- getItm, err := mc.Get("sup")
+ getItm, err := mc.GetKey(c, "sup")
So(err, ShouldBeNil)
So(len(mci.data.items), ShouldEqual, 1)
So(mci.data.casID, ShouldEqual, 1)
@@ -215,23 +217,22 @@ func TestMemcache(t *testing.T) {
})
Convey("When adding an item to an unset namespace", func() {
- _, has := infoS.Get(c).GetNamespace()
- So(has, ShouldBeFalse)
+ So(info.GetNamespace(c), ShouldEqual, "")
- item := mc.NewItem("foo").SetValue([]byte("heya"))
- So(mc.Set(item), ShouldBeNil)
+ item := mc.NewItem(c, "foo").SetValue([]byte("heya"))
+ So(mc.Set(c, item), ShouldBeNil)
Convey("The item can be retrieved from the unset namespace.", func() {
- got, err := mc.Get("foo")
+ got, err := mc.GetKey(c, "foo")
So(err, ShouldBeNil)
So(got.Value(), ShouldResemble, []byte("heya"))
})
Convey("The item can be retrieved from a set, empty namespace.", func() {
// Now test with empty namespace.
- c = infoS.Get(c).MustNamespace("")
+ c = info.MustNamespace(c, "")
- got, err := mc.Get("foo")
+ got, err := mc.GetKey(c, "foo")
So(err, ShouldBeNil)
So(got.Value(), ShouldResemble, []byte("heya"))
})
« no previous file with comments | « impl/memory/memcache.go ('k') | impl/memory/module.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698