Merge pull request #213 from iopred/fixtest

Fix handler tests.
This commit is contained in:
Bruce 2016-06-04 20:19:28 -05:00
commit 04c0add4fd

View file

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