-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtesting.go
141 lines (117 loc) · 3.84 KB
/
testing.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
package main
import (
"context"
"fmt"
"log/slog"
"math/rand"
"os"
"strings"
"time"
"github.com/leighmacdonald/bd/rules"
"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/steamweb/v2"
)
type mkPlayerFunc func(ctx context.Context, sid64 steamid.SteamID) (PlayerState, error)
// CreateTestPlayers will generate fake player data for testing purposes.
// nolint:gosec
func CreateTestPlayers(playerState *playerStates, fn mkPlayerFunc, count int) {
idIdx := 0
knownIDs := steamid.Collection{
steamid.New("76561197998365611"), steamid.New("76561197977133523"), steamid.New("76561198065825165"),
steamid.New("76561198004429398"), steamid.New("76561198182505218"), steamid.New("76561197989961569"),
steamid.New("76561198183927541"), steamid.New("76561198005026984"), steamid.New("76561197997861796"),
steamid.New("76561198377596915"), steamid.New("76561198336028289"), steamid.New("76561198066637626"),
steamid.New("76561198818013048"), steamid.New("76561198196411029"), steamid.New("76561198079544034"),
steamid.New("76561198008337801"), steamid.New("76561198042902038"), steamid.New("76561198013287458"),
steamid.New("76561198038487121"), steamid.New("76561198046766708"), steamid.New("76561197963310062"),
steamid.New("76561198017314810"), steamid.New("76561197967842214"), steamid.New("76561197984047970"),
steamid.New("76561198020124821"), steamid.New("76561198010868782"), steamid.New("76561198022397372"),
steamid.New("76561198016314731"), steamid.New("76561198087124802"), steamid.New("76561198024022137"),
steamid.New("76561198015577906"), steamid.New("76561197997861796"),
}
randPlayer := func(userId int) PlayerState {
team := Blu
if userId%2 == 0 {
team = Red
}
player, errPlayer := fn(context.Background(), knownIDs[idIdx])
if errPlayer != nil {
panic(errPlayer)
}
if player.Personaname == "" {
player.Personaname = fmt.Sprintf("%d - %s", userId, player.SteamID.String())
}
player.Visibility = int64(steamweb.VisibilityPublic)
player.KillsOn = int64(rand.Intn(20))
player.RageQuits = int64(rand.Intn(10))
player.DeathsBy = int64(rand.Intn(20))
player.Team = team
player.Connected = time.Duration(rand.Intn(3600) * 1000000)
player.UserID = userId
player.Ping = rand.Intn(150)
player.Kills = rand.Intn(50)
player.Deaths = rand.Intn(300)
idIdx++
return player
}
var testPlayers []PlayerState
for i := 0; i < count; i++ {
player := randPlayer(i)
switch i {
case 1:
player.VacBans = 2
player.Notes = "User notes \ngo here"
last := time.Now().AddDate(-1, 0, 0).Unix()
player.LastVacBanOn = last
case 4:
player.Matches = append(player.Matches, rules.MatchResult{
Origin: "Test Rules List",
Attributes: []string{"cheater"},
MatcherType: "string",
})
case 6:
player.Matches = append(player.Matches, rules.MatchResult{
Origin: "Test Rules List",
Attributes: []string{"other"},
MatcherType: "string",
})
case 7:
player.Team = Spec
}
testPlayers = append(testPlayers, player)
}
playerState.replace(testPlayers)
}
func testLogFeeder(ctx context.Context, ingest *logIngest) {
testLogPath, found := os.LookupEnv("TEST_CONSOLE_LOG")
if !found {
return
}
logPath := "testdata/console.log"
if testLogPath != "" {
logPath = testLogPath
}
body, errRead := os.ReadFile(logPath)
if errRead != nil {
slog.Error("Failed to load TEST_CONSOLE_LOG", slog.String("path", logPath), errAttr(errRead))
return
}
lines := strings.Split(string(body), "\n")
curLine := 0
lineCount := len(lines)
// Delay the incoming data a bit so its more realistic
updateTicker := time.NewTicker(time.Millisecond * 100)
for {
select {
case <-updateTicker.C:
ingest.external <- lines[curLine]
curLine++
// Wrap back around once we are done
if curLine >= lineCount {
curLine = 0
}
case <-ctx.Done():
return
}
}
}