diff options
author | Wolfy-J <[email protected]> | 2018-06-07 17:56:24 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-07 17:56:24 +0300 |
commit | 5a2dff38591c90cb1501dc09fea14e1a59d0cb52 (patch) | |
tree | 9a52dc0309d1f30b20560ae9f796712fb6337d73 | |
parent | ef78372ec1570571a1af6b1dc9847f7cc72fdc37 (diff) |
rcp tests
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | rpc/config_test.go | 2 | ||||
-rw-r--r-- | rpc/service.go | 4 | ||||
-rw-r--r-- | rpc/service_test.go | 84 | ||||
-rw-r--r-- | service/registry.go | 2 |
5 files changed, 93 insertions, 5 deletions
diff --git a/.travis.yml b/.travis.yml index 5ea8b20d..e84964f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,9 @@ install: - go get -u "github.com/stretchr/testify/assert" script: - - go test -race -v -coverprofile=coverage.txt -covermode=atomic + - go test -race -v -coverprofile=lib.txt -covermode=atomic + - go test ./rpc -race -v -coverprofile=rpc.txt -covermode=atomic after_success: - - bash <(curl -s https://codecov.io/bash) + - bash <(curl -s https://codecov.io/bash) -f lib.txt + - bash <(curl -s https://codecov.io/bash) -f rpc.txt
\ No newline at end of file diff --git a/rpc/config_test.go b/rpc/config_test.go index dbb028d3..a953e30e 100644 --- a/rpc/config_test.go +++ b/rpc/config_test.go @@ -1,9 +1,9 @@ package rpc import ( - "testing" "github.com/stretchr/testify/assert" "runtime" + "testing" ) func TestConfig_Listener(t *testing.T) { diff --git a/rpc/service.go b/rpc/service.go index ff5b85c8..d6e1bfa1 100644 --- a/rpc/service.go +++ b/rpc/service.go @@ -21,6 +21,8 @@ type Service struct { // 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) { + // todo: logging ? + config := &config{} if err := cfg.Unmarshal(config); err != nil { return nil, err @@ -57,10 +59,10 @@ func (s *Service) Serve() error { default: conn, err := ln.Accept() if err != nil { + conn.Close() continue } - s.rpc.Accept(ln) go s.rpc.ServeCodec(goridge.NewCodec(conn)) } } diff --git a/rpc/service_test.go b/rpc/service_test.go new file mode 100644 index 00000000..a83de388 --- /dev/null +++ b/rpc/service_test.go @@ -0,0 +1,84 @@ +package rpc + +import ( + "encoding/json" + "github.com/spiral/roadrunner/service" + "github.com/stretchr/testify/assert" + "testing" +) + +type testService struct{} + +func (ts *testService) Echo(msg string, r *string) error { *r = msg; return nil } + +type testCfg struct{ cfg string } + +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_Disabled(t *testing.T) { + s, err := (&Service{}).WithConfig(&testCfg{`{"enable":false}`}, nil) + + assert.NoError(t, err) + assert.Nil(t, s) +} + +func Test_RegisterNotConfigured(t *testing.T) { + s := &Service{} + assert.Error(t, s.Register("test", &testService{})) + + client, err := s.Client() + assert.Nil(t, client) + assert.Error(t, err) +} + +func Test_Enabled(t *testing.T) { + s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + + assert.NoError(t, err) + assert.NotNil(t, s) + assert.IsType(t, &Service{}, s) +} + +func Test_StopNonServing(t *testing.T) { + s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil) + + assert.NoError(t, err) + assert.NotNil(t, s) + assert.IsType(t, &Service{}, s) + s.Stop() +} + +func Test_Serve_Errors(t *testing.T) { + s, err := (&Service{}).WithConfig(&testCfg{`{"enable":true, "listen":"mailformed"}`}, nil) + assert.NoError(t, err) + assert.NotNil(t, s) + assert.IsType(t, &Service{}, s) + + assert.Error(t, s.Serve()) + + client, err := s.(*Service).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) + assert.NoError(t, err) + assert.NotNil(t, s) + assert.IsType(t, &Service{}, s) + defer s.Stop() + + assert.NoError(t, s.(*Service).Register("test", &testService{})) + + go func() { assert.NoError(t, s.Serve()) }() + + client, err := s.(*Service).Client() + assert.NotNil(t, client) + assert.NoError(t, err) + defer client.Close() + + var resp string + assert.NoError(t, client.Call("test.Echo", "hello world", &resp)) + assert.Equal(t, "hello world", resp) +} diff --git a/service/registry.go b/service/registry.go index 424ad328..f94d4171 100644 --- a/service/registry.go +++ b/service/registry.go @@ -2,9 +2,9 @@ package service import ( "fmt" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "sync" - "github.com/pkg/errors" ) // Config provides ability to slice configuration sections and unmarshal configuration data into |