Atomic incrementing of call count for dispatch test (1->0).

This probably could be done better with channels...
This commit is contained in:
Alex Bramley 2013-09-30 14:49:32 +01:00
parent 4eaad0e95e
commit d0606981cb
1 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package client package client
import ( import (
"sync/atomic"
"testing" "testing"
"time" "time"
) )
@ -16,9 +17,9 @@ func TestHandlerSet(t *testing.T) {
t.Errorf("New set contains things!") t.Errorf("New set contains things!")
} }
callcount := 0 callcount := new(int32)
f := func(_ *Conn, _ *Line) { f := func(_ *Conn, _ *Line) {
callcount++ atomic.AddInt32(callcount, 1)
} }
// Add one // Add one
@ -85,12 +86,12 @@ func TestHandlerSet(t *testing.T) {
} }
// Dispatch should result in 4 additions. // Dispatch should result in 4 additions.
if callcount != 0 { if atomic.LoadInt32(callcount) != 0 {
t.Errorf("Something incremented call count before we were expecting it.") t.Errorf("Something incremented call count before we were expecting it.")
} }
hs.dispatch(c, &Line{Cmd: "One"}) hs.dispatch(c, &Line{Cmd: "One"})
<-time.After(time.Millisecond) <-time.After(time.Millisecond)
if callcount != 4 { if atomic.LoadInt32(callcount) != 4 {
t.Errorf("Our handler wasn't called four times :-(") t.Errorf("Our handler wasn't called four times :-(")
} }
@ -114,7 +115,7 @@ func TestHandlerSet(t *testing.T) {
// Dispatch should result in 3 additions. // Dispatch should result in 3 additions.
hs.dispatch(c, &Line{Cmd: "One"}) hs.dispatch(c, &Line{Cmd: "One"})
<-time.After(time.Millisecond) <-time.After(time.Millisecond)
if callcount != 7 { if atomic.LoadInt32(callcount) != 7 {
t.Errorf("Our handler wasn't called three times :-(") t.Errorf("Our handler wasn't called three times :-(")
} }
@ -136,7 +137,7 @@ func TestHandlerSet(t *testing.T) {
// Dispatch should result in 2 additions. // Dispatch should result in 2 additions.
hs.dispatch(c, &Line{Cmd: "One"}) hs.dispatch(c, &Line{Cmd: "One"})
<-time.After(time.Millisecond) <-time.After(time.Millisecond)
if callcount != 9 { if atomic.LoadInt32(callcount) != 9 {
t.Errorf("Our handler wasn't called two times :-(") t.Errorf("Our handler wasn't called two times :-(")
} }
@ -158,7 +159,7 @@ func TestHandlerSet(t *testing.T) {
// Dispatch should result in 1 addition. // Dispatch should result in 1 addition.
hs.dispatch(c, &Line{Cmd: "One"}) hs.dispatch(c, &Line{Cmd: "One"})
<-time.After(time.Millisecond) <-time.After(time.Millisecond)
if callcount != 10 { if atomic.LoadInt32(callcount) != 10 {
t.Errorf("Our handler wasn't called once :-(") t.Errorf("Our handler wasn't called once :-(")
} }
@ -177,7 +178,7 @@ func TestHandlerSet(t *testing.T) {
// Dispatch should result in NO additions. // Dispatch should result in NO additions.
hs.dispatch(c, &Line{Cmd: "One"}) hs.dispatch(c, &Line{Cmd: "One"})
<-time.After(time.Millisecond) <-time.After(time.Millisecond)
if callcount != 10 { if atomic.LoadInt32(callcount) != 10 {
t.Errorf("Our handler was called?") t.Errorf("Our handler was called?")
} }
} }