2024-01-21 20:53:08 -05:00
|
|
|
package lobby_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-27 10:43:55 -05:00
|
|
|
"sync/atomic"
|
2024-01-21 20:53:08 -05:00
|
|
|
"testing"
|
|
|
|
|
2024-01-31 23:40:12 -05:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
2024-01-21 20:53:08 -05:00
|
|
|
"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]
|
2024-01-27 10:43:55 -05:00
|
|
|
var dealers, challs atomic.Int32
|
2024-01-21 20:53:08 -05:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
i := i
|
|
|
|
go func() {
|
2024-01-31 23:40:12 -05:00
|
|
|
id, _, deal := l.Queue(context.Background(), player.ID{UUID: uuid.UUID{uint8(i), uint8(i >> 8)}})
|
2024-01-27 10:43:55 -05:00
|
|
|
if deal {
|
|
|
|
dealers.Add(1)
|
|
|
|
} else {
|
|
|
|
challs.Add(1)
|
|
|
|
}
|
|
|
|
ch <- id
|
2024-01-21 20:53:08 -05:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 10:43:55 -05:00
|
|
|
// The number of dealers must match the number of challengers.
|
|
|
|
if dealers.Load() != challs.Load() {
|
|
|
|
t.Errorf("%d dealers != %d challengers", dealers.Load(), challs.Load())
|
2024-01-21 20:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|