forked from spiral-modules/php-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_test.go
138 lines (110 loc) · 3 KB
/
rpc_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package grpc
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spiral/php-grpc/tests"
"github.com/spiral/roadrunner/service"
"github.com/spiral/roadrunner/service/rpc"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"strconv"
"testing"
"time"
)
func Test_RPC(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(rpc.ID, &rpc.Service{})
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`,
grpcCfg: `{
"listen": "tcp://:9080",
"tls": {
"key": "tests/server.key",
"cert": "tests/server.crt"
},
"proto": "tests/test.proto",
"workers":{
"command": "php tests/worker.php",
"relay": "pipes",
"pool": {
"numWorkers": 1,
"allocateTimeout": 10,
"destroyTimeout": 10
}
}
}`,
}))
s, _ := c.Get(ID)
ss := s.(*Service)
s2, _ := c.Get(rpc.ID)
rs := s2.(*rpc.Service)
go func() { assert.NoError(t, c.Serve()) }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
cl, cn := getClient("localhost:9080")
defer cn.Close()
rcl, err := rs.Client()
assert.NoError(t, err)
out, err := cl.Info(context.Background(), &tests.Message{Msg: "PID"})
assert.NoError(t, err)
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), out.Msg)
r := ""
assert.NoError(t, rcl.Call("grpc.Reset", true, &r))
assert.Equal(t, "OK", r)
out2, err := cl.Info(context.Background(), &tests.Message{Msg: "PID"})
assert.NoError(t, err)
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), out2.Msg)
assert.NotEqual(t, out.Msg, out2.Msg)
}
func Test_Workers(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(rpc.ID, &rpc.Service{})
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`,
grpcCfg: `{
"listen": "tcp://:9080",
"tls": {
"key": "tests/server.key",
"cert": "tests/server.crt"
},
"proto": "tests/test.proto",
"workers":{
"command": "php tests/worker.php",
"relay": "pipes",
"pool": {
"numWorkers": 1,
"allocateTimeout": 10,
"destroyTimeout": 10
}
}
}`,
}))
s, _ := c.Get(ID)
ss := s.(*Service)
s2, _ := c.Get(rpc.ID)
rs := s2.(*rpc.Service)
go func() { assert.NoError(t, c.Serve()) }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
cl, cn := getClient("localhost:9080")
defer cn.Close()
rcl, err := rs.Client()
assert.NoError(t, err)
out, err := cl.Info(context.Background(), &tests.Message{Msg: "PID"})
assert.NoError(t, err)
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), out.Msg)
r := &WorkerList{}
assert.NoError(t, rcl.Call("grpc.Workers", true, &r))
assert.Len(t, r.Workers, 1)
}
func Test_Errors(t *testing.T) {
r := &rpcServer{nil}
assert.Error(t, r.Reset(true, nil))
assert.Error(t, r.Workers(true, nil))
}