Fix handler tests.

This commit is contained in:
Chris Rhodes 2016-06-04 18:13:46 -07:00
parent 323216e171
commit 84d430918b

View file

@ -3,6 +3,7 @@ package discordgo
import ( import (
"os" "os"
"runtime" "runtime"
"sync/atomic"
"testing" "testing"
"time" "time"
) )
@ -226,14 +227,14 @@ func TestOpenClose(t *testing.T) {
} }
func TestAddHandler(t *testing.T) { func TestAddHandler(t *testing.T) {
testHandlerCalled := 0 testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) { testHandler := func(s *Session, m *MessageCreate) {
testHandlerCalled++ atomic.AddInt32(&testHandlerCalled, 1)
} }
interfaceHandlerCalled := 0 interfaceHandlerCalled := int32(0)
interfaceHandler := func(s *Session, i interface{}) { interfaceHandler := func(s *Session, i interface{}) {
interfaceHandlerCalled++ atomic.AddInt32(&interfaceHandlerCalled, 1)
} }
bogusHandlerCalled := false bogusHandlerCalled := false
@ -251,6 +252,8 @@ func TestAddHandler(t *testing.T) {
d.handle(&MessageCreate{}) d.handle(&MessageCreate{})
d.handle(&MessageDelete{}) d.handle(&MessageDelete{})
<-time.After(100 * time.Millisecond)
// testHandler will be called twice because it was added twice. // testHandler will be called twice because it was added twice.
if testHandlerCalled != 2 { if testHandlerCalled != 2 {
t.Fatalf("testHandler was not called twice.") t.Fatalf("testHandler was not called twice.")
@ -267,9 +270,9 @@ func TestAddHandler(t *testing.T) {
} }
func TestRemoveHandler(t *testing.T) { func TestRemoveHandler(t *testing.T) {
testHandlerCalled := 0 testHandlerCalled := int32(0)
testHandler := func(s *Session, m *MessageCreate) { testHandler := func(s *Session, m *MessageCreate) {
testHandlerCalled++ atomic.AddInt32(&testHandlerCalled, 1)
} }
d := Session{} d := Session{}
@ -281,6 +284,8 @@ func TestRemoveHandler(t *testing.T) {
d.handle(&MessageCreate{}) d.handle(&MessageCreate{})
<-time.After(100 * time.Millisecond)
// testHandler will be called once, as it was removed in between calls. // testHandler will be called once, as it was removed in between calls.
if testHandlerCalled != 1 { if testHandlerCalled != 1 {
t.Fatalf("testHandler was not called once.") t.Fatalf("testHandler was not called once.")