-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
208 lines (180 loc) · 3.92 KB
/
tree_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Filters
#
+/tennis/#
sport/#
sport/tennis/player1/#
should all match the following topics
sport/tennis/player1
sport/tennis/player1/ranking
sport/tennis/player1/score/wimbledon
https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901241
*/
package arn
import (
"bytes"
"reflect"
"testing"
"github.com/gregoryv/golden"
)
func TestTree_MatchMiddlePlus(t *testing.T) {
filter := "gopher/+/hat"
x := NewTree(filter)
var result []*Node
topic := "gopher/pink/hat"
x.Match(&result, topic)
if len(result) == 0 {
t.Fatal(filter, "should match", topic)
}
got := result[0].Filter()
if got != filter {
t.Error(x.Filters(), "should match", topic)
}
}
func TestTree_MatchTopicUsedInManual(t *testing.T) {
x := NewTree("gopher/+")
var result []*Node
topic := "gopher/pink"
x.Match(&result, topic)
if len(result) != 1 {
t.Error(x.Filters(), "should match", topic)
}
}
func TestTree_Modify(t *testing.T) {
x := NewTree()
x.AddFilter("sport/#")
x.Modify("a/b/c", func(n *Node) {
t.Error("modify called on", n)
})
x.Modify("sport/golf", func(n *Node) {
t.Error("modify called on", n)
})
var called bool
filter := "sport/#"
x.Modify(filter, func(n *Node) {
called = true
})
if !called {
t.Error("modify not called for", filter)
}
}
func TestTree_Find(t *testing.T) {
x := NewTree()
n, found := x.Find("")
if found || n != nil {
t.Error("Find returned", n, found)
}
n, found = x.Find("no/such/filter")
if found || n != nil {
t.Error("Find returned", n, found)
}
filter := "store/candy/door/#"
x.AddFilter(filter)
n, found = x.Find(filter)
if !found || n == nil {
t.Error("Find returned", n, found)
}
}
func TestTree_NoMatch(t *testing.T) {
x := newTestTree()
var result []*Node
topic := "store/fruit/apple"
x.Match(&result, topic)
if len(result) != 1 {
t.Log("filters: ", x.Filters())
t.Errorf("%s should only match one filter", topic)
}
}
func TestTree_Filters(t *testing.T) {
x := newTestTree()
var buf bytes.Buffer
for _, f := range x.Filters() {
buf.WriteString(f)
buf.WriteString("\n")
}
golden.Assert(t, buf.String())
}
func newTestTree() *Tree {
x := NewTree()
x.AddFilter("#")
x.AddFilter("+/tennis/#")
x.AddFilter("sport/#")
x.AddFilter("sport/tennis/player1/#")
return x
}
func TestRouter(t *testing.T) {
t.Run("Tree", func(t *testing.T) {
testRouterMatch(t, NewTree())
})
}
func testRouterMatch(t *testing.T, r Router) {
t.Helper()
exp := []string{
"#",
"+/tennis/#",
"sport/#",
"sport/tennis/player1/#",
}
for _, filter := range exp {
r.AddFilter(filter)
}
topics := []string{
"sport/tennis/player1",
"sport/tennis/player1/ranking",
"sport/tennis/player1/score/wimbledon",
}
var filters []string
var result []*Node
for _, topic := range topics {
t.Run(topic, func(t *testing.T) {
result = result[:0] // reset
r.Match(&result, topic)
filters = filters[:0] // reset
for _, n := range result {
filters = append(filters, n.Filter())
}
if !reflect.DeepEqual(filters, exp) {
t.Error("\ntopic: ", topic, "\nmatch: ", filters, "\nexpect:", exp)
}
})
}
t.Run("$sys", func(t *testing.T) {
var result []*Node
topic := "$sys/health"
r.Match(&result, topic)
if len(result) > 0 {
t.Error("$sys should not match", r.(*Tree).Filters())
}
})
}
type Router interface {
AddFilter(string) *Node
Match(result *[]*Node, topic string)
}
func BenchmarkTree_Match(b *testing.B) {
benchmarkRouterMatch(b, NewTree())
}
func benchmarkRouterMatch(b *testing.B, r Router) {
b.Helper()
exp := []string{
"#",
"+/tennis/#",
"sport/#",
"sport/tennis/player1/#",
}
for _, filter := range exp {
r.AddFilter(filter)
}
topics := []string{
"sport/tennis/player1",
"sport/tennis/player1/ranking",
"sport/tennis/player1/score/wimbledon",
}
var result []*Node // could make result pooled
for i := 0; i < b.N; i++ {
for _, topic := range topics {
result = result[:0] // reset
r.Match(&result, topic)
}
}
}