harness-drone/engine/bus_test.go

51 lines
955 B
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package engine
2015-04-28 22:30:51 +00:00
import (
"testing"
2015-09-30 01:21:17 +00:00
. "github.com/franela/goblin"
2015-04-28 22:30:51 +00:00
)
2015-09-30 01:21:17 +00:00
func TestBus(t *testing.T) {
2015-04-28 22:30:51 +00:00
g := Goblin(t)
2015-09-30 01:21:17 +00:00
g.Describe("Event bus", func() {
2015-04-28 22:30:51 +00:00
g.It("Should unsubscribe", func() {
2015-09-30 01:21:17 +00:00
c1 := make(chan *Event)
c2 := make(chan *Event)
b := newEventbus()
b.subscribe(c1)
b.subscribe(c2)
2015-04-28 22:30:51 +00:00
g.Assert(len(b.subs)).Equal(2)
})
g.It("Should subscribe", func() {
2015-09-30 01:21:17 +00:00
c1 := make(chan *Event)
c2 := make(chan *Event)
b := newEventbus()
b.subscribe(c1)
b.subscribe(c2)
2015-04-28 22:30:51 +00:00
g.Assert(len(b.subs)).Equal(2)
2015-09-30 01:21:17 +00:00
b.unsubscribe(c1)
b.unsubscribe(c2)
2015-04-28 22:30:51 +00:00
g.Assert(len(b.subs)).Equal(0)
})
g.It("Should send", func() {
2015-08-20 17:57:46 +00:00
em := map[string]bool{"foo": true, "bar": true}
2015-09-30 01:21:17 +00:00
e1 := &Event{Name: "foo"}
e2 := &Event{Name: "bar"}
c := make(chan *Event)
b := newEventbus()
b.subscribe(c)
b.send(e1)
b.send(e2)
2015-04-28 22:30:51 +00:00
r1 := <-c
r2 := <-c
2015-08-20 17:57:46 +00:00
g.Assert(em[r1.Name]).Equal(true)
g.Assert(em[r2.Name]).Equal(true)
2015-04-28 22:30:51 +00:00
})
})
}