12
"mosn.io/mosn/pkg/protocol"
13
"mosn.io/mosn/pkg/protocol/xprotocol/bolt"
14
"mosn.io/mosn/pkg/types"
15
"mosn.io/mosn/test/util"
16
"mosn.io/mosn/test/util/mosn"
19
type RetryCase struct {
21
GoodServer util.UpstreamServer
22
BadServer util.UpstreamServer
26
func NewRetryCase(t *testing.T, serverProto, meshProto types.ProtocolName, isClose bool) *RetryCase {
27
app1 := "127.0.0.1:8080"
28
app2 := "127.0.0.1:8081"
29
var good, bad util.UpstreamServer
32
good = util.NewHTTPServer(t, &PathHTTPHandler{})
33
bad = util.NewHTTPServer(t, &BadHTTPHandler{})
35
good = util.NewUpstreamHTTP2(t, app1, &PathHTTPHandler{})
36
bad = util.NewUpstreamHTTP2(t, app2, &BadHTTPHandler{})
38
tc := NewTestCase(t, serverProto, meshProto, util.NewRPCServer(t, "", bolt.ProtocolName))
46
func (c *RetryCase) StartProxy() {
47
c.GoodServer.GoServe()
49
app1 := c.GoodServer.Addr()
50
app2 := c.BadServer.Addr()
54
clientMeshAddr := util.CurrentMeshAddr()
55
c.ClientMeshAddr = clientMeshAddr
56
cfg := util.CreateProxyMesh(clientMeshAddr, []string{app1, app2}, c.AppProtocol)
57
mesh := mosn.NewMosn(cfg)
68
time.Sleep(5 * time.Second)
72
func (c *RetryCase) Start(tls bool) {
73
c.GoodServer.GoServe()
75
app1 := c.GoodServer.Addr()
76
app2 := c.BadServer.Addr()
80
clientMeshAddr := util.CurrentMeshAddr()
81
c.ClientMeshAddr = clientMeshAddr
82
serverMeshAddr := util.CurrentMeshAddr()
83
cfg := util.CreateMeshToMeshConfig(clientMeshAddr, serverMeshAddr, c.AppProtocol, c.MeshProtocol, []string{app1, app2}, tls)
84
mesh := mosn.NewMosn(cfg)
95
time.Sleep(5 * time.Second)
98
type PathHTTPHandler struct{}
100
func (h *PathHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
101
w.Header().Set("Content-Type", "text/plain")
102
if strings.Trim(r.URL.Path, "/") != HTTPTestPath {
103
w.WriteHeader(http.StatusInternalServerError)
105
fmt.Fprintf(w, "\nRequestId:%s\n", r.Header.Get("Requestid"))
109
type BadHTTPHandler struct{}
111
func (h *BadHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
112
w.Header().Set("Content-Type", "text/plain")
113
w.WriteHeader(http.StatusInternalServerError)
114
fmt.Fprintf(w, "\nRequestId:%s\n", r.Header.Get("Requestid"))
117
func TestRetry(t *testing.T) {
118
util.StartRetry = true
120
util.StartRetry = false
122
testCases := []*RetryCase{
124
NewRetryCase(t, protocol.HTTP1, protocol.HTTP1, false),
127
NewRetryCase(t, protocol.HTTP2, protocol.HTTP2, false),
129
NewRetryCase(t, protocol.HTTP1, protocol.HTTP1, true),
131
NewRetryCase(t, protocol.HTTP2, protocol.HTTP2, true),
139
for i, tc := range testCases {
140
t.Logf("start case #%d\n", i)
147
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v test failed, error: %v\n", i, tc.AppProtocol, tc.MeshProtocol, err)
149
case <-time.After(15 * time.Second):
150
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v hang\n", i, tc.AppProtocol, tc.MeshProtocol)
156
func TestRetryProxy(t *testing.T) {
157
util.StartRetry = true
159
util.StartRetry = false
161
testCases := []*RetryCase{
162
NewRetryCase(t, protocol.HTTP1, protocol.HTTP1, false),
163
NewRetryCase(t, protocol.HTTP2, protocol.HTTP2, false),
168
for i, tc := range testCases {
169
t.Logf("start case #%d\n", i)
175
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v test failed, error: %v\n", i, tc.AppProtocol, tc.MeshProtocol, err)
177
case <-time.After(30 * time.Second):
178
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v hang\n", i, tc.AppProtocol, tc.MeshProtocol)
185
type XRetryCase struct {
187
GoodServer util.UpstreamServer
188
BadServer util.UpstreamServer
192
func NewXRetryCase(t *testing.T, subProtocol types.ProtocolName, isClose bool) *XRetryCase {
193
app1 := "127.0.0.1:8080"
194
app2 := "127.0.0.1:8081"
195
var good, bad util.UpstreamServer
197
case bolt.ProtocolName:
198
good = util.NewRPCServer(t, app1, bolt.ProtocolName)
199
bad = util.RPCServer{
200
Client: util.NewRPCClient(t, "rpcClient", bolt.ProtocolName),
202
UpstreamServer: util.NewUpstreamServer(t, app2, ServeBadBoltV1),
205
tc := NewXTestCase(t, subProtocol, util.NewRPCServer(t, "", subProtocol))
213
func (c *XRetryCase) StartProxy() {
214
c.GoodServer.GoServe()
215
c.BadServer.GoServe()
216
app1 := c.GoodServer.Addr()
217
app2 := c.BadServer.Addr()
221
clientMeshAddr := util.CurrentMeshAddr()
222
c.ClientMeshAddr = clientMeshAddr
223
cfg := util.CreateXProtocolProxyMesh(clientMeshAddr, []string{app1, app2}, c.SubProtocol)
224
mesh := mosn.NewMosn(cfg)
235
time.Sleep(5 * time.Second)
239
func (c *XRetryCase) Start(tls bool) {
240
c.GoodServer.GoServe()
241
c.BadServer.GoServe()
242
app1 := c.GoodServer.Addr()
243
app2 := c.BadServer.Addr()
247
clientMeshAddr := util.CurrentMeshAddr()
248
c.ClientMeshAddr = clientMeshAddr
249
serverMeshAddr := util.CurrentMeshAddr()
250
cfg := util.CreateXProtocolMesh(clientMeshAddr, serverMeshAddr, c.SubProtocol, []string{app1, app2}, tls)
251
mesh := mosn.NewMosn(cfg)
262
time.Sleep(5 * time.Second)
265
func ServeBadBoltV1(t *testing.T, conn net.Conn) {
267
proto := (&bolt.XCodec{}).NewXProtocol(context.Background())
268
response := func(iobuf types.IoBuffer) ([]byte, bool) {
269
cmd, _ := proto.Decode(nil, iobuf)
273
if req, ok := cmd.(*bolt.Request); ok {
274
resp := bolt.NewRpcResponse(req.RequestId, bolt.ResponseStatusServerException, nil, nil)
275
iobufresp, err := proto.Encode(nil, resp)
277
t.Errorf("Build response error: %v\n", err)
280
return iobufresp.Bytes(), true
284
util.ServeRPC(t, conn, response)
287
func TestXRetry(t *testing.T) {
288
util.StartRetry = true
290
util.StartRetry = false
292
testCases := []*XRetryCase{
294
NewXRetryCase(t, bolt.ProtocolName, true),
297
for i, tc := range testCases {
298
t.Logf("start case #%d\n", i)
305
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v test xprotocol %s failed, error: %v\n", i, tc.AppProtocol, tc.MeshProtocol, tc.SubProtocol, err)
307
case <-time.After(15 * time.Second):
308
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v xprotocol %s hang\n", i, tc.AppProtocol, tc.MeshProtocol, tc.SubProtocol)
314
func TestXRetryProxy(t *testing.T) {
315
util.StartRetry = true
317
util.StartRetry = false
319
testCases := []*XRetryCase{
320
NewXRetryCase(t, bolt.ProtocolName, true),
323
for i, tc := range testCases {
324
t.Logf("start case #%d\n", i)
330
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v xprotocol %s test failed, error: %v\n", i, tc.AppProtocol, tc.MeshProtocol, tc.SubProtocol, err)
332
case <-time.After(30 * time.Second):
333
t.Errorf("[ERROR MESSAGE] #%d %v to mesh %v xprotocol %s hang\n", i, tc.AppProtocol, tc.MeshProtocol, tc.SubProtocol)