From 84d430918b65d0812c212ff5c8004fee1d560f6d Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Sat, 4 Jun 2016 18:13:46 -0700 Subject: [PATCH] Fix handler tests. --- discord_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/discord_test.go b/discord_test.go index d2b497f..7aa31ed 100644 --- a/discord_test.go +++ b/discord_test.go @@ -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.")