diff options
author | Wolfy-J <[email protected]> | 2018-06-10 13:10:11 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-10 13:10:11 +0300 |
commit | d4c007859c002e6af540ec516929de81d03ff82d (patch) | |
tree | d7115bded0ae58a08dfe84d8a2cfcdb32c347132 | |
parent | 2f4e1d950339076574edc50044d6f1ebc3a83a01 (diff) |
no immutable services
-rw-r--r-- | rpc/service.go | 19 | ||||
-rw-r--r-- | rpc/service_test.go | 53 | ||||
-rw-r--r-- | service/container.go (renamed from service/registry.go) | 28 | ||||
-rw-r--r-- | service/container_test.go (renamed from service/registry_test.go) | 0 |
4 files changed, 61 insertions, 39 deletions
diff --git a/rpc/service.go b/rpc/service.go index c07c70b9..268087bc 100644 --- a/rpc/service.go +++ b/rpc/service.go @@ -20,19 +20,26 @@ type Service struct { serving bool } -// WithConfig must return Service instance configured with the given environment. Must return error in case of -// misconfiguration, might return nil as Service if Service is not enabled. -func (s *Service) WithConfig(cfg service.Config, reg service.Registry) (service.Service, error) { +// Configure must return configure service and return true if service is enabled. Must return error in case of +// misconfiguration. +func (s *Service) Configure(cfg service.Config, reg service.Container) (enabled bool, err error) { + if s.cfg != nil { + return true, errors.New("service is already configured") + } + config := &config{} if err := cfg.Unmarshal(config); err != nil { - return nil, err + return false, err } if !config.Enable { - return nil, nil + return false, nil } - return &Service{cfg: config, rpc: rpc.NewServer()}, nil + s.cfg = config + s.rpc = rpc.NewServer() + + return true, nil } // Serve serves Service. diff --git a/rpc/service_test.go b/rpc/service_test.go index 109a5df8..13dd4930 100644 --- a/rpc/service_test.go +++ b/rpc/service_test.go @@ -17,17 +17,19 @@ func (cfg *testCfg) Get(name string) service.Config { return nil } func (cfg *testCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) } func Test_ConfigError(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":false`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":false`}, nil) assert.Error(t, err) - assert.Nil(t, s) + assert.False(t, ok) } func Test_Disabled(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":false}`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":false}`}, nil) assert.NoError(t, err) - assert.Nil(t, s) + assert.False(t, ok) } func Test_RegisterNotConfigured(t *testing.T) { @@ -41,47 +43,60 @@ func Test_RegisterNotConfigured(t *testing.T) { } func Test_Enabled(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + + assert.NoError(t, err) + assert.True(t, ok) +} + +func Test_TwoConfigurations(t *testing.T) { + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) assert.NoError(t, err) - assert.NotNil(t, s) - assert.IsType(t, &Service{}, s) + assert.True(t, ok) + + ok, err = s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + assert.Error(t, err) + assert.True(t, ok) } func Test_StopNonServing(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) assert.NoError(t, err) - assert.NotNil(t, s) - assert.IsType(t, &Service{}, s) + assert.True(t, ok) s.Stop() } func Test_Serve_Errors(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"mailformed"}`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"mailformed"}`}, nil) assert.NoError(t, err) - assert.NotNil(t, s) - assert.IsType(t, &Service{}, s) + assert.True(t, ok) assert.Error(t, s.Serve()) - client, err := s.(*Service).Client() + client, err := s.Client() assert.Nil(t, client) assert.Error(t, err) } func Test_Serve_Client(t *testing.T) { - s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + s := &Service{} + ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) assert.NoError(t, err) - assert.NotNil(t, s) - assert.IsType(t, &Service{}, s) + assert.True(t, ok) + defer s.Stop() - assert.NoError(t, s.(*Service).Register("test", &testService{})) + assert.NoError(t, s.Register("test", &testService{})) go func() { assert.NoError(t, s.Serve()) }() - client, err := s.(*Service).Client() + client, err := s.Client() assert.NotNil(t, client) assert.NoError(t, err) defer client.Close() diff --git a/service/registry.go b/service/container.go index f94d4171..fb1d6874 100644 --- a/service/registry.go +++ b/service/container.go @@ -17,8 +17,8 @@ type Config interface { Unmarshal(out interface{}) error } -// Registry controls all internal RR services and provides plugin based system. -type Registry interface { +// Container controls all internal RR services and provides plugin based system. +type Container interface { // Register add new service to the registry under given name. Register(name string, service Service) @@ -40,9 +40,9 @@ type Registry interface { // Service provides high level functionality for road runner Service. type Service interface { - // WithConfig must return Service instance configured with the given environment. Must return error in case of - // misconfiguration, might return nil as Service if Service is not enabled. - WithConfig(cfg Config, reg Registry) (Service, error) + // Configure must return configure service and return true if service is enabled. Must return error in case of + // misconfiguration. Services must not be used without proper configuration pushed first. + Configure(cfg Config, reg Container) (enabled bool, err error) // Serve serves Service. Serve() error @@ -96,7 +96,7 @@ func (e *entry) setStopped() { } // NewRegistry creates new registry. -func NewRegistry(log logrus.FieldLogger) Registry { +func NewRegistry(log logrus.FieldLogger) Container { return ®istry{ log: log, candidates: make([]*entry, 0), @@ -131,18 +131,18 @@ func (r *registry) Configure(cfg Config) error { continue } - s, err := e.Service.WithConfig(segment, r) + _, err := e.Service.Configure(segment, r) if err != nil { return errors.Wrap(err, fmt.Sprintf("%s.service", e.Name)) } - if s != nil { - r.configured = append(r.configured, &entry{ - Name: e.Name, - Service: s, - serving: false, - }) - } + //if s != nil { + // r.configured = append(r.configured, &entry{ + // Name: e.Name, + // Service: s, + // serving: false, + // }) + //} } return nil diff --git a/service/registry_test.go b/service/container_test.go index 6d43c336..6d43c336 100644 --- a/service/registry_test.go +++ b/service/container_test.go |