| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 coordinator |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 "time" |
| 10 |
| 11 "github.com/luci/luci-go/common/clock/testclock" |
| 12 "github.com/luci/luci-go/common/errors" |
| 13 "github.com/luci/luci-go/logdog/common/storage/caching" |
| 14 |
| 15 "github.com/luci/gae/filter/featureBreaker" |
| 16 "github.com/luci/gae/impl/memory" |
| 17 "github.com/luci/gae/service/memcache" |
| 18 |
| 19 "golang.org/x/net/context" |
| 20 |
| 21 . "github.com/smartystreets/goconvey/convey" |
| 22 ) |
| 23 |
| 24 func testStorageCache(t *testing.T, compress bool) { |
| 25 t.Parallel() |
| 26 |
| 27 cloneItemsWithData := func(src []*caching.Item, data []byte) []*caching.
Item { |
| 28 dst := make([]*caching.Item, len(src)) |
| 29 for i, itm := range src { |
| 30 clone := *itm |
| 31 clone.Data = data |
| 32 dst[i] = &clone |
| 33 } |
| 34 return dst |
| 35 } |
| 36 |
| 37 Convey(`Testing storage cache in a testing envrionment`, t, func() { |
| 38 c := memory.Use(context.Background()) |
| 39 c, tc := testclock.UseTime(c, testclock.TestTimeLocal) |
| 40 c, fb := featureBreaker.FilterMC(c, nil) |
| 41 |
| 42 var cache StorageCache |
| 43 if compress { |
| 44 cache.compressionThreshold = 1 |
| 45 } |
| 46 |
| 47 items := []*caching.Item{ |
| 48 {Schema: "test", Type: "type", Key: "foo", Data: []byte(
"foo")}, |
| 49 {Schema: "test", Type: "type", Key: "bar", Data: []byte(
"bar")}, |
| 50 {Schema: "test", Type: "othertype", Key: "foo", Data: []
byte("foo2")}, |
| 51 {Schema: "otherschema", Type: "othertype", Key: "foo", D
ata: []byte("foo3")}, |
| 52 } |
| 53 |
| 54 Convey(`Can load those items into cache`, func() { |
| 55 cache.Put(c, time.Minute, items...) |
| 56 |
| 57 stats, err := memcache.Stats(c) |
| 58 So(err, ShouldBeNil) |
| 59 So(stats.Items, ShouldEqual, 4) |
| 60 |
| 61 Convey(`And retrieve those items from cache.`, func() { |
| 62 oit := cloneItemsWithData(items, []byte("junk")) |
| 63 cache.Get(c, oit...) |
| 64 So(oit, ShouldResemble, items) |
| 65 }) |
| 66 |
| 67 Convey(`Returns nil data if memcache.GetMulti is broken.
`, func() { |
| 68 fb.BreakFeatures(errors.New("test error"), "GetM
ulti") |
| 69 |
| 70 cache.Get(c, items...) |
| 71 So(items, ShouldResemble, cloneItemsWithData(ite
ms, nil)) |
| 72 }) |
| 73 }) |
| 74 |
| 75 Convey(`Does not load items into cache if memcache.SetMulti is b
roken.`, func() { |
| 76 fb.BreakFeatures(errors.New("test error"), "SetMulti") |
| 77 |
| 78 cache.Put(c, time.Minute, items...) |
| 79 cache.Get(c, items...) |
| 80 So(items, ShouldResemble, cloneItemsWithData(items, nil)
) |
| 81 }) |
| 82 |
| 83 Convey(`Get on missing item returns nil data.`, func() { |
| 84 cache.Get(c, items...) |
| 85 So(items, ShouldResemble, cloneItemsWithData(items, nil)
) |
| 86 }) |
| 87 |
| 88 Convey(`Will replace existing item value.`, func() { |
| 89 cache.Put(c, time.Minute, items...) |
| 90 |
| 91 other := cloneItemsWithData(items, []byte("ohaithere")) |
| 92 cache.Put(c, time.Minute, other...) |
| 93 |
| 94 cache.Get(c, items...) |
| 95 So(items, ShouldResemble, other) |
| 96 }) |
| 97 |
| 98 Convey(`Applies expiration (or lack thereof).`, func() { |
| 99 cache.Put(c, time.Minute, items[0]) |
| 100 cache.Put(c, 0, items[1]) |
| 101 |
| 102 // items[0] |
| 103 itm, err := memcache.GetKey(c, cache.mkCacheKey(items[0]
)) |
| 104 So(err, ShouldBeNil) |
| 105 So(itm.Key(), ShouldEqual, cache.mkCacheKey(items[0])) |
| 106 So(len(itm.Value()), ShouldNotEqual, 0) |
| 107 |
| 108 tc.Add(time.Minute + 1) // Expires items[0]. |
| 109 _, err = memcache.GetKey(c, cache.mkCacheKey(items[0])) |
| 110 So(err, ShouldEqual, memcache.ErrCacheMiss) |
| 111 |
| 112 // items[1] |
| 113 itm, err = memcache.GetKey(c, cache.mkCacheKey(items[1])
) |
| 114 So(err, ShouldBeNil) |
| 115 So(itm.Key(), ShouldEqual, cache.mkCacheKey(items[1])) |
| 116 So(len(itm.Value()), ShouldNotEqual, 0) |
| 117 }) |
| 118 }) |
| 119 } |
| 120 |
| 121 func TestStorageCacheWithoutCompression(t *testing.T) { |
| 122 testStorageCache(t, false) |
| 123 } |
| 124 |
| 125 func TestStorageCacheWithCompression(t *testing.T) { |
| 126 testStorageCache(t, true) |
| 127 } |
| OLD | NEW |