package process import ( "testing" "github.com/nats-io/nats.go/jetstream" ) func TestTheHubMatchesSubjectsAndRefusesOverlappingPatterns(t *testing.T) { for _, c := range []struct { a, b string match, overlap bool }{ {"mesh.mod.*.event.views.*", "mesh.mod.o.event.views.x", true, true}, {"mesh.mod.p.event.>", "mesh.mod.p.event", false, false}, {"mesh.mod.p.event.a", "mesh.mod.q.event.a", false, false}, {"mesh.mod.p.event.>", "mesh.mod.*.event.a", false, true}, {"mesh.mod.p.event.a", "mesh.mod.p.event.a.b", false, false}, } { if got := subjectMatches(c.a, c.b); got != c.match && c.match { t.Errorf("%s should match %s", c.a, c.b) } if got := subjectsOverlap(c.a, c.b); got != c.overlap { t.Errorf("overlap(%s, %s) = %v", c.a, c.b, got) } } h := NewEventHub(nil) nop := func(jetstream.Msg) {} if err := h.Handle("mesh.mod.p.event.>", nop); err != nil { t.Fatal(err) } if err := h.Handle("mesh.mod.*.event.a", nop); err == nil { t.Fatal("an overlapping pattern was accepted") } if err := h.Handle("mesh.mod.q.event.>", nop); err != nil { t.Fatal(err) } }