From 08527d5aea0abee1e6dd4c6225330fbe9f6e3f85 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 28 Apr 2015 15:30:51 -0700 Subject: [PATCH] test cov for eventbus --- drone.go | 2 +- eventbus/{eventbus.go => builtin/bus.go} | 18 +++++---- eventbus/builtin/bus_test.go | 51 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) rename eventbus/{eventbus.go => builtin/bus.go} (63%) create mode 100644 eventbus/builtin/bus_test.go diff --git a/drone.go b/drone.go index c4fc0ffd..4c457326 100644 --- a/drone.go +++ b/drone.go @@ -8,13 +8,13 @@ import ( "github.com/gin-gonic/gin" "github.com/drone/drone/datastore/bolt" - "github.com/drone/drone/eventbus" "github.com/drone/drone/remote/github" "github.com/drone/drone/server" "github.com/drone/drone/server/session" "github.com/drone/drone/settings" "github.com/elazarl/go-bindata-assetfs" + eventbus "github.com/drone/drone/eventbus/builtin" queue "github.com/drone/drone/queue/builtin" ) diff --git a/eventbus/eventbus.go b/eventbus/builtin/bus.go similarity index 63% rename from eventbus/eventbus.go rename to eventbus/builtin/bus.go index c08666d3..f7081305 100644 --- a/eventbus/eventbus.go +++ b/eventbus/builtin/bus.go @@ -1,26 +1,28 @@ -package eventbus +package builtin import ( "sync" + + "github.com/drone/drone/eventbus" ) type EventBus struct { sync.Mutex - subs map[chan *Event]bool + subs map[chan *eventbus.Event]bool } // New creates a new EventBus that manages a list of // subscribers to which events are published. func New() *EventBus { return &EventBus{ - subs: make(map[chan *Event]bool), + subs: make(map[chan *eventbus.Event]bool), } } // Subscribe adds the channel to the list of // subscribers. Each subscriber in the list will // receive broadcast events. -func (b *EventBus) Subscribe(c chan *Event) { +func (b *EventBus) Subscribe(c chan *eventbus.Event) { b.Lock() b.subs[c] = true b.Unlock() @@ -28,19 +30,19 @@ func (b *EventBus) Subscribe(c chan *Event) { // Unsubscribe removes the channel from the // list of subscribers. -func (b *EventBus) Unsubscribe(c chan *Event) { +func (b *EventBus) Unsubscribe(c chan *eventbus.Event) { b.Lock() delete(b.subs, c) b.Unlock() } // Send dispatches a message to all subscribers. -func (b *EventBus) Send(event *Event) { +func (b *EventBus) Send(event *eventbus.Event) { b.Lock() defer b.Unlock() - for s, _ := range b.subs { - go func(c chan *Event) { + for s := range b.subs { + go func(c chan *eventbus.Event) { defer recover() c <- event }(s) diff --git a/eventbus/builtin/bus_test.go b/eventbus/builtin/bus_test.go new file mode 100644 index 00000000..1ce21584 --- /dev/null +++ b/eventbus/builtin/bus_test.go @@ -0,0 +1,51 @@ +package builtin + +import ( + "testing" + + "github.com/drone/drone/common" + "github.com/drone/drone/eventbus" + . "github.com/franela/goblin" +) + +func TestBuild(t *testing.T) { + g := Goblin(t) + g.Describe("Bus", func() { + + g.It("Should unsubscribe", func() { + c1 := make(chan *eventbus.Event) + c2 := make(chan *eventbus.Event) + b := New() + b.Subscribe(c1) + b.Subscribe(c2) + g.Assert(len(b.subs)).Equal(2) + }) + + g.It("Should subscribe", func() { + c1 := make(chan *eventbus.Event) + c2 := make(chan *eventbus.Event) + b := New() + b.Subscribe(c1) + b.Subscribe(c2) + g.Assert(len(b.subs)).Equal(2) + b.Unsubscribe(c1) + b.Unsubscribe(c2) + g.Assert(len(b.subs)).Equal(0) + }) + + g.It("Should send", func() { + e1 := &eventbus.Event{Repo: &common.Repo{Name: "foo"}} + e2 := &eventbus.Event{Repo: &common.Repo{Name: "bar"}} + c := make(chan *eventbus.Event) + b := New() + b.Subscribe(c) + b.Send(e1) + b.Send(e2) + r1 := <-c + r2 := <-c + g.Assert(e1).Equal(r1) + g.Assert(e2).Equal(r2) + }) + }) + +}