forked from spiral-modules/php-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_test.go
114 lines (95 loc) · 2.41 KB
/
proxy_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
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/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"testing"
"time"
)
func Test_Proxy_Error(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
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, st := c.Get(ID)
assert.NotNil(t, s)
assert.Equal(t, service.StatusOK, st)
// should do nothing
s.(*Service).Stop()
go func() { assert.NoError(t, c.Serve()) }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
cl, cn := getClient("localhost:9080")
defer cn.Close()
_, err := cl.Throw(context.Background(), &tests.Message{Msg: "notFound"})
assert.Error(t, err)
se, _ := status.FromError(err)
assert.Equal(t, "nothing here", se.Message())
assert.Equal(t, codes.NotFound, se.Code())
}
func Test_Proxy_Metadata(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
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, st := c.Get(ID)
assert.NotNil(t, s)
assert.Equal(t, service.StatusOK, st)
// should do nothing
s.(*Service).Stop()
go func() { assert.NoError(t, c.Serve()) }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
cl, cn := getClient("localhost:9080")
defer cn.Close()
out, err := cl.Info(
metadata.AppendToOutgoingContext(context.Background(), "key", "proxy-value"),
&tests.Message{Msg: "MD"},
)
assert.NoError(t, err)
assert.Equal(t, `["proxy-value"]`, out.Msg)
}