forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
110 lines (92 loc) · 2.67 KB
/
router_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
package router
import (
"context"
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/cluster/mocks"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/route"
)
var (
serverID = "id"
serverType = "serverType"
frontend = true
server = cluster.NewServer(serverID, serverType, frontend)
servers = map[string]*cluster.Server{
serverID: server,
}
routingFunction = func(
ctx context.Context,
route *route.Route,
payload []byte,
servers map[string]*cluster.Server,
) (*cluster.Server, error) {
return server, nil
}
)
var routerTables = map[string]struct {
server *cluster.Server
serverType string
rpcType protos.RPCType
err error
}{
"test_server_has_route_func": {server, serverType, protos.RPCType_Sys, nil},
"test_server_use_default_func": {server, "notRegisteredType", protos.RPCType_Sys, nil},
"test_user_use_default_func": {server, serverType, protos.RPCType_User, nil},
"test_error_on_service_disc": {nil, serverType, protos.RPCType_Sys, errors.New("sd error")},
}
var addRouteRouterTables = map[string]struct {
serverType string
}{
"test_overrige_server_type": {serverType},
"test_new_server_type": {"notRegisteredType"},
}
func TestNew(t *testing.T) {
t.Parallel()
router := New()
assert.NotNil(t, router)
}
func TestDefaultRoute(t *testing.T) {
t.Parallel()
router := New()
retServer := router.defaultRoute(servers)
assert.Equal(t, server, retServer)
}
func TestRoute(t *testing.T) {
t.Parallel()
ctx := context.Background()
route := route.NewRoute(serverType, "service", "method")
for name, table := range routerTables {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockServiceDiscovery := mocks.NewMockServiceDiscovery(ctrl)
mockServiceDiscovery.EXPECT().
GetServersByType(table.serverType).
Return(servers, table.err)
router := New()
router.AddRoute(serverType, routingFunction)
router.SetServiceDiscovery(mockServiceDiscovery)
retServer, err := router.Route(ctx, table.rpcType, table.serverType, route, &message.Message{
Data: []byte{0x01},
})
assert.Equal(t, table.server, retServer)
assert.Equal(t, table.err, err)
})
}
}
func TestAddRoute(t *testing.T) {
t.Parallel()
for name, table := range addRouteRouterTables {
t.Run(name, func(t *testing.T) {
router := New()
router.AddRoute(table.serverType, routingFunction)
assert.NotNil(t, router.routesMap[table.serverType])
assert.Nil(t, router.routesMap["anotherServerType"])
})
}
}