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

Unified Diff: luci_config/appengine/backend/memcache/cache_test.go

Issue 2575383002: Add server/cache support to gaeconfig. (Closed)
Patch Set: Un-collapse. Created 3 years, 11 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 | « luci_config/appengine/backend/memcache/cache.go ('k') | luci_config/appengine/format/context.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: luci_config/appengine/backend/memcache/cache_test.go
diff --git a/luci_config/appengine/backend/memcache/cache_test.go b/luci_config/appengine/backend/memcache/cache_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..9ba85107adbb51d0142194c995d5bfddb2dc42a7
--- /dev/null
+++ b/luci_config/appengine/backend/memcache/cache_test.go
@@ -0,0 +1,137 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package memcache
+
+import (
+ "math/rand"
+ "testing"
+ "time"
+
+ "github.com/luci/luci-go/common/clock/testclock"
+ memconfig "github.com/luci/luci-go/common/config/impl/memory"
+ "github.com/luci/luci-go/common/errors"
+ "github.com/luci/luci-go/luci_config/server/cfgclient"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend/caching"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend/client"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig"
+
+ "github.com/luci/gae/filter/featureBreaker"
+ "github.com/luci/gae/impl/memory"
+ mc "github.com/luci/gae/service/memcache"
+
+ "golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestMemcache(t *testing.T) {
+ t.Parallel()
+
+ Convey("Test cache", t, func() {
+ c := context.Background()
+ c = memory.Use(c)
+
+ c, clk := testclock.UseTime(c, testclock.TestTimeUTC)
+ _ = clk
+
+ c, mcFB := featureBreaker.FilterMC(c, nil)
+
+ getMCStats := func(c context.Context) *mc.Statistics {
+ st, err := mc.Stats(c)
+ if err != nil {
+ panic(err)
+ }
+ return st
+ }
+
+ configDB := map[string]memconfig.ConfigSet{
+ "projects/foo": {
+ "test.cfg": "foo",
+ },
+ }
+
+ var be backend.B
+ be = &client.Backend{
+ Provider: &testconfig.Provider{
+ Base: memconfig.New(configDB),
+ },
+ }
+ be = Backend(be, time.Minute)
+ c = backend.WithBackend(c, be)
+
+ Convey(`Pulling items from memcache`, func() {
+ var content string
+
+ Convey(`Should be able to fetch a config as service`, func() {
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, "foo")
+ So(getMCStats(c).Misses, ShouldEqual, 1)
+ So(getMCStats(c).Hits, ShouldEqual, 0)
+ So(getMCStats(c).Items, ShouldEqual, 1)
+
+ // A second fetch hits memcache.
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, "foo")
+ So(getMCStats(c).Misses, ShouldEqual, 1)
+ So(getMCStats(c).Hits, ShouldEqual, 1)
+ So(getMCStats(c).Items, ShouldEqual, 1)
+
+ // Memcache expires, full reload.
+ clk.Add(time.Minute)
+
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, "foo")
+ So(getMCStats(c).Misses, ShouldEqual, 2)
+ So(getMCStats(c).Hits, ShouldEqual, 1)
+ So(getMCStats(c).Items, ShouldEqual, 1)
+ })
+
+ Convey(`When memcache is broken`, func() {
+ testErr := errors.New("test error")
+ mcFB.BreakFeatures(testErr, "GetMulti")
+
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, "foo")
+ So(getMCStats(c).Misses, ShouldEqual, 0)
+ So(getMCStats(c).Hits, ShouldEqual, 0)
+ So(getMCStats(c).Items, ShouldEqual, 0)
+ })
+
+ Convey(`When a cached entry is corrupted`, func() {
+ cacheKey := caching.Key{
+ Schema: caching.Schema,
+ Op: caching.OpGet,
+ ConfigSet: "projects/foo",
+ Path: "test.cfg",
+ Content: true,
+ Authority: backend.AsService,
+ }
+ So(mc.Set(c, mc.NewItem(c, memcacheKey(&cacheKey)).SetValue([]byte("!!! trash !!!"))), ShouldBeNil)
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, "foo")
+ So(getMCStats(c).Misses, ShouldEqual, 0)
+ So(getMCStats(c).Hits, ShouldEqual, 1)
+ So(getMCStats(c).Items, ShouldEqual, 1)
+ })
+
+ Convey(`Will skip the cache if an item is too large.`, func() {
+ // We need to use a pseudo-random string b/c we compress it.
+ buf := make([]byte, maxMemCacheSize*2)
+ rng := rand.New(rand.NewSource(0))
+ _, _ = rng.Read(buf)
+
+ orig := string(buf)
+ configDB["projects/foo"]["test.cfg"] = orig
+
+ So(cfgclient.Get(c, cfgclient.AsService, "projects/foo", "test.cfg", cfgclient.String(&content), nil), ShouldBeNil)
+ So(content, ShouldEqual, orig)
+ So(getMCStats(c).Misses, ShouldEqual, 1)
+ So(getMCStats(c).Hits, ShouldEqual, 0)
+ So(getMCStats(c).Items, ShouldEqual, 0)
+ })
+ })
+ })
+}
« no previous file with comments | « luci_config/appengine/backend/memcache/cache.go ('k') | luci_config/appengine/format/context.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698