-
Notifications
You must be signed in to change notification settings - Fork 1
/
broker_test.go
79 lines (68 loc) · 1.44 KB
/
broker_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
68
69
70
71
72
73
74
75
76
77
78
79
package gogadgets_test
import (
"fmt"
"sync"
"github.com/cswank/gogadgets"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type fakeGadget struct {
in chan gogadgets.Message
msgs []gogadgets.Message
lock sync.Mutex
}
func (f *fakeGadget) add(msg gogadgets.Message) {
f.lock.Lock()
f.msgs = append(f.msgs, msg)
f.lock.Unlock()
}
func (f *fakeGadget) len() int {
f.lock.Lock()
i := len(f.msgs)
f.lock.Unlock()
return i
}
func (f *fakeGadget) start() {
for {
msg := <-f.in
f.add(msg)
}
}
var _ = Describe("broker", func() {
var (
b *gogadgets.Broker
clients map[string]chan gogadgets.Message
in, out chan gogadgets.Message
gadgets []*fakeGadget
)
BeforeEach(func() {
in = make(chan gogadgets.Message)
out = make(chan gogadgets.Message)
clients = map[string]chan gogadgets.Message{
"x": make(chan gogadgets.Message),
"y": make(chan gogadgets.Message),
}
gadgets = []*fakeGadget{}
for _, n := range []string{"x", "y"} {
g := &fakeGadget{in: clients[n]}
go g.start()
gadgets = append(gadgets, g)
}
b = gogadgets.NewBroker(clients, in, out)
go b.Start()
})
Context("brokering", func() {
It("brokers", func() {
for i := 0; i < 10; i++ {
out <- gogadgets.Message{
Sender: "the test",
Type: "junk",
Body: fmt.Sprintf("%d", i),
}
}
Eventually(func() bool {
return gadgets[0].len() == 10 && gadgets[1].len() == 10
}).Should(BeTrue())
})
})
})