test cov for eventbus
This commit is contained in:
parent
b84943222b
commit
08527d5aea
3 changed files with 62 additions and 9 deletions
2
drone.go
2
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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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)
|
51
eventbus/builtin/bus_test.go
Normal file
51
eventbus/builtin/bus_test.go
Normal file
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
}
|
Loading…
Reference in a new issue