-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbus_test.go
67 lines (57 loc) · 1.42 KB
/
eventbus_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
61
62
63
64
65
66
67
package eventbus_test
import (
"github.com/bmizerany/assert"
"github.com/jamesharr/eventbus"
"testing"
)
type receiver struct {
input chan eventbus.Message
results chan []eventbus.Message
}
func makeReceiver() receiver {
input := make(chan eventbus.Message)
received_messages := make(chan []eventbus.Message)
go func() {
messages := make([]eventbus.Message, 0)
for {
msg, ok := <-input
if ok {
messages = append(messages, msg)
} else {
break
}
}
received_messages <- messages
close(received_messages)
}()
return receiver{input, received_messages}
}
func TestBusEmpty(t *testing.T) {
eb := eventbus.CreateEventBus()
eb.Close()
}
func TestBusShort(t *testing.T) {
message_list := []eventbus.Message{1, 5, 7, 3, 5, 8, 9, 90, -1, "HAI"}
eb := eventbus.CreateEventBus()
h1 := makeReceiver()
eb.Register(h1.input)
for _, msg := range message_list {
eb.Emit(msg)
}
eb.Close()
assert.Equal(t, message_list, <-h1.results, "Handler 1 received items")
}
func TestMultipleListeners(t *testing.T) {
message_list := []eventbus.Message{1, 5, 7, 3, 5, 8, 9, 90, -1, "HAI"}
eb := eventbus.CreateEventBus()
h1 := makeReceiver()
eb.Register(h1.input)
h2 := makeReceiver()
eb.Register(h2.input)
for _, msg := range message_list {
eb.Emit(msg)
}
eb.Close()
assert.Equal(t, message_list, <-h1.results, "Handler 1 received items")
assert.Equal(t, message_list, <-h2.results, "Handler 2 received items")
}