| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package gaeconfig | |
| 6 | |
| 7 import ( | |
| 8 "testing" | |
| 9 "time" | |
| 10 | |
| 11 "golang.org/x/net/context" | |
| 12 | |
| 13 "github.com/luci/gae/filter/featureBreaker" | |
| 14 "github.com/luci/gae/impl/memory" | |
| 15 mc "github.com/luci/gae/service/memcache" | |
| 16 "github.com/luci/luci-go/common/clock/testclock" | |
| 17 "github.com/luci/luci-go/common/data/caching/proccache" | |
| 18 | |
| 19 . "github.com/luci/luci-go/common/testing/assertions" | |
| 20 . "github.com/smartystreets/goconvey/convey" | |
| 21 ) | |
| 22 | |
| 23 func TestCache(t *testing.T) { | |
| 24 t.Parallel() | |
| 25 | |
| 26 Convey("Test cache", t, func() { | |
| 27 c := context.Background() | |
| 28 c = memory.Use(c) | |
| 29 | |
| 30 pc := &proccache.Cache{} | |
| 31 c = proccache.Use(c, pc) | |
| 32 c, clk := testclock.UseTime(c, testclock.TestTimeUTC) | |
| 33 _ = clk | |
| 34 | |
| 35 c, mcFB := featureBreaker.FilterMC(c, nil) | |
| 36 | |
| 37 cache := &cache{} | |
| 38 | |
| 39 Convey("Should be able to store stuff", func() { | |
| 40 Convey("memcache+proccache", func() { | |
| 41 cache.Store(c, "item", time.Second, []byte("foob
ar")) | |
| 42 itm, err := mc.GetKey(c, string(cacheKey("item")
)) | |
| 43 So(err, ShouldBeNil) | |
| 44 So(itm.Value(), ShouldResemble, []byte("\xff\xf4
\xea\x9c\xf7\xd8\xde\xdc\x01foobar")) | |
| 45 val, ok := proccache.Get(c, cacheKey("item")) | |
| 46 So(ok, ShouldBeTrue) | |
| 47 So(val, ShouldResemble, []byte("foobar")) | |
| 48 | |
| 49 Convey("and get it back", func() { | |
| 50 So(cache.Retrieve(c, "item"), ShouldRese
mble, []byte("foobar")) | |
| 51 | |
| 52 Convey("unless it's expired", func() { | |
| 53 clk.Add(time.Second * 2) | |
| 54 So(cache.Retrieve(c, "item"), Sh
ouldBeNil) | |
| 55 }) | |
| 56 | |
| 57 Convey("when missing from proccache", fu
nc() { | |
| 58 proccache.GetCache(c).Mutate(cac
heKey("item"), func(*proccache.Entry) *proccache.Entry { | |
| 59 return nil | |
| 60 }) | |
| 61 So(cache.Retrieve(c, "item"), Sh
ouldResemble, []byte("foobar")) | |
| 62 }) | |
| 63 | |
| 64 Convey("unless memcache is corrupt", fun
c() { | |
| 65 proccache.GetCache(c).Mutate(cac
heKey("item"), func(*proccache.Entry) *proccache.Entry { | |
| 66 return nil | |
| 67 }) | |
| 68 err := mc.Set(c, mc.NewItem(c, s
tring(cacheKey("item"))).SetValue([]byte("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x
ff\xff"))) | |
| 69 So(err, ShouldBeNil) | |
| 70 | |
| 71 So(cache.Retrieve(c, "item"), Sh
ouldBeNil) | |
| 72 }) | |
| 73 | |
| 74 }) | |
| 75 }) | |
| 76 | |
| 77 Convey("if memcache is down", func() { | |
| 78 mcFB.BreakFeatures(nil, "SetMulti") | |
| 79 | |
| 80 cache.Store(c, "item", time.Second, []byte("foob
ar")) | |
| 81 _, err := mc.GetKey(c, string(cacheKey("item"))) | |
| 82 So(err, ShouldErrLike, mc.ErrCacheMiss) | |
| 83 | |
| 84 val, ok := proccache.Get(c, cacheKey("item")) | |
| 85 So(ok, ShouldBeTrue) | |
| 86 So(val, ShouldResemble, []byte("foobar")) | |
| 87 | |
| 88 Convey("and still get it back", func() { | |
| 89 So(cache.Retrieve(c, "item"), ShouldRese
mble, []byte("foobar")) | |
| 90 | |
| 91 Convey("unless it's expired", func() { | |
| 92 clk.Add(time.Second * 2) | |
| 93 So(cache.Retrieve(c, "item"), Sh
ouldBeNil) | |
| 94 }) | |
| 95 }) | |
| 96 }) | |
| 97 }) | |
| 98 }) | |
| 99 } | |
| OLD | NEW |