-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsynctest_test.go
296 lines (265 loc) · 8.4 KB
/
synctest_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package ggpo_test
import (
"bytes"
"testing"
"github.com/assemblaj/ggpo/internal/mocks"
"github.com/assemblaj/ggpo"
)
func TestNewSyncTestBackend(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
}
func TestSyncTestBackendAddPlayerOver(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 2)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
err := stb.AddPlayer(&player, &handle)
if err == nil {
t.Errorf("There should be an error for adding a player greater than the total num Players.")
}
}
func TestSyncTestBackendAddPlayerNegative(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, -1)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
err := stb.AddPlayer(&player, &handle)
if err == nil {
t.Errorf("There should be an error for adding a player with a negative number")
}
}
func TestSyncTestBackendAddLocalInputError(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
err := stb.AddLocalInput(handle, []byte{1, 2, 3, 4}, 4)
if err == nil {
t.Errorf("There should be an error for adding local input when sync test isn't running yet.")
}
}
func TestSyncTestBackendAddLocalInput(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
stb.Idle(0)
err := stb.AddLocalInput(handle, []byte{1, 2, 3, 4}, 4)
if err != nil {
t.Errorf("There shouldn't be an error, adding local input should be successful.")
}
}
func TestSyncTestBackendSyncInput(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
stb := ggpo.NewSyncTest(&session, 1, 8, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
stb.Idle(0)
inputBytes := []byte{1, 2, 3, 4}
stb.AddLocalInput(handle, []byte{1, 2, 3, 4}, 4)
var disconnectFlags int
input, _ := stb.SyncInput(&disconnectFlags)
got := input[0]
want := inputBytes
if !bytes.Equal(got, want) {
t.Errorf("expected '%#v' but got '%#v'", want, got)
}
}
func TestSyncTestBackendIncrementFramePanic(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic wen trying to load a state that hadn't been saved")
}
}()
for i := 0; i < checkDistance; i++ {
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
}
func TestSyncTestBackendIncrementFrameCharacterization(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
stb.Idle(0)
inputBytes := []byte{1, 2, 3, 4}
var disconnectFlags int
for i := 0; i < checkDistance-1; i++ {
stb.AddLocalInput(handle, inputBytes, 4)
stb.SyncInput(&disconnectFlags)
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic due to a SyncError")
}
}()
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
// Attempt to simulate STB workflow in a test harness. Always panics on check
// distance frame. Do not full understand why even though it works perfectly
// fine in real time.
func TestSyncTestBackendIncrementFrame(t *testing.T) {
session := mocks.NewFakeSession()
player := ggpo.NewLocalPlayer(20, 1)
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
inputBytes := []byte{1, 2, 3, 4}
var disconnectFlags int
var result error
for i := 0; i < checkDistance-1; i++ {
stb.Idle(0)
result = stb.AddLocalInput(handle, inputBytes, 4)
if result == nil {
_, result = stb.SyncInput(&disconnectFlags)
if result == nil {
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
}
}
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic due to a SyncError")
}
}()
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
/*Again, WIP, I don't know how to test that this is working, but it is. */
func TestSyncTestBackendChecksumCheck(t *testing.T) {
session := mocks.NewFakeSessionWithBackend()
var stb ggpo.SyncTest
session.SetBackend(&stb)
player := ggpo.NewLocalPlayer(20, 1)
checkDistance := 8
stb = ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
var handle ggpo.PlayerHandle
stb.AddPlayer(&player, &handle)
inputBytes := []byte{1, 2, 3, 4}
var disconnectFlags int
var result error
for i := 0; i < checkDistance+1; i++ {
stb.Idle(0)
result = stb.AddLocalInput(handle, inputBytes, 4)
if result == nil {
vals, result := stb.SyncInput(&disconnectFlags)
if result == nil {
session.Game.UpdateByInputs(vals)
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
}
}
}
func TestSyncTestBackendMultiplePlayers(t *testing.T) {
session := mocks.NewFakeSessionWithBackend()
var stb ggpo.SyncTest
session.SetBackend(&stb)
player1 := ggpo.NewLocalPlayer(20, 1)
player2 := ggpo.NewLocalPlayer(20, 2)
checkDistance := 8
stb = ggpo.NewSyncTest(&session, 2, checkDistance, 2, true)
var handle1 ggpo.PlayerHandle
stb.AddPlayer(&player1, &handle1)
var handle2 ggpo.PlayerHandle
stb.AddPlayer(&player2, &handle2)
ib1 := []byte{2, 4}
ib2 := []byte{1, 3}
var disconnectFlags int
var result error
res1 := []float64{0, 0}
res2 := []float64{0, 0}
for i := 0; i < checkDistance+1; i++ {
res1[0] += float64(ib1[0])
res1[1] += float64(ib1[1])
res2[0] += float64(ib2[0])
res2[1] += float64(ib2[1])
}
for i := 0; i < checkDistance+1; i++ {
stb.Idle(0)
result = stb.AddLocalInput(handle1, ib1, 2)
if result == nil {
result = stb.AddLocalInput(handle2, ib2, 2)
}
if result == nil {
vals, result := stb.SyncInput(&disconnectFlags)
if result == nil {
session.Game.UpdateByInputs(vals)
stb.AdvanceFrame(ggpo.DefaultChecksum)
}
}
}
if session.Game.Players[0].X != res1[0] || session.Game.Players[0].Y != res1[1] ||
session.Game.Players[1].X != res2[0] || session.Game.Players[1].Y != res2[1] {
t.Errorf("Invalid result in 2 Player SyncTest Session")
}
}
// Unsupported functions
func TestSyncTestBackendDissconnectPlayerError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
err := stb.DisconnectPlayer(ggpo.PlayerHandle(1))
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}
func TestSyncTestBackendGetNetworkStatsError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
_, err := stb.GetNetworkStats(ggpo.PlayerHandle(1))
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}
func TestSyncTestBackendSetFrameDelayError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
err := stb.SetFrameDelay(ggpo.PlayerHandle(1), 20)
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}
func TestSyncTestBackendSetDisconnectTimeoutError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
err := stb.SetDisconnectTimeout(20)
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}
func TestSyncTestBackendSetDisconnectNotifyStartError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
err := stb.SetDisconnectNotifyStart(20)
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}
func TestSyncTestBackendCloseError(t *testing.T) {
session := mocks.NewFakeSession()
checkDistance := 8
stb := ggpo.NewSyncTest(&session, 1, checkDistance, 4, true)
err := stb.Close()
if err == nil {
t.Errorf("The code did not error when using an unsupported Feature.")
}
}