-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcaster_test.go
60 lines (44 loc) · 1.05 KB
/
broadcaster_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package goarken
import (
. "github.com/smartystreets/goconvey/convey"
"sync"
"testing"
)
type Listener struct {
HasBeenCalled bool
}
func Test_Broadcaster(t *testing.T) {
var b *Broadcaster
Convey("Given a broadcaster", t, func() {
var wg sync.WaitGroup
b = NewBroadcaster()
l1 := &Listener{HasBeenCalled: false}
l2 := &Listener{HasBeenCalled: false}
l3 := &Listener{HasBeenCalled: false}
channel1 := b.Listen()
channel2 := b.Listen()
wg.Add(2)
go func() {
defer wg.Done()
<-channel1
l1.HasBeenCalled = true
}()
go func() {
defer wg.Done()
<-channel2
l2.HasBeenCalled = true
}()
So(l1.HasBeenCalled, ShouldEqual, false)
So(l2.HasBeenCalled, ShouldEqual, false)
So(l3.HasBeenCalled, ShouldEqual, false)
Convey("When one send an event to it", func() {
b.Write(&struct{}{})
wg.Wait()
Convey("Then the listener has received the message", func() {
So(l1.HasBeenCalled, ShouldEqual, true)
So(l2.HasBeenCalled, ShouldEqual, true)
So(l3.HasBeenCalled, ShouldEqual, false)
})
})
})
}