package process import ( "context" "strings" "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) } // W1.12: a seat's event kept in a stream of its own is heard on one bus name and one durable, the maker's and the // hearer's alike; an event kept last per subject is delivered from the last of each subject. The name is the // contract's Stream as it stands (seatdef.KeptStream), never made again here: a dotted or unprefixed name is // refused by name before the bus is asked. a := Account{Node: "ace", Module: "mesh-grants"} for _, stream := range []string{"node-engine.running", "SEAT_node-engine.running", "SEAT_", "EVENTS"} { if _, err := a.HearSeatEvents(context.Background(), nil, stream, func(jetstream.Msg) {}, nil); err == nil || !strings.Contains(err.Error(), "is not the bus's name of a seat event's stream") { t.Errorf("hearing %q: %v", stream, err) } } cfg := a.SeatEventConsumerConfig([]string{"mesh.seat.node-engine.event.running.>"}, true) if cfg.Durable != "ace_mesh-grants" || cfg.DeliverPolicy != jetstream.DeliverLastPerSubjectPolicy || cfg.AckPolicy != jetstream.AckExplicitPolicy || len(cfg.FilterSubjects) != 1 { t.Errorf("the durable on a stream kept last per subject: %+v", cfg) } if cfg := a.SeatEventConsumerConfig(nil, false); cfg.DeliverPolicy != jetstream.DeliverAllPolicy { t.Errorf("the durable on a stream kept with an age starts from its first message, got %v", cfg.DeliverPolicy) } }