From aaaff34448e7840940f1e015d667173855405954 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 28 Aug 2013 22:16:55 -0700 Subject: [PATCH] tests work for GOMAXPROCS>1 --- game.go | 4 ++++ id.go | 2 +- id_test.go | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 id_test.go diff --git a/game.go b/game.go index 8ba9a81..6634f85 100644 --- a/game.go +++ b/game.go @@ -17,6 +17,7 @@ type game struct { spectators map[*Spectator]bool sregister chan *Spectator sunregister chan *Spectator + kill chan bool } func NewGame() *game { @@ -30,6 +31,7 @@ func NewGame() *game { spectators: make(map[*Spectator]bool), sregister: make(chan *Spectator), sunregister: make(chan *Spectator), + kill: make(chan bool), } } @@ -66,6 +68,8 @@ func (g *game) run() { for { select { + case <-g.kill: + return case p := <-g.register: g.players[p] = true case p := <-g.unregister: diff --git a/id.go b/id.go index 89f48e7..22965fd 100644 --- a/id.go +++ b/id.go @@ -15,7 +15,7 @@ func NewIdGenerator() *IdGenerator { g := IdGenerator{} g.id = make(chan int64) go func() { - var i int64 + var i int64 for i = 0; ; i++ { g.id <- i } diff --git a/id_test.go b/id_test.go new file mode 100644 index 0000000..49f8206 --- /dev/null +++ b/id_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" +) + +func TestIDGenerator(t *testing.T) { + cache := make(map[string]bool) + var cur string + gg := NewIdGenerator() + for i := 0; i < 10000; i++ { + cur = gg.Hash() + if _, ok := cache[cur]; ok { + t.Errorf("unexpected duplicate key") + } + cache[cur] = true + } +}