| 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 caching |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 "time" |
| 10 |
| 11 "github.com/luci/luci-go/common/clock/testclock" |
| 12 "github.com/luci/luci-go/common/config/impl/memory" |
| 13 "github.com/luci/luci-go/common/data/caching/proccache" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 15 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 16 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/client" |
| 17 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" |
| 18 |
| 19 "golang.org/x/net/context" |
| 20 |
| 21 //. "github.com/luci/luci-go/common/testing/assertions" |
| 22 . "github.com/smartystreets/goconvey/convey" |
| 23 ) |
| 24 |
| 25 func TestProcCache(t *testing.T) { |
| 26 t.Parallel() |
| 27 |
| 28 Convey(`A testing setup`, t, func() { |
| 29 c := context.Background() |
| 30 c, tc := testclock.UseTime(c, testclock.TestTimeLocal) |
| 31 |
| 32 mbase := map[string]memory.ConfigSet{ |
| 33 "projects/foo": { |
| 34 "file": "content", |
| 35 }, |
| 36 } |
| 37 |
| 38 var pc proccache.Cache |
| 39 c = proccache.Use(c, &pc) |
| 40 |
| 41 // Install our backend: memory backed by cache backed by force e
rror. |
| 42 var be backend.B |
| 43 lcp := testconfig.Provider{ |
| 44 Base: memory.New(mbase), |
| 45 } |
| 46 be = &client.Backend{ |
| 47 Provider: &lcp, |
| 48 } |
| 49 be = ProcCache(be, time.Hour) |
| 50 c = backend.WithBackend(c, be) |
| 51 |
| 52 Convey(`Will store and cache a value.`, func() { |
| 53 var s string |
| 54 So(cfgclient.Get(c, cfgclient.AsService, "projects/foo",
"file", cfgclient.String(&s), nil), ShouldBeNil) |
| 55 So(s, ShouldEqual, "content") |
| 56 |
| 57 delete(mbase, "projects/foo") |
| 58 |
| 59 // Cached. |
| 60 So(cfgclient.Get(c, cfgclient.AsService, "projects/foo",
"file", cfgclient.String(&s), nil), ShouldBeNil) |
| 61 So(s, ShouldEqual, "content") |
| 62 |
| 63 // Expires, records lack of config. |
| 64 tc.Add(time.Hour) |
| 65 |
| 66 So(cfgclient.Get(c, cfgclient.AsService, "projects/foo",
"file", cfgclient.String(&s), nil), |
| 67 ShouldEqual, cfgclient.ErrNoConfig) |
| 68 |
| 69 // Re-add, still no config. |
| 70 mbase["projects/foo"] = memory.ConfigSet{ |
| 71 "file": "content", |
| 72 } |
| 73 So(cfgclient.Get(c, cfgclient.AsService, "projects/foo",
"file", cfgclient.String(&s), nil), |
| 74 ShouldEqual, cfgclient.ErrNoConfig) |
| 75 |
| 76 // "No config" expires, config is back. |
| 77 tc.Add(time.Hour) |
| 78 So(cfgclient.Get(c, cfgclient.AsService, "projects/foo",
"file", cfgclient.String(&s), nil), ShouldBeNil) |
| 79 So(s, ShouldEqual, "content") |
| 80 }) |
| 81 }) |
| 82 } |
| OLD | NEW |