summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-09-17 13:35:48 +0300
committerValery Piashchynski <[email protected]>2020-09-17 13:35:48 +0300
commit08d6b6b7f773f83b286cd48c1a0fbec9a62fb42b (patch)
treeafd077d7a3a22ed0999773f861d4c48d68e26b68 /service
parent0af502835616774a8b306596e4d1a8c5843b6bc8 (diff)
Move old container to the _old folder
Diffstat (limited to 'service')
-rw-r--r--service/container.go371
-rw-r--r--service/container_test.go533
-rw-r--r--service/entry.go59
-rw-r--r--service/entry_test.go16
-rw-r--r--service/rpc/config.go5
5 files changed, 3 insertions, 981 deletions
diff --git a/service/container.go b/service/container.go
deleted file mode 100644
index 77a6dfc0..00000000
--- a/service/container.go
+++ /dev/null
@@ -1,371 +0,0 @@
-package service
-
-import (
- "fmt"
- "github.com/pkg/errors"
- "github.com/sirupsen/logrus"
- "reflect"
- "sync"
-)
-
-var errNoConfig = fmt.Errorf("no config has been provided")
-var errTempFix223 = fmt.Errorf("temporary error for fix #223") // meant no error here, just shutdown the server
-
-// InitMethod contains name of the method to be automatically invoked while service initialization. Must return
-// (bool, error). Container can be requested as well. Config can be requested in a form
-// of service.Config or pointer to service specific config struct (automatically unmarshalled), config argument must
-// implement service.HydrateConfig.
-const InitMethod = "Init"
-
-// Service can serve. Services can provide Init method which must return (bool, error) signature and might accept
-// other services and/or configs as dependency.
-type Service interface {
- // Serve serves.
- Serve() error
-
- // Detach stops the service.
- Stop()
-}
-
-// Container controls all internal RR services and provides plugin based system.
-type Container interface {
- // Register add new service to the container under given name.
- Register(name string, service interface{})
-
- // Reconfigure configures all underlying services with given configuration.
- Init(cfg Config) error
-
- // Check if svc has been registered.
- Has(service string) bool
-
- // get returns svc instance by it's name or nil if svc not found. Method returns current service status
- // as second value.
- Get(service string) (svc interface{}, status int)
-
- // Serve all configured services. Non blocking.
- Serve() error
-
- // Close all active services.
- Stop()
-
- // List service names.
- List() []string
-}
-
-// Config provides ability to slice configuration sections and unmarshal configuration data into
-// given structure.
-type Config interface {
- // get nested config section (sub-map), returns nil if section not found.
- Get(service string) Config
-
- // Unmarshal unmarshal config data into given struct.
- Unmarshal(out interface{}) error
-}
-
-// HydrateConfig provides ability to automatically hydrate config with values using
-// service.Config as the source.
-type HydrateConfig interface {
- // Hydrate must populate config values using given config source.
- // Must return error if config is not valid.
- Hydrate(cfg Config) error
-}
-
-// DefaultsConfig declares ability to be initated without config data provided.
-type DefaultsConfig interface {
- // InitDefaults allows to init blank config with pre-defined set of default values.
- InitDefaults() error
-}
-
-type container struct {
- log logrus.FieldLogger
- mu sync.Mutex
- services []*entry
- errc chan struct {
- name string
- err error
- }
-}
-
-// NewContainer creates new service container.
-func NewContainer(log logrus.FieldLogger) Container {
- return &container{
- log: log,
- services: make([]*entry, 0),
- errc: make(chan struct {
- name string
- err error
- }, 1),
- }
-}
-
-// Register add new service to the container under given name.
-func (c *container) Register(name string, service interface{}) {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- c.services = append(c.services, &entry{
- name: name,
- svc: service,
- status: StatusInactive,
- })
-}
-
-// Check hasStatus svc has been registered.
-func (c *container) Has(target string) bool {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- for _, e := range c.services {
- if e.name == target {
- return true
- }
- }
-
- return false
-}
-
-// get returns svc instance by it's name or nil if svc not found.
-func (c *container) Get(target string) (svc interface{}, status int) {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- for _, e := range c.services {
- if e.name == target {
- return e.svc, e.getStatus()
- }
- }
-
- return nil, StatusUndefined
-}
-
-// Init configures all underlying services with given configuration.
-func (c *container) Init(cfg Config) error {
- for _, e := range c.services {
- if e.getStatus() >= StatusOK {
- return fmt.Errorf("service [%s] has already been configured", e.name)
- }
-
- // inject service dependencies
- if ok, err := c.initService(e.svc, cfg.Get(e.name)); err != nil {
- // soft error (skipping)
- if err == errNoConfig {
- c.log.Debugf("[%s]: disabled", e.name)
- continue
- }
-
- return errors.Wrap(err, fmt.Sprintf("[%s]", e.name))
- } else if ok {
- e.setStatus(StatusOK)
- } else {
- c.log.Debugf("[%s]: disabled", e.name)
- }
- }
-
- return nil
-}
-
-// Serve all configured services. Non blocking.
-func (c *container) Serve() error {
- var running = 0
- for _, e := range c.services {
- if e.hasStatus(StatusOK) && e.canServe() {
- running++
- c.log.Debugf("[%s]: started", e.name)
- go func(e *entry) {
- e.setStatus(StatusServing)
- defer e.setStatus(StatusStopped)
- if err := e.svc.(Service).Serve(); err != nil {
- c.errc <- struct {
- name string
- err error
- }{name: e.name, err: errors.Wrap(err, fmt.Sprintf("[%s]", e.name))}
- } else {
- c.errc <- struct {
- name string
- err error
- }{name: e.name, err: errTempFix223}
- }
- }(e)
- }
- }
-
- // simple handler to handle empty configs
- if running == 0 {
- return nil
- }
-
- for fail := range c.errc {
- if fail.err == errTempFix223 {
- // if we call stop, then stop all plugins
- break
- } else {
- c.log.Errorf("[%s]: %s", fail.name, fail.err)
- c.Stop()
- return fail.err
- }
- }
-
- return nil
-}
-
-// Detach sends stop command to all running services.
-func (c *container) Stop() {
- for _, e := range c.services {
- if e.hasStatus(StatusServing) {
- e.setStatus(StatusStopping)
- e.svc.(Service).Stop()
- e.setStatus(StatusStopped)
-
- c.log.Debugf("[%s]: stopped", e.name)
- }
- }
-}
-
-// List all service names.
-func (c *container) List() []string {
- names := make([]string, 0, len(c.services))
- for _, e := range c.services {
- names = append(names, e.name)
- }
-
- return names
-}
-
-// calls Init method with automatically resolved arguments.
-func (c *container) initService(s interface{}, segment Config) (bool, error) {
- r := reflect.TypeOf(s)
-
- m, ok := r.MethodByName(InitMethod)
- if !ok {
- // no Init method is presented, assuming service does not need initialization.
- return true, nil
- }
-
- if err := c.verifySignature(m); err != nil {
- return false, err
- }
-
- // hydrating
- values, err := c.resolveValues(s, m, segment)
- if err != nil {
- return false, err
- }
-
- // initiating service
- out := m.Func.Call(values)
-
- if out[1].IsNil() {
- return out[0].Bool(), nil
- }
-
- return out[0].Bool(), out[1].Interface().(error)
-}
-
-// resolveValues returns slice of call arguments for service Init method.
-func (c *container) resolveValues(s interface{}, m reflect.Method, cfg Config) (values []reflect.Value, err error) {
- for i := 0; i < m.Type.NumIn(); i++ {
- v := m.Type.In(i)
-
- switch {
- case v.ConvertibleTo(reflect.ValueOf(s).Type()): // service itself
- values = append(values, reflect.ValueOf(s))
-
- case v.Implements(reflect.TypeOf((*Container)(nil)).Elem()): // container
- values = append(values, reflect.ValueOf(c))
-
- case v.Implements(reflect.TypeOf((*logrus.StdLogger)(nil)).Elem()),
- v.Implements(reflect.TypeOf((*logrus.FieldLogger)(nil)).Elem()),
- v.ConvertibleTo(reflect.ValueOf(c.log).Type()): // logger
- values = append(values, reflect.ValueOf(c.log))
-
- case v.Implements(reflect.TypeOf((*HydrateConfig)(nil)).Elem()): // injectable config
- sc := reflect.New(v.Elem())
-
- if dsc, ok := sc.Interface().(DefaultsConfig); ok {
- err := dsc.InitDefaults()
- if err != nil {
- return nil, err
- }
- if cfg == nil {
- values = append(values, sc)
- continue
- }
-
- } else if cfg == nil {
- return nil, errNoConfig
- }
-
- if err := sc.Interface().(HydrateConfig).Hydrate(cfg); err != nil {
- return nil, err
- }
-
- values = append(values, sc)
-
- case v.Implements(reflect.TypeOf((*Config)(nil)).Elem()): // generic config section
- if cfg == nil {
- return nil, errNoConfig
- }
-
- values = append(values, reflect.ValueOf(cfg))
-
- default: // dependency on other service (resolution to nil if service can't be found)
- value, err := c.resolveValue(v)
- if err != nil {
- return nil, err
- }
-
- values = append(values, value)
- }
- }
-
- return
-}
-
-// verifySignature checks if Init method has valid signature
-func (c *container) verifySignature(m reflect.Method) error {
- if m.Type.NumOut() != 2 {
- return fmt.Errorf("method Init must have exact 2 return values")
- }
-
- if m.Type.Out(0).Kind() != reflect.Bool {
- return fmt.Errorf("first return value of Init method must be bool type")
- }
-
- if !m.Type.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
- return fmt.Errorf("second return value of Init method value must be error type")
- }
-
- return nil
-}
-
-func (c *container) resolveValue(v reflect.Type) (reflect.Value, error) {
- value := reflect.Value{}
- for _, e := range c.services {
- if !e.hasStatus(StatusOK) {
- continue
- }
-
- if v.Kind() == reflect.Interface && reflect.TypeOf(e.svc).Implements(v) {
- if value.IsValid() {
- return value, fmt.Errorf("disambiguous dependency `%s`", v)
- }
-
- value = reflect.ValueOf(e.svc)
- }
-
- if v.ConvertibleTo(reflect.ValueOf(e.svc).Type()) {
- if value.IsValid() {
- return value, fmt.Errorf("disambiguous dependency `%s`", v)
- }
-
- value = reflect.ValueOf(e.svc)
- }
- }
-
- if !value.IsValid() {
- // placeholder (make sure to check inside the method)
- value = reflect.New(v).Elem()
- }
-
- return value, nil
-}
diff --git a/service/container_test.go b/service/container_test.go
deleted file mode 100644
index b3ec7054..00000000
--- a/service/container_test.go
+++ /dev/null
@@ -1,533 +0,0 @@
-package service
-
-import (
- "errors"
- json "github.com/json-iterator/go"
- "github.com/sirupsen/logrus"
- "github.com/sirupsen/logrus/hooks/test"
- "github.com/stretchr/testify/assert"
- "sync"
- "testing"
- "time"
-)
-
-type testService struct {
- mu sync.Mutex
- waitForServe chan interface{}
- delay time.Duration
- ok bool
- cfg Config
- c Container
- cfgE, serveE error
- done chan interface{}
-}
-
-func (t *testService) Init(cfg Config, c Container) (enabled bool, err error) {
- t.cfg = cfg
- t.c = c
- t.done = make(chan interface{})
- return t.ok, t.cfgE
-}
-
-func (t *testService) Serve() error {
- time.Sleep(t.delay)
-
- if t.serveE != nil {
- return t.serveE
- }
-
- if c := t.waitChan(); c != nil {
- close(c)
- t.setChan(nil)
- }
-
- <-t.done
- return nil
-}
-
-func (t *testService) Stop() {
- close(t.done)
-}
-
-func (t *testService) waitChan() chan interface{} {
- t.mu.Lock()
- defer t.mu.Unlock()
-
- return t.waitForServe
-}
-
-func (t *testService) setChan(c chan interface{}) {
- t.mu.Lock()
- defer t.mu.Unlock()
-
- t.waitForServe = c
-}
-
-type testCfg struct{ cfg string }
-
-func (cfg *testCfg) Get(name string) Config {
- vars := make(map[string]interface{})
- j := json.ConfigCompatibleWithStandardLibrary
- err := j.Unmarshal([]byte(cfg.cfg), &vars)
- if err != nil {
- panic("error unmarshalling the cfg.cfg value")
- }
-
- v, ok := vars[name]
- if !ok {
- return nil
- }
-
- d, _ := j.Marshal(v)
- return &testCfg{cfg: string(d)}
-}
-func (cfg *testCfg) Unmarshal(out interface{}) error {
- j := json.ConfigCompatibleWithStandardLibrary
- return j.Unmarshal([]byte(cfg.cfg), out)
-}
-
-// Config defines RPC service config.
-type dConfig struct {
- // Indicates if RPC connection is enabled.
- Value string
-}
-
-// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
-func (c *dConfig) Hydrate(cfg Config) error {
- return cfg.Unmarshal(c)
-}
-
-// InitDefaults allows to init blank config with pre-defined set of default values.
-func (c *dConfig) InitDefaults() error {
- c.Value = "default"
-
- return nil
-}
-
-type dService struct {
- Cfg *dConfig
-}
-
-func (s *dService) Init(cfg *dConfig) (bool, error) {
- s.Cfg = cfg
- return true, nil
-}
-
-func TestContainer_Register(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testService{})
-
- assert.Equal(t, 0, len(hook.Entries))
-}
-
-func TestContainer_Has(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testService{})
-
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.True(t, c.Has("test"))
- assert.False(t, c.Has("another"))
-}
-
-func TestContainer_List(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testService{})
-
- assert.Equal(t, 0, len(hook.Entries))
- assert.Equal(t, 1, len(c.List()))
-
- assert.True(t, c.Has("test"))
- assert.False(t, c.Has("another"))
-}
-
-func TestContainer_Get(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testService{})
- assert.Equal(t, 0, len(hook.Entries))
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusInactive, st)
-
- s, st = c.Get("another")
- assert.Nil(t, s)
- assert.Equal(t, StatusUndefined, st)
-}
-
-func TestContainer_Stop_NotStarted(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testService{})
- assert.Equal(t, 0, len(hook.Entries))
-
- c.Stop()
-}
-
-func TestContainer_Configure(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{ok: true}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{"test":"something"}`}))
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusOK, st)
-}
-
-func TestContainer_Init_Default(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &dService{}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{}`}))
-
- s, st := c.Get("test")
- assert.IsType(t, &dService{}, s)
- assert.Equal(t, StatusOK, st)
-
- assert.Equal(t, "default", svc.Cfg.Value)
-}
-
-func TestContainer_Init_Default_Overwrite(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &dService{}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{"test":{"value": "something"}}`}))
-
- s, st := c.Get("test")
- assert.IsType(t, &dService{}, s)
- assert.Equal(t, StatusOK, st)
-
- assert.Equal(t, "something", svc.Cfg.Value)
-}
-
-func TestContainer_ConfigureNull(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{ok: true}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{"another":"something"}`}))
- assert.Equal(t, 1, len(hook.Entries))
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusInactive, st)
-}
-
-func TestContainer_ConfigureDisabled(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{ok: false}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{"test":"something"}`}))
- assert.Equal(t, 1, len(hook.Entries))
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusInactive, st)
-}
-
-func TestContainer_ConfigureError(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{
- ok: false,
- cfgE: errors.New("configure error"),
- }
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- err := c.Init(&testCfg{`{"test":"something"}`})
- assert.Error(t, err)
- assert.Contains(t, err.Error(), "configure error")
- assert.Contains(t, err.Error(), "test")
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusInactive, st)
-}
-
-func TestContainer_ConfigureTwice(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{ok: true}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- assert.NoError(t, c.Init(&testCfg{`{"test":"something"}`}))
- assert.Error(t, c.Init(&testCfg{`{"test":"something"}`}))
-}
-
-// bug #276 test
-func TestContainer_ServeEmptyContainer(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{ok: true}
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
-
- go assert.NoError(t, c.Serve())
-
- time.Sleep(time.Millisecond * 500)
-
- c.Stop()
-}
-
-func TestContainer_Serve(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{
- ok: true,
- waitForServe: make(chan interface{}),
- }
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
- assert.NoError(t, c.Init(&testCfg{`{"test":"something"}`}))
-
- go func() {
- assert.NoError(t, c.Serve())
- }()
-
- <-svc.waitChan()
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusServing, st)
-
- c.Stop()
-
- s, st = c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusStopped, st)
-}
-
-func TestContainer_ServeError(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{
- ok: true,
- waitForServe: make(chan interface{}),
- serveE: errors.New("serve error"),
- }
-
- c := NewContainer(logger)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
- assert.NoError(t, c.Init(&testCfg{`{"test":"something"}`}))
-
- err := c.Serve()
- assert.Error(t, err)
- assert.Contains(t, err.Error(), "serve error")
- assert.Contains(t, err.Error(), "test")
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusStopped, st)
-}
-
-func TestContainer_ServeErrorMultiple(t *testing.T) {
- logger, hook := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- svc := &testService{
- ok: true,
- delay: time.Millisecond * 10,
- waitForServe: make(chan interface{}),
- serveE: errors.New("serve error"),
- }
-
- svc2 := &testService{
- ok: true,
- waitForServe: make(chan interface{}),
- }
-
- c := NewContainer(logger)
- c.Register("test2", svc2)
- c.Register("test", svc)
- assert.Equal(t, 0, len(hook.Entries))
- assert.NoError(t, c.Init(&testCfg{`{"test":"something", "test2":"something-else"}`}))
-
- err := c.Serve()
- assert.Error(t, err)
- assert.Contains(t, err.Error(), "serve error")
- assert.Contains(t, err.Error(), "test")
-
- s, st := c.Get("test")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusStopped, st)
-
- s, st = c.Get("test2")
- assert.IsType(t, &testService{}, s)
- assert.Equal(t, StatusStopped, st)
-}
-
-type testInitA struct{}
-
-func (t *testInitA) Init() error {
- return nil
-}
-
-type testInitB struct{}
-
-func (t *testInitB) Init() (int, error) {
- return 0, nil
-}
-
-func TestContainer_InitErrorA(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testInitA{})
-
- assert.Error(t, c.Init(&testCfg{`{"test":"something", "test2":"something-else"}`}))
-}
-
-func TestContainer_InitErrorB(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testInitB{})
-
- assert.Error(t, c.Init(&testCfg{`{"test":"something", "test2":"something-else"}`}))
-}
-
-type testInitC struct{}
-
-func (r *testInitC) Test() bool {
- return true
-}
-
-func TestContainer_NoInit(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test", &testInitC{})
-
- assert.NoError(t, c.Init(&testCfg{`{"test":"something", "test2":"something-else"}`}))
-}
-
-type testInitD struct {
- c *testInitC //nolint:golint,unused,structcheck
-}
-
-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"}}`}))
-}
-
-func TestContainer_InitDependencyEmpty(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
-
- c := NewContainer(logger)
- c.Register("test2", &testInitD{})
-
- assert.Contains(t, c.Init(&testCfg{`{"test2":{"v":"ok"}}`}).Error(), "testInitC")
-}
diff --git a/service/entry.go b/service/entry.go
deleted file mode 100644
index 497742d1..00000000
--- a/service/entry.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package service
-
-import (
- "sync"
-)
-
-const (
- // StatusUndefined when service bus can not find the service.
- StatusUndefined = iota
-
- // StatusInactive when service has been registered in container.
- StatusInactive
-
- // StatusOK when service has been properly configured.
- StatusOK
-
- // StatusServing when service is currently done.
- StatusServing
-
- // StatusStopping when service is currently stopping.
- StatusStopping
-
- // StatusStopped when service being stopped.
- StatusStopped
-)
-
-// entry creates association between service instance and given name.
-type entry struct {
- name string
- svc interface{}
- mu sync.Mutex
- status int
-}
-
-// status returns service status
-func (e *entry) getStatus() int {
- e.mu.Lock()
- defer e.mu.Unlock()
-
- return e.status
-}
-
-// setStarted indicates that service hasStatus status.
-func (e *entry) setStatus(status int) {
- e.mu.Lock()
- defer e.mu.Unlock()
- e.status = status
-}
-
-// hasStatus checks if entry in specific status
-func (e *entry) hasStatus(status int) bool {
- return e.getStatus() == status
-}
-
-// canServe returns true is service can serve.
-func (e *entry) canServe() bool {
- _, ok := e.svc.(Service)
- return ok
-}
diff --git a/service/entry_test.go b/service/entry_test.go
deleted file mode 100644
index b5c71a10..00000000
--- a/service/entry_test.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package service
-
-import (
- "github.com/stretchr/testify/assert"
- "testing"
-)
-
-func TestEntry_CanServeFalse(t *testing.T) {
- e := &entry{svc: nil}
- assert.False(t, e.canServe())
-}
-
-func TestEntry_CanServeTrue(t *testing.T) {
- e := &entry{svc: &testService{}}
- assert.True(t, e.canServe())
-}
diff --git a/service/rpc/config.go b/service/rpc/config.go
index a4cf0f91..cc492622 100644
--- a/service/rpc/config.go
+++ b/service/rpc/config.go
@@ -2,10 +2,11 @@ package rpc
import (
"errors"
- "github.com/spiral/roadrunner/service"
- "github.com/spiral/roadrunner/util"
"net"
"strings"
+
+ "github.com/spiral/roadrunner/service"
+ "github.com/spiral/roadrunner/util"
)
// Config defines RPC service config.