-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver_test.go
145 lines (122 loc) · 3.22 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package netcode
import (
"net"
"testing"
"time"
)
type SendFunc func(serv *Server, payload []byte)
func TestServerListen(t *testing.T) {
port := 40000
doneCh := make(chan struct{})
go runTestServer(port, testSendFunc, doneCh, t)
go runTestClient(port, t)
<-doneCh
}
func TestServerSendPayloadToClient(t *testing.T) {
port := 40001
doneCh := make(chan struct{})
go runTestServer(port, testSendToClientFunc, doneCh, t)
go runTestClient(port, t)
<-doneCh
}
// Tests sending payloads to all connected clients
func testSendFunc(serv *Server, payload []byte) {
// send payloads to clients
serv.SendPayloads(payload)
}
// Tests sending to individual clients via their ClientIds
func testSendToClientFunc(serv *Server, payload []byte) {
// do simulation/process payload packets
clientIds := serv.GetConnectedClientIds()
if len(clientIds) > 0 {
for _, clientId := range clientIds {
serv.SendPayloadToClient(clientId, payload)
}
}
}
func runTestServer(port int, sendFunc SendFunc, doneCh chan struct{}, t *testing.T) {
maxClients := 32
addr := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}
serv := NewServer(&addr, TEST_PRIVATE_KEY, TEST_PROTOCOL_ID, maxClients)
if err := serv.Init(); err != nil {
t.Fatalf("error initializing server: %s\n", err)
}
defer serv.Stop()
if err := serv.Listen(); err != nil {
t.Fatalf("error listening: %s\n", err)
}
payload := make([]byte, MAX_PAYLOAD_BYTES)
for i := 0; i < len(payload); i += 1 {
payload[i] = byte(i)
}
serverTime := float64(0.0)
delta := float64(1.0 / 60.0)
deltaTime := time.Duration(delta * float64(time.Second))
count := 0
payloadCount := 0
for {
serv.Update(serverTime)
if count > 0 && payloadCount > 0 {
close(doneCh)
return
}
for i := 0; i < serv.MaxClients(); i += 1 {
for {
responsePayload, _ := serv.RecvPayload(i)
if len(responsePayload) == 0 {
break
}
payloadCount++
t.Logf("got payload: %d\n", len(responsePayload))
}
}
// do simulation/process payload packets
// send payloads to clients
//sendFunc(serv, payload, serverTime)
serv.SendPayloads(payload)
time.Sleep(deltaTime)
serverTime += deltaTime.Seconds()
count += 1
}
}
func runTestClient(port int, t *testing.T) {
server := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}
servers := make([]net.UDPAddr, 1)
servers[0] = server
connectToken := testGenerateConnectToken(servers, TEST_PRIVATE_KEY, t)
clientTime := float64(0)
delta := float64(1.0 / 60.0)
deltaTime := time.Duration(delta * float64(time.Second))
c := NewClient(connectToken)
if err := c.Connect(); err != nil {
t.Fatalf("error connecting: %s\n", err)
}
defer c.Close()
packetData := make([]byte, MAX_PAYLOAD_BYTES)
for i := 0; i < MAX_PAYLOAD_BYTES; i += 1 {
packetData[i] = byte(i)
}
count := 0
// fake game loop
for {
if count == 20 {
t.Fatalf("never recv'd a payload packet after 20 iterations")
return
}
c.Update(clientTime)
if c.GetState() == StateConnected {
c.SendData(packetData)
}
for {
if payload, seq := c.RecvData(); payload == nil {
break
} else {
t.Logf("seq: %d recv'd payload: of %d bytes\n", seq, len(payload))
return
}
}
time.Sleep(deltaTime)
clientTime += deltaTime.Seconds()
count++
}
}