| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package config |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 "time" |
| 10 |
| 11 "github.com/golang/protobuf/proto" |
| 12 "github.com/luci/luci-go/common/clock" |
| 13 "github.com/luci/luci-go/common/clock/testclock" |
| 14 "github.com/luci/luci-go/common/config" |
| 15 "github.com/luci/luci-go/common/config/impl/memory" |
| 16 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| 17 "golang.org/x/net/context" |
| 18 |
| 19 . "github.com/luci/luci-go/common/testing/assertions" |
| 20 . "github.com/smartystreets/goconvey/convey" |
| 21 ) |
| 22 |
| 23 func TestConfig(t *testing.T) { |
| 24 t.Parallel() |
| 25 |
| 26 Convey(`Using in-memory configuration manager options`, t, func() { |
| 27 c := context.Background() |
| 28 c, tc := testclock.UseTime(c, testclock.TestTimeLocal) |
| 29 |
| 30 // In-memory configuration service. |
| 31 cfg := &svcconfig.Config{ |
| 32 Transport: &svcconfig.Transport{ |
| 33 Type: &svcconfig.Transport_Pubsub{ |
| 34 Pubsub: &svcconfig.Transport_PubSub{ |
| 35 Project: "foo", |
| 36 Topic: "bar", |
| 37 Subscription: "baz", |
| 38 }, |
| 39 }, |
| 40 }, |
| 41 } |
| 42 cset := memory.ConfigSet{ |
| 43 "test-configuration.cfg": proto.MarshalTextString(cfg), |
| 44 } |
| 45 o := Options{ |
| 46 Config: memory.New(map[string]memory.ConfigSet{ |
| 47 "svcconfig/logdog/test": cset, |
| 48 }), |
| 49 ConfigSet: "svcconfig/logdog/test", |
| 50 ConfigPath: "test-configuration.cfg", |
| 51 } |
| 52 |
| 53 Convey(`Will fail to create a Manager if the configuration does
not exist.`, func() { |
| 54 o.ConfigPath = "nonexistent.cfg" |
| 55 |
| 56 _, err := NewManager(c, o) |
| 57 So(err, ShouldEqual, config.ErrNoConfig) |
| 58 }) |
| 59 |
| 60 Convey(`Will fail to create a Manager if the configuration is an
invalid protobuf.`, func() { |
| 61 cset[o.ConfigPath] = "not a valid text protobuf" |
| 62 |
| 63 _, err := NewManager(c, o) |
| 64 So(err, ShouldNotBeNil) |
| 65 }) |
| 66 |
| 67 Convey(`Can create a Manager.`, func() { |
| 68 m, err := NewManager(c, o) |
| 69 So(err, ShouldBeNil) |
| 70 So(m.Config(), ShouldResembleV, cfg) |
| 71 }) |
| 72 |
| 73 Convey(`With a kill function installed`, func() { |
| 74 killedC := make(chan bool, 1) |
| 75 o.KillCheckInterval = time.Second |
| 76 o.KillFunc = func() { |
| 77 killedC <- true |
| 78 } |
| 79 |
| 80 c, cancelFunc := context.WithCancel(c) |
| 81 defer cancelFunc() |
| 82 |
| 83 timeAdvanceC := make(chan time.Duration) |
| 84 tc.SetTimerCallback(func(time.Duration, clock.Timer) { |
| 85 tc.Add(<-timeAdvanceC) |
| 86 }) |
| 87 |
| 88 m, err := NewManager(c, o) |
| 89 So(err, ShouldBeNil) |
| 90 |
| 91 Convey(`When the configuration changes`, func() { |
| 92 cfg.Transport.GetPubsub().Project = "qux" |
| 93 cset[o.ConfigPath] = proto.MarshalTextString(cfg
) |
| 94 |
| 95 Convey(`Will execute the kill function if the co
nfiguration changes.`, func() { |
| 96 timeAdvanceC <- time.Second |
| 97 So(<-killedC, ShouldBeTrue) |
| 98 time.Sleep(10) |
| 99 }) |
| 100 }) |
| 101 |
| 102 Convey(`Will do nothing if the configuration doesn't cha
nge.`, func() { |
| 103 // Advancing time twice ensures that the poll lo
op has processed at |
| 104 // least one non-changing reload. |
| 105 timeAdvanceC <- time.Second |
| 106 timeAdvanceC <- time.Second |
| 107 |
| 108 So(m.Config(), ShouldResembleV, cfg) |
| 109 }) |
| 110 }) |
| 111 }) |
| 112 } |
| OLD | NEW |