summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-07-08 21:51:46 -0700
committerWolfy-J <[email protected]>2018-07-08 21:51:46 -0700
commitc691f2b3e91c00d29201f06253b845f59e66959d (patch)
treea6dd94125619c214ab2fa4b39ed2060f5e75378e
parent74a9664f7df21ef26d0718aa2f8239a0562566c6 (diff)
more tests
-rw-r--r--service/container.go2
-rw-r--r--service/container_test.go64
2 files changed, 63 insertions, 3 deletions
diff --git a/service/container.go b/service/container.go
index 7eff4551..96f2d546 100644
--- a/service/container.go
+++ b/service/container.go
@@ -216,7 +216,7 @@ func (c *container) initService(s interface{}, segment Config) (bool, error) {
m, ok := r.MethodByName("Init")
if !ok {
// no Init method is presented, assuming service does not need initialization.
- return false, nil
+ return true, nil
}
if err := c.verifySignature(m); err != nil {
diff --git a/service/container_test.go b/service/container_test.go
index fbafe809..8cb97c74 100644
--- a/service/container_test.go
+++ b/service/container_test.go
@@ -66,7 +66,7 @@ func (t *testService) setChan(c chan interface{}) {
type testCfg struct{ cfg string }
func (cfg *testCfg) Get(name string) Config {
- vars := make(map[string]string)
+ vars := make(map[string]interface{})
json.Unmarshal([]byte(cfg.cfg), &vars)
v, ok := vars[name]
@@ -74,7 +74,8 @@ func (cfg *testCfg) Get(name string) Config {
return nil
}
- return &testCfg{cfg: v}
+ d, _ := json.Marshal(v)
+ return &testCfg{cfg: string(d)}
}
func (cfg *testCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) }
@@ -369,3 +370,62 @@ func TestContainer_NoInit(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{`{"test":"something", "test2":"something-else"}`}))
}
+
+type testInitD struct {
+ c *testInitC
+}
+
+type DCfg struct {
+ V string
+}
+
+// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
+func (c *DCfg) Hydrate(cfg Config) error {
+ if err := cfg.Unmarshal(c); err != nil {
+ return err
+ }
+
+ if c.V == "fail" {
+ return errors.New("failed config")
+ }
+
+ return nil
+}
+
+func (t *testInitD) Init(r *testInitC, c Container, cfg *DCfg) (bool, error) {
+ if r == nil {
+ return false, errors.New("unable to find testInitC")
+ }
+
+ if c == nil {
+ return false, errors.New("unable to find Container")
+ }
+
+ if cfg.V != "ok" {
+ return false, errors.New("invalid config")
+ }
+
+ return false, nil
+}
+
+func TestContainer_InitDependency(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := NewContainer(logger)
+ c.Register("test", &testInitC{})
+ c.Register("test2", &testInitD{})
+
+ assert.NoError(t, c.Init(&testCfg{`{"test":"something", "test2":{"v":"ok"}}`}))
+}
+
+func TestContainer_InitDependencyFail(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := NewContainer(logger)
+ c.Register("test", &testInitC{})
+ c.Register("test2", &testInitD{})
+
+ assert.Error(t, c.Init(&testCfg{`{"test":"something", "test2":{"v":"fail"}}`}))
+}