Skip to content

Commit

Permalink
Merge pull request #388 from v-zhuravlev/master
Browse files Browse the repository at this point in the history
Fix callbacks being replaced on topics with wildcards
  • Loading branch information
Al S-M committed Dec 20, 2019
2 parents 939f516 + a190fa1 commit 529b029
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 529b029

Please sign in to comment.