1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
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_ConfigError(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":false`}, nil)
assert.Error(t, err)
assert.False(t, ok)
}
func Test_Disabled(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":false}`}, nil)
assert.NoError(t, err)
assert.False(t, ok)
}
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)
assert.Error(t, s.Serve())
}
func Test_Enabled(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil)
assert.NoError(t, err)
assert.True(t, ok)
}
func Test_StopNonServing(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil)
assert.NoError(t, err)
assert.True(t, ok)
s.Stop()
}
func Test_Serve_Errors(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"mailformed"}`}, nil)
assert.NoError(t, err)
assert.True(t, ok)
assert.Error(t, s.Serve())
client, err := s.Client()
assert.Nil(t, client)
assert.Error(t, err)
}
func Test_Serve_Client(t *testing.T) {
s := &Service{}
ok, err := s.Configure(&testCfg{`{"enable":true, "listen":"tcp://localhost:9008"}`}, nil)
assert.NoError(t, err)
assert.True(t, ok)
defer s.Stop()
assert.NoError(t, s.Register("test", &testService{}))
go func() { assert.NoError(t, s.Serve()) }()
client, err := s.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)
}
|