-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
132 lines (107 loc) · 3.19 KB
/
router_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2018 Dan Jacques. All rights reserved.
// Use of this source code is governed under the MIT License
// that can be found in the LICENSE file.
package device
import (
"github.com/danjacques/gopushpixels/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Router", func() {
var r *Router
BeforeEach(func() {
r = &Router{
Registry: &Registry{},
}
})
AfterEach(func() {
r.Shutdown()
})
It("will return ErrNoRoute for unregistered devices", func() {
err := r.Route(InvalidOrdinal(), "nonexist", nil)
Expect(err).To(Equal(ErrNoRoute))
})
Context("with two registered devices", func() {
var d0, d1 *testD
BeforeEach(func() {
d0 = makeTestD("foo")
d0.ordinal.Group = 1
r.Registry.Add(d0)
d1 = makeTestD("bar")
d1.ordinal.Group = 2
r.Registry.Add(d1)
})
AfterEach(func() {
d0.markDone()
d1.markDone()
})
It("can route to both devices by ID", func() {
pktFoo := &protocol.Packet{}
err := r.Route(InvalidOrdinal(), "foo", pktFoo)
Expect(err).ToNot(HaveOccurred())
pktBar := &protocol.Packet{}
err = r.Route(InvalidOrdinal(), "bar", pktBar)
Expect(err).ToNot(HaveOccurred())
Expect(d0.packets).To(ConsistOf(pktFoo))
Expect(d1.packets).To(ConsistOf(pktFoo))
})
It("can route to both devices by Ordinal", func() {
// Group 1 == foo
pktFoo := &protocol.Packet{}
err := r.Route(Ordinal{Group: 1}, "nonexist", pktFoo)
Expect(err).ToNot(HaveOccurred())
// Group 1 == bar
pktBar := &protocol.Packet{}
err = r.Route(Ordinal{Group: 2}, "nonexist", pktBar)
Expect(err).ToNot(HaveOccurred())
Expect(d0.packets).To(ConsistOf(pktFoo))
Expect(d1.packets).To(ConsistOf(pktFoo))
})
It("routes to Ordinal before ID", func() {
// Group 1 == foo, but we use "bar" ID.
pktFoo := &protocol.Packet{}
err := r.Route(Ordinal{Group: 1}, "bar", pktFoo)
Expect(err).ToNot(HaveOccurred())
// Group 2 == bar, but we use "foo" ID.
pktBar := &protocol.Packet{}
err = r.Route(Ordinal{Group: 2}, "foo", pktBar)
Expect(err).ToNot(HaveOccurred())
Expect(d0.packets).To(ConsistOf(pktFoo))
Expect(d1.packets).To(ConsistOf(pktFoo))
})
Context("when connected to a Listener", func() {
type capturedPacket struct {
d D
pkt *protocol.Packet
}
var l Listener
var packets []capturedPacket
BeforeEach(func() {
packets = nil
l = ListenerFunc(func(d D, pkt *protocol.Packet) {
packets = append(packets, capturedPacket{d, pkt})
})
r.AddListener(l)
})
AfterEach(func() { r.RemoveListener(l) })
It("can send to a Listener", func() {
pkt := &protocol.Packet{}
err := r.Route(InvalidOrdinal(), "foo", pkt)
Expect(err).ToNot(HaveOccurred())
By("the packet was routed")
Expect(d0.packets).To(ConsistOf(pkt))
By("the listener received the packet")
Expect(packets).To(Equal([]capturedPacket{
{d: d0, pkt: pkt},
}))
})
It("when unroutable, the Listener does not receive the packet", func() {
pkt := &protocol.Packet{}
err := r.Route(InvalidOrdinal(), "nonexist", pkt)
Expect(err).To(Equal(ErrNoRoute))
By("the listener received the packet")
Expect(packets).To(BeEmpty())
})
})
})
})