55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package lobby_test
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/lobby"
|
|
)
|
|
|
|
func TestQueue(t *testing.T) {
|
|
const N = 10000 // must be even
|
|
games := make([]int, 0, N)
|
|
ch := make(chan int)
|
|
pc := make(chan int, N)
|
|
l := lobby.New[int, int]()
|
|
var dealers, challs atomic.Int32
|
|
for i := 0; i < N; i++ {
|
|
i := i
|
|
new := func() int { return i }
|
|
go func() {
|
|
id, j, deal := l.Queue(context.Background(), new, i)
|
|
if deal {
|
|
dealers.Add(1)
|
|
} else {
|
|
challs.Add(1)
|
|
}
|
|
ch <- id
|
|
pc <- j
|
|
}()
|
|
}
|
|
for i := 0; i < N; i++ {
|
|
games = append(games, <-ch)
|
|
}
|
|
// Every unique game ID should appear exactly twice.
|
|
counts := make(map[int]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 unique challenger info should appear exactly twice.
|
|
ps := make(map[int]int, N/2)
|
|
for i := 0; i < N; i++ {
|
|
ps[<-pc]++
|
|
}
|
|
// 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())
|
|
}
|
|
}
|