forked from markus-wa/demoinfocs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demoinfocs_test.go
378 lines (318 loc) · 9.31 KB
/
demoinfocs_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package demoinfocs_test
import (
"bytes"
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/gogo/protobuf/proto"
dispatch "github.com/markus-wa/godispatch"
dem "github.com/visual42/demoinfocs-golang"
common "github.com/visual42/demoinfocs-golang/common"
events "github.com/visual42/demoinfocs-golang/events"
msg "github.com/visual42/demoinfocs-golang/msg"
)
const csDemosPath = "cs-demos"
const demSetPath = csDemosPath + "/set"
const defaultDemPath = csDemosPath + "/default.dem"
const unexpectedEndOfDemoPath = csDemosPath + "/unexpected_end_of_demo.dem"
const valveMatchmakingDemoPath = csDemosPath + "/valve_matchmaking.dem"
var concurrentDemos int
func init() {
flag.IntVar(&concurrentDemos, "concurrentdemos", 2, "The `number` of current demos")
flag.Parse()
if _, err := os.Stat(defaultDemPath); err != nil {
panic(fmt.Sprintf("Failed to read test demo %q", defaultDemPath))
}
}
func TestDemoInfoCs(t *testing.T) {
if testing.Short() {
t.Skip("skipping test")
}
f, err := os.Open(defaultDemPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
p := dem.NewParserWithConfig(f, dem.ParserConfig{
MsgQueueBufferSize: 1000,
AdditionalNetMessageCreators: map[int]dem.NetMessageCreator{
4: func() proto.Message { return new(msg.CNETMsg_Tick) },
},
})
fmt.Println("Parsing header")
h, err := p.ParseHeader()
if err != nil {
t.Fatal(err)
}
fmt.Printf("Header: %v - FrameRate()=%.2f frames/s ; FrameTime()=%s ; TickRate()=%.2f frames/s ; TickTime()=%s\n", h, h.FrameRate(), h.FrameTime(), h.TickRate(), h.TickTime())
h2 := p.Header()
if h != h2 {
t.Errorf("Headers returned by ParseHeader() & Header(), respectively, aren't equal; ParseHeader(): %v - Header(): %v", h, h2)
}
fmt.Println("Registering handlers")
gs := p.GameState()
p.RegisterEventHandler(func(e events.RoundEnd) {
var winner *common.TeamState
var loser *common.TeamState
var winnerSide string
switch e.Winner {
case common.TeamTerrorists:
winner = gs.TeamTerrorists()
loser = gs.TeamCounterTerrorists()
winnerSide = "T"
case common.TeamCounterTerrorists:
winner = gs.TeamCounterTerrorists()
loser = gs.TeamTerrorists()
winnerSide = "CT"
default:
// Probably match medic or something similar
fmt.Println("Round finished: No winner (tie)")
return
}
winnerClan := winner.ClanName
winnerID := winner.ID
winnerFlag := winner.Flag
ingameTime := p.CurrentTime()
progressPercent := p.Progress() * 100
ingameTick := gs.IngameTick()
currentFrame := p.CurrentFrame()
// Score + 1 for winner because it hasn't actually been updated yet
fmt.Printf("Round finished: score=%d:%d ; winnerSide=%s ; clanName=%q ; teamId=%d ; teamFlag=%s ; ingameTime=%s ; progress=%.1f%% ; tick=%d ; frame=%d\n", winner.Score+1, loser.Score, winnerSide, winnerClan, winnerID, winnerFlag, ingameTime, progressPercent, ingameTick, currentFrame)
if len(winnerClan) == 0 || winnerID == 0 || len(winnerFlag) == 0 || ingameTime == 0 || progressPercent == 0 || ingameTick == 0 || currentFrame == 0 {
t.Error("Unexprected default value, check output of last round")
}
})
// Check some things at match start
p.RegisterEventHandler(func(events.MatchStart) {
participants := gs.Participants()
all := participants.All()
players := participants.Playing()
if len(all) <= len(players) {
// We know the default demo has spectators
t.Error("Expected more participants than players (spectators)")
}
if nPlayers := len(players); nPlayers != 10 {
// We know there should be 10 players at match start in the default demo
t.Error("Expected 10 players; got", nPlayers)
}
if nTerrorists := len(participants.TeamMembers(common.TeamTerrorists)); nTerrorists != 5 {
// We know there should be 5 terrorists at match start in the default demo
t.Error("Expected 5 terrorists; got", nTerrorists)
}
if nCTs := len(participants.TeamMembers(common.TeamCounterTerrorists)); nCTs != 5 {
// We know there should be 5 CTs at match start in the default demo
t.Error("Expected 5 CTs; got", nCTs)
}
})
// Regression test for grenade projectiles not being deleted at the end of the round (issue #42)
p.RegisterEventHandler(func(events.RoundStart) {
if nProjectiles := len(p.GameState().GrenadeProjectiles()); nProjectiles > 0 {
t.Error("Expected 0 GrenadeProjectiles at the start of the round, got", nProjectiles)
}
if nInfernos := len(p.GameState().Infernos()); nInfernos > 0 {
t.Error("Expected 0 Infernos at the start of the round, got", nInfernos)
}
})
// Net-message stuff
var netTickHandlerID dispatch.HandlerIdentifier
netTickHandlerID = p.RegisterNetMessageHandler(func(tick *msg.CNETMsg_Tick) {
fmt.Println("Net-message tick handled, unregistering - tick:", tick.Tick)
p.UnregisterNetMessageHandler(netTickHandlerID)
})
ts := time.Now()
frameByFrameCount := 1000
fmt.Printf("Parsing frame by frame (%d frames)\n", frameByFrameCount)
for i := 0; i < frameByFrameCount; i++ {
ok, errFrame := p.ParseNextFrame()
if errFrame != nil {
t.Fatal(errFrame)
}
if !ok {
t.Fatalf("Parser reported end of demo after less than %d frames", frameByFrameCount)
}
}
fmt.Println("Parsing to end")
err = p.ParseToEnd()
if err != nil {
t.Fatal(err)
}
fmt.Printf("Took %s\n", time.Since(ts))
}
func TestUnexpectedEndOfDemo(t *testing.T) {
if testing.Short() {
t.Skip("skipping test")
}
f, err := os.Open(unexpectedEndOfDemoPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
p := dem.NewParser(f)
err = p.ParseToEnd()
if err != dem.ErrUnexpectedEndOfDemo {
t.Fatal("Parsing cancelled but error was not ErrUnexpectedEndOfDemo:", err)
}
}
func TestCancelParseToEnd(t *testing.T) {
if testing.Short() {
t.Skip("skipping test")
}
f, err := os.Open(defaultDemPath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
p := dem.NewParser(f)
maxTicks := 100
var tix int
var handlerID dispatch.HandlerIdentifier
handlerID = p.RegisterEventHandler(func(events.FrameDone) {
tix++
if tix == maxTicks {
p.Cancel()
p.UnregisterEventHandler(handlerID)
}
})
err = p.ParseToEnd()
if err != dem.ErrCancelled {
t.Error("Parsing cancelled but error was not ErrCancelled:", err)
}
if tix > maxTicks {
t.Error("FrameDone handler was triggered after being unregistered")
}
}
func TestInvalidFileType(t *testing.T) {
invalidDemoData := make([]byte, 2048)
_, err := rand.Read(invalidDemoData)
if err != nil {
t.Fatal("Failed to read random data:", err)
}
msgWrongError := "Invalid demo but error was not ErrInvalidFileType:"
p := dem.NewParser(bytes.NewBuffer(invalidDemoData))
_, err = p.ParseHeader()
if err != dem.ErrInvalidFileType {
t.Fatal("ParseHeader():", msgWrongError, err)
}
p = dem.NewParser(bytes.NewBuffer(invalidDemoData))
_, err = p.ParseNextFrame()
if err != dem.ErrInvalidFileType {
t.Fatal("ParseNextFrame():", msgWrongError, err)
}
p = dem.NewParser(bytes.NewBuffer(invalidDemoData))
err = p.ParseToEnd()
if err != dem.ErrInvalidFileType {
t.Fatal("ParseToEnd():", msgWrongError, err)
}
}
func TestConcurrent(t *testing.T) {
if testing.Short() {
t.Skip("skipping test")
}
t.Logf("Running concurrency test with %d demos\n", concurrentDemos)
var i int64
runner := func() {
n := atomic.AddInt64(&i, 1)
fmt.Printf("Starting concurrent runner %d\n", n)
ts := time.Now()
parseDefaultDemo(t)
fmt.Printf("Runner %d took %s\n", n, time.Since(ts))
}
runConcurrently(runner)
}
func parseDefaultDemo(tb testing.TB) {
f, err := os.Open(defaultDemPath)
if err != nil {
tb.Fatal(err)
}
defer f.Close()
p := dem.NewParser(f)
err = p.ParseToEnd()
if err != nil {
tb.Fatal("Parsing failed:", err)
}
}
func runConcurrently(runner func()) {
var wg sync.WaitGroup
for i := 0; i < concurrentDemos; i++ {
wg.Add(1)
go func() { runner(); wg.Done() }()
}
wg.Wait()
}
func TestDemoSet(t *testing.T) {
if testing.Short() {
t.Skip("skipping test")
}
dems, err := ioutil.ReadDir(demSetPath)
if err != nil {
t.Fatal(err)
}
for _, d := range dems {
name := d.Name()
if strings.HasSuffix(name, ".dem") {
fmt.Printf("Parsing '%s/%s'\n", demSetPath, name)
func() {
var f *os.File
f, err = os.Open(demSetPath + "/" + name)
if err != nil {
t.Error(err)
}
defer f.Close()
defer func() {
pErr := recover()
if pErr != nil {
t.Errorf("Parsing of '%s/%s' paniced: %s\n", demSetPath, name, pErr)
}
}()
p := dem.NewParser(f)
err = p.ParseToEnd()
if err != nil {
t.Errorf("Parsing of '%s/%s' failed: %s\n", demSetPath, name, err)
return
}
}()
}
}
}
func BenchmarkDemoInfoCs(b *testing.B) {
for i := 0; i < b.N; i++ {
parseDefaultDemo(b)
}
}
func BenchmarkInMemory(b *testing.B) {
f, err := os.Open(defaultDemPath)
if err != nil {
b.Fatal(err)
}
defer f.Close()
inf, err := f.Stat()
if err != nil {
b.Fatal(err)
}
d := make([]byte, inf.Size())
n, err := f.Read(d)
if err != nil || int64(n) != inf.Size() {
b.Fatal(fmt.Sprintf("Expected %d bytes, got %d", inf.Size(), n), err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := dem.NewParser(bytes.NewReader(d))
err = p.ParseToEnd()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkConcurrent(b *testing.B) {
b.Logf("Running concurrency benchmark with %d demos\n", concurrentDemos)
for i := 0; i < b.N; i++ {
runConcurrently(func() { parseDefaultDemo(b) })
}
}