Skip to content

Commit

Permalink
Merge pull request #1 from hyponet/fix/wildcard
Browse files Browse the repository at this point in the history
fix: wildcard event deliver
  • Loading branch information
hyponet authored Apr 17, 2024
2 parents 128b42e + dd387f1 commit f32706c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
15 changes: 4 additions & 11 deletions bus/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func lookupRadixTree(root *radixNode, topic string) (values []string) {
for crt != nil {
prefix := strings.Split(crt.prefix, sectionDelimiter)
subSections := sections[idx:]
m, isWildcard := getSameSectionsWithWildcard(prefix, sections[idx:])
m, _ := getSameSectionsWithWildcard(prefix, sections[idx:])
if m == 0 {
crt = crt.next
continue
Expand All @@ -189,19 +189,12 @@ func lookupRadixTree(root *radixNode, topic string) (values []string) {

if m < len(subSections) {
queue = append(queue, pos{idx: idx + m, next: crt.children})
if isWildcard {
crt = crt.next
continue
}
break
}

values = append(values, crt.values...)
if isWildcard {
crt = crt.next
continue
}
break

values = append(values, crt.values...)
crt = crt.next
}
}

Expand Down
23 changes: 23 additions & 0 deletions bus/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ var _ = Describe("TestExchangeRoute", func() {
Expect(isContained(ex.route("a.b.*.*.f"), "id-6")).Should(BeTrue())
})
})

It("test route wildcard", func() {
Context("add topics", func() {
ex.add("a.b.c.d", "id-1")
ex.add("a.b.c.e", "id-2")
ex.add("a.b.c.*", "id-3")
ex.add("a.b.d.e.f", "id-4")
ex.add("a.b.d.*.f", "id-5")
ex.add("a.b.d.*.*", "id-6")
})
Context("add topics", func() {
Expect(isContained(ex.route("a.b.c.*"), "id-1")).Should(BeTrue())
Expect(isContained(ex.route("a.b.c.*"), "id-2")).Should(BeTrue())
Expect(isContained(ex.route("a.b.c.*"), "id-3")).Should(BeTrue())

Expect(isContained(ex.route("a.b.c.d"), "id-1")).Should(BeTrue())
Expect(isContained(ex.route("a.b.c.d"), "id-3")).Should(BeTrue())

Expect(isContained(ex.route("a.b.d.e.f"), "id-4")).Should(BeTrue())
Expect(isContained(ex.route("a.b.d.e.f"), "id-5")).Should(BeTrue())
Expect(isContained(ex.route("a.b.d.e.f"), "id-6")).Should(BeTrue())
})
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hyponet/eventbus

go 1.18
go 1.20

require (
github.com/google/uuid v1.3.0
Expand Down

0 comments on commit f32706c

Please sign in to comment.