shotgun/lobby/match_test.go

50 lines
1.1 KiB
Go
Raw Normal View History

2024-01-21 19:53:08 -06:00
package lobby_test
import (
"context"
"testing"
"git.sunturtle.xyz/studio/shotgun/lobby"
"git.sunturtle.xyz/studio/shotgun/player"
)
func TestQueue(t *testing.T) {
const N = 1000 // must be even
games := make([]lobby.GameID, 0, N)
ch := make(chan lobby.GameID)
l := lobby.New()
for i := 0; i < 100; i++ {
games = games[:0]
for i := 0; i < N; i++ {
i := i
go func() {
ch <- l.Queue(context.Background(), player.ID{uint8(i), uint8(i >> 8)})
}()
}
for i := 0; i < N; i++ {
games = append(games, <-ch)
}
// Every unique game ID should appear exactly twice.
counts := make(map[lobby.GameID]int, N/2)
for _, id := range games {
counts[id]++
}
for id, c := range counts {
if c != 2 {
t.Errorf("game %v appears %d times", id, c)
}
}
// Every game should have two different players.
for _, id := range games {
g := l.Game(id)
if g == nil {
t.Errorf("game %v was created but doesn't exist", g)
continue
}
if g.CurrentPlayer().Is(g.Opponent()) {
t.Errorf("game %v matched with self", id)
}
}
}
}