Skip to content

Commit

Permalink
Merge pull request #6 from vansante/ci
Browse files Browse the repository at this point in the history
Add comments indicated by golint +  Fix dataraces in go test
  • Loading branch information
vansante committed Dec 17, 2018
2 parents 88406fa + b56f6b6 commit dcd6ffb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 12 additions & 11 deletions emitter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eventemitter

import (
"sync/atomic"
"testing"
"time"
)
Expand All @@ -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)
Expand All @@ -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()
}
}
Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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{})
Expand Down

0 comments on commit dcd6ffb

Please sign in to comment.