Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix callbacks being replaced on topics with wildcards #388

Merged
merged 4 commits into from Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions router.go
Expand Up @@ -99,7 +99,7 @@ func (r *router) addRoute(topic string, callback MessageHandler) {
r.Lock()
defer r.Unlock()
for e := r.routes.Front(); e != nil; e = e.Next() {
if e.Value.(*route).match(topic) {
if e.Value.(*route).topic == topic {
r := e.Value.(*route)
r.callback = callback
return
Expand All @@ -114,7 +114,7 @@ func (r *router) deleteRoute(topic string) {
r.Lock()
defer r.Unlock()
for e := r.routes.Front(); e != nil; e = e.Next() {
if e.Value.(*route).match(topic) {
if e.Value.(*route).topic == topic {
r.routes.Remove(e)
return
}
Expand Down
26 changes: 26 additions & 0 deletions unit_router_test.go
Expand Up @@ -44,6 +44,32 @@ func Test_AddRoute(t *testing.T) {
}
}

func Test_AddRoute_Wildcards(t *testing.T) {
router, _ := newRouter()
cb := func(client Client, msg Message) {
}
router.addRoute("#", cb)
router.addRoute("topic1", cb)

if router.routes.Len() != 2 {
t.Fatalf("addRoute should only override routes on exact topic match")
}
}

func Test_DeleteRoute_Wildcards(t *testing.T) {
router, _ := newRouter()
cb := func(client Client, msg Message) {
}
router.addRoute("#", cb)
router.addRoute("topic1", cb)
router.deleteRoute("topic1")

expected := "#"
got := router.routes.Front().Value.(*route).topic; if !(router.routes.Front().Value.(*route).topic == "#") {
t.Fatalf("deleteRoute deleted wrong route when wildcards are used, got topic '%s', expected route with topic '%s'", got, expected)
}
}

func Test_Match(t *testing.T) {
router, _ := newRouter()
router.addRoute("/alpha", nil)
Expand Down