| 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 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/logdog/api/config/svcconfig" |  | 
|   17         "golang.org/x/net/context" |  | 
|   18  |  | 
|   19         . "github.com/smartystreets/goconvey/convey" |  | 
|   20 ) |  | 
|   21  |  | 
|   22 func TestConfig(t *testing.T) { |  | 
|   23         t.Parallel() |  | 
|   24  |  | 
|   25         Convey(`Using in-memory configuration manager options`, t, func() { |  | 
|   26                 c := context.Background() |  | 
|   27                 c, tc := testclock.UseTime(c, testclock.TestTimeLocal) |  | 
|   28  |  | 
|   29                 // In-memory configuration service. |  | 
|   30                 cfg := &svcconfig.Config{ |  | 
|   31                         Transport: &svcconfig.Transport{ |  | 
|   32                                 Type: &svcconfig.Transport_Pubsub{ |  | 
|   33                                         Pubsub: &svcconfig.Transport_PubSub{ |  | 
|   34                                                 Project:      "foo", |  | 
|   35                                                 Topic:        "bar", |  | 
|   36                                                 Subscription: "baz", |  | 
|   37                                         }, |  | 
|   38                                 }, |  | 
|   39                         }, |  | 
|   40                 } |  | 
|   41                 cset := memory.ConfigSet{ |  | 
|   42                         "test-configuration.cfg": proto.MarshalTextString(cfg), |  | 
|   43                 } |  | 
|   44                 o := Options{ |  | 
|   45                         Config: memory.New(map[string]memory.ConfigSet{ |  | 
|   46                                 "svcconfig/logdog/test": cset, |  | 
|   47                         }), |  | 
|   48                         ConfigSet:         "svcconfig/logdog/test", |  | 
|   49                         ServiceConfigPath: "test-configuration.cfg", |  | 
|   50                 } |  | 
|   51  |  | 
|   52                 Convey(`Will fail to create a Manager if the configuration does 
     not exist.`, func() { |  | 
|   53                         o.ServiceConfigPath = "nonexistent.cfg" |  | 
|   54  |  | 
|   55                         _, err := NewManager(c, o) |  | 
|   56                         So(err, ShouldEqual, config.ErrNoConfig) |  | 
|   57                 }) |  | 
|   58  |  | 
|   59                 Convey(`Will fail to create a Manager if the configuration is an
      invalid protobuf.`, func() { |  | 
|   60                         cset[o.ServiceConfigPath] = "not a valid text protobuf" |  | 
|   61  |  | 
|   62                         _, err := NewManager(c, o) |  | 
|   63                         So(err, ShouldNotBeNil) |  | 
|   64                 }) |  | 
|   65  |  | 
|   66                 Convey(`Can create a Manager.`, func() { |  | 
|   67                         m, err := NewManager(c, o) |  | 
|   68                         So(err, ShouldBeNil) |  | 
|   69                         defer m.Close() |  | 
|   70  |  | 
|   71                         So(m.Config(), ShouldResemble, cfg) |  | 
|   72                 }) |  | 
|   73  |  | 
|   74                 Convey(`With a kill function installed`, func() { |  | 
|   75                         killedC := make(chan bool, 1) |  | 
|   76                         o.KillCheckInterval = time.Second |  | 
|   77                         o.KillFunc = func() { |  | 
|   78                                 killedC <- true |  | 
|   79                         } |  | 
|   80  |  | 
|   81                         c, cancelFunc := context.WithCancel(c) |  | 
|   82                         defer cancelFunc() |  | 
|   83  |  | 
|   84                         timeAdvanceC := make(chan time.Duration) |  | 
|   85                         tc.SetTimerCallback(func(time.Duration, clock.Timer) { |  | 
|   86                                 t, ok := <-timeAdvanceC |  | 
|   87                                 if ok { |  | 
|   88                                         tc.Add(t) |  | 
|   89                                 } |  | 
|   90                         }) |  | 
|   91  |  | 
|   92                         m, err := NewManager(c, o) |  | 
|   93                         So(err, ShouldBeNil) |  | 
|   94                         defer m.Close() |  | 
|   95  |  | 
|   96                         // Unblock any timer callbacks. |  | 
|   97                         defer close(timeAdvanceC) |  | 
|   98  |  | 
|   99                         Convey(`When the configuration changes`, func() { |  | 
|  100                                 cfg.Transport.GetPubsub().Project = "qux" |  | 
|  101                                 cset[o.ServiceConfigPath] = proto.MarshalTextStr
     ing(cfg) |  | 
|  102  |  | 
|  103                                 Convey(`Will execute the kill function if the co
     nfiguration changes.`, func() { |  | 
|  104                                         timeAdvanceC <- time.Second |  | 
|  105                                         So(<-killedC, ShouldBeTrue) |  | 
|  106                                         time.Sleep(10) |  | 
|  107                                 }) |  | 
|  108                         }) |  | 
|  109  |  | 
|  110                         Convey(`Will do nothing if the configuration doesn't cha
     nge.`, func() { |  | 
|  111                                 // Advancing time twice ensures that the poll lo
     op has processed at |  | 
|  112                                 // least one non-changing reload. |  | 
|  113                                 timeAdvanceC <- time.Second |  | 
|  114                                 timeAdvanceC <- time.Second |  | 
|  115  |  | 
|  116                                 So(m.Config(), ShouldResemble, cfg) |  | 
|  117                         }) |  | 
|  118                 }) |  | 
|  119         }) |  | 
|  120 } |  | 
| OLD | NEW |