From 333e4f098274347eda2fab9ffec900ebdeb5b339 Mon Sep 17 00:00:00 2001 From: Paul van Santen Date: Mon, 17 Dec 2018 08:39:01 +0100 Subject: [PATCH 1/2] Add comments indicated by golint --- emitter.go | 1 + interface.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/emitter.go b/emitter.go index 78ad340..9e514fb 100644 --- a/emitter.go +++ b/emitter.go @@ -4,6 +4,7 @@ import ( "sync" ) +// Emitter is the base struct which manages event subscriptions and calls all registered handlers on event emits. type Emitter struct { sync.RWMutex diff --git a/interface.go b/interface.go index d1670ef..35d9a98 100644 --- a/interface.go +++ b/interface.go @@ -1,5 +1,6 @@ package eventemitter +// EventType is a type of event, each type of event should have a different type type EventType string // HandleFunc is a handler function for a given event type @@ -32,6 +33,7 @@ type Observable interface { RemoveCapturer(capturer *Capturer) } +// EventEmitter is the interface which allows implementers to emit events type EventEmitter interface { // EmitEvent emits the given event to all listeners and capturers EmitEvent(event EventType, arguments ...interface{}) From b56f6b6736a97c167e4cb9c8268717b378bee105 Mon Sep 17 00:00:00 2001 From: Paul van Santen Date: Mon, 17 Dec 2018 08:49:04 +0100 Subject: [PATCH 2/2] Fix dataraces in go test --- emitter_test.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/emitter_test.go b/emitter_test.go index 546da74..3a0bcd1 100644 --- a/emitter_test.go +++ b/emitter_test.go @@ -1,6 +1,7 @@ package eventemitter import ( + "sync/atomic" "testing" "time" ) @@ -21,21 +22,21 @@ func testEmitter(t *testing.T, async bool) { em = e ob = e - var ASingle, AListener, capture int + var ASingle, AListener, capture int32 listener := ob.AddListener("test event A", func(args ...interface{}) { verifyArgs(t, args) - AListener++ + atomic.AddInt32(&AListener, 1) }) ob.ListenOnce("test event A", func(args ...interface{}) { verifyArgs(t, args) - ASingle++ + atomic.AddInt32(&ASingle, 1) }) capturer := ob.AddCapturer(func(event EventType, args ...interface{}) { verifyArgs(t, args) - capture++ + atomic.AddInt32(&capture, 1) }) em.EmitEvent("test event A", "test", 123, true) @@ -53,19 +54,19 @@ func testEmitter(t *testing.T, async bool) { if async { // Events are async, so wait a bit for them to finish - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * 200) } - if ASingle != 1 { - t.Log("Single A event not triggered right", ASingle) + if atomic.LoadInt32(&ASingle) != 1 { + t.Log("Single A event not triggered right", atomic.LoadInt32(&ASingle)) t.Fail() } - if AListener != 3 { - t.Log("A event not triggered right", AListener) + if atomic.LoadInt32(&AListener) != 3 { + t.Log("A event not triggered right", atomic.LoadInt32(&AListener)) t.Fail() } - if capture != 5 { - t.Log("Capture all not triggered right", capture) + if atomic.LoadInt32(&capture) != 5 { + t.Log("Capture all not triggered right", atomic.LoadInt32(&capture)) t.Fail() } }