forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_test.go
99 lines (93 loc) · 4.28 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
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package pitaya
import (
"context"
"testing"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/v2/cluster"
clustermocks "github.com/topfreegames/pitaya/v2/cluster/mocks"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/conn/codec"
"github.com/topfreegames/pitaya/v2/conn/message"
"github.com/topfreegames/pitaya/v2/constants"
"github.com/topfreegames/pitaya/v2/pipeline"
"github.com/topfreegames/pitaya/v2/protos"
"github.com/topfreegames/pitaya/v2/protos/test"
"github.com/topfreegames/pitaya/v2/route"
"github.com/topfreegames/pitaya/v2/router"
serializemocks "github.com/topfreegames/pitaya/v2/serialize/mocks"
"github.com/topfreegames/pitaya/v2/service"
sessionmocks "github.com/topfreegames/pitaya/v2/session/mocks"
)
func TestDoSendRPCNotInitialized(t *testing.T) {
config := config.NewDefaultPitayaConfig()
app := NewDefaultApp(true, "testtype", Standalone, map[string]string{}, *config).(*App)
err := app.doSendRPC(nil, "", "", nil, nil)
assert.Equal(t, constants.ErrRPCServerNotInitialized, err)
}
func TestDoSendRPC(t *testing.T) {
config := config.NewDefaultPitayaConfig()
app := NewDefaultApp(true, "testtype", Cluster, map[string]string{}, *config).(*App)
app.server.ID = "myserver"
app.rpcServer = &cluster.NatsRPCServer{}
tables := []struct {
name string
routeStr string
reply proto.Message
arg proto.Message
err error
}{
{"bad_route", "badroute", &test.SomeStruct{}, nil, route.ErrInvalidRoute},
{"no_server_type", "bla.bla", &test.SomeStruct{}, nil, constants.ErrNoServerTypeChosenForRPC},
{"nonsense_rpc", "mytype.bla.bla", &test.SomeStruct{}, nil, constants.ErrNonsenseRPC},
{"success", "bla.bla.bla", &test.SomeStruct{}, &test.SomeStruct{A: 1}, nil},
}
for _, table := range tables {
t.Run(table.name, func(t *testing.T) {
ctx := context.Background()
if table.err == nil {
packetEncoder := codec.NewPomeloPacketEncoder()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockSerializer := serializemocks.NewMockSerializer(ctrl)
mockSD := clustermocks.NewMockServiceDiscovery(ctrl)
mockRPCClient := clustermocks.NewMockRPCClient(ctrl)
mockRPCServer := clustermocks.NewMockRPCServer(ctrl)
messageEncoder := message.NewMessagesEncoder(false)
sessionPool := sessionmocks.NewMockSessionPool(ctrl)
router := router.New()
handlerPool := service.NewHandlerPool()
svc := service.NewRemoteService(mockRPCClient, mockRPCServer, mockSD, packetEncoder, mockSerializer, router, messageEncoder, &cluster.Server{}, sessionPool, pipeline.NewRemoteHooks(), pipeline.NewHandlerHooks(), handlerPool)
assert.NotNil(t, svc)
app.remoteService = svc
app.server.ID = "notmyserver"
b, err := proto.Marshal(&test.SomeStruct{A: 1})
assert.NoError(t, err)
mockSD.EXPECT().GetServer("myserver").Return(&cluster.Server{}, nil)
mockRPCClient.EXPECT().Call(ctx, protos.RPCType_User, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&protos.Response{Data: b}, nil)
}
err := app.RPCTo(ctx, "myserver", table.routeStr, table.reply, table.arg)
assert.Equal(t, table.err, err)
})
}
}