-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
129 lines (118 loc) · 2.86 KB
/
server_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
package flyrpc
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func makeClient(t *testing.T, addr string) *Client {
client, err := Dial("tcp", addr)
assert.NoError(t, err)
assert.NotNil(t, client)
client.SetSerializer(JSON)
return client
}
func TestServerMultiConnect(t *testing.T) {
server := NewServer(&ServerOpts{
Serializer: JSON,
})
wg := &sync.WaitGroup{}
server.OnConnect(func(ctx *Context) {
ctx.SendMessage("1", &TestUser{Id: 123})
})
go func() {
err := server.Listen("tcp", "127.0.0.1:15555")
assert.Nil(t, err)
}()
<-time.After(10 * time.Millisecond)
wg.Add(1)
c1 := makeClient(t, "127.0.0.1:15555")
assert.NotNil(t, c1)
c1.OnMessage("1", func(ctx *Context, user *TestUser) {
assert.Equal(t, int32(123), user.Id)
wg.Done()
})
wg.Add(1)
c2 := makeClient(t, "127.0.0.1:15555")
assert.NotNil(t, c2)
c2.OnMessage("1", func(ctx *Context, user *TestUser) {
assert.Equal(t, int32(123), user.Id)
wg.Done()
})
wg.Wait()
server.Close()
}
func TestServerHandlePacket(t *testing.T) {
uid := int32(123)
server := NewServer(&ServerOpts{
Serializer: JSON,
})
wg := &sync.WaitGroup{}
server.OnMessage("1", func(ctx *Context, u *TestUser) {
assert.Equal(t, uid, u.Id)
wg.Done()
})
go func() {
err := server.Listen("tcp", "127.0.0.1:15556")
assert.Nil(t, err)
}()
<-time.After(10 * time.Millisecond)
wg.Add(1)
client := makeClient(t, "127.0.0.1:15556")
client.SendMessage("1", &TestUser{Id: 123})
wg.Wait()
server.Close()
}
/*
func TestServer(t *testing.T) {
server := NewServer(&ServerOpts{
Serializer: JSON,
})
err := server.Listen("127.0.0.1:15555")
assert.Nil(t, err)
assert.NotNil(t, server.listener)
server.OnMessage(21, func(ctx *Context, in *TestUser) *TestUser {
reply := new(TestUser)
log.Println("server on", in)
// call client code 2 and response another user
log.Println("server call 2")
err := ctx.Call(12, in, reply)
assert.Nil(t, err)
log.Println("client response", reply)
reply.Id += 10
return reply
})
server.OnConnect(func(ctx *Context) {
log.Println("connected")
in := new(TestUser)
err := ctx.SendMessage(11, in)
assert.Nil(t, err)
})
go server.HandleConnections()
wg := &sync.WaitGroup{}
for i := 0; i < 2; i++ {
testClient(t, wg, i)
}
wg.Wait()
}
func testClient(t *testing.T, wg *sync.WaitGroup, i int) {
wg.Add(1)
conn, err := net.Dial("tcp", "127.0.0.1:15555")
assert.Nil(t, err)
client := NewClient(conn, JSON)
client.Context.ClientId = 100 + i
client.OnMessage(12, func(ctx *Context, in *TestUser) *TestUser {
log.Println("client on", in)
return &TestUser{Id: in.Id + 1}
})
reply := new(TestUser)
client.OnMessage(11, func(ctx *Context, in *TestUser) {
log.Println("client call 1")
err = client.Call(21, &TestUser{Id: 100}, reply)
assert.Nil(t, err)
log.Println("server response", reply)
assert.Equal(t, 111, reply.Id)
wg.Done()
})
}
*/