summaryrefslogtreecommitdiff
path: root/rpc/service_test.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-07 17:56:24 +0300
committerWolfy-J <[email protected]>2018-06-07 17:56:24 +0300
commit5a2dff38591c90cb1501dc09fea14e1a59d0cb52 (patch)
tree9a52dc0309d1f30b20560ae9f796712fb6337d73 /rpc/service_test.go
parentef78372ec1570571a1af6b1dc9847f7cc72fdc37 (diff)
rcp tests
Diffstat (limited to 'rpc/service_test.go')
-rw-r--r--rpc/service_test.go84
1 files changed, 84 insertions, 0 deletions
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)
+}