package process import ( "context" "strings" "testing" "time" "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) } // H-A-169: a message no handler takes is acknowledged and told to each OnUnhandled hook, with its subject; one taken is not. var told []string h.OnUnhandled(func(subject string) { told = append(told, subject) }) taken, lost := &bindMsg{subject: "mesh.mod.q.event.x"}, &bindMsg{subject: "mesh.seat.s.event.nobody"} h.hear(taken) h.hear(lost) if !lost.acked || len(told) != 1 || told[0] != "mesh.seat.s.event.nobody" { t.Errorf("the unhandled message: acked %v, told %v", lost.acked, told) } // 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) } } type pullConsumer struct { jetstream.Consumer info *jetstream.ConsumerInfo handle jetstream.MessageHandler options []jetstream.PullConsumeOpt } func (c *pullConsumer) CachedInfo() *jetstream.ConsumerInfo { return c.info } func (c *pullConsumer) Consume(handle jetstream.MessageHandler, opts ...jetstream.PullConsumeOpt) (jetstream.ConsumeContext, error) { c.handle, c.options = handle, opts return &pullContext{closed: make(chan struct{})}, nil } type pullContext struct { jetstream.ConsumeContext closed chan struct{} } func (p *pullContext) Stop() { close(p.closed) } func (p *pullContext) Closed() <-chan struct{} { return p.closed } type slowMsg struct { jetstream.Msg said chan struct{} } func (m *slowMsg) InProgress() error { m.said <- struct{}{}; return nil } // H-A-281: the hub pulls at most twenty messages per pull, and says in progress for a message whose handler runs past a // third of the consumer's ack wait, at each third until it returns; a handler that returns first says nothing. func TestTheHubPullsSmallBatchesAndKeepsSlowHandlersAlive(t *testing.T) { cons := &pullConsumer{info: &jetstream.ConsumerInfo{Config: jetstream.ConsumerConfig{AckWait: 30 * time.Second}}} ticks := make(chan time.Time) var every time.Duration stopped := 0 tick := func(d time.Duration) (<-chan time.Time, func()) { every = d; return ticks, func() { stopped++ } } var handled func(jetstream.Msg) cc, err := hearEvents(cons, "the test", func(m jetstream.Msg) { handled(m) }, t.Logf, tick) if err != nil { t.Fatal(err) } defer cc.Stop() pulls := 0 for _, o := range cons.options { if n, ok := o.(jetstream.PullMaxMessages); ok { pulls = int(n) } } if pulls != 20 { t.Errorf("the hub pulls %d messages per pull, want 20 (0: the bus client's default of 500)", pulls) } slow := &slowMsg{said: make(chan struct{}, 4)} handled = func(jetstream.Msg) { for range 2 { // two thirds of the ack wait pass while the handler runs select { case ticks <- time.Time{}: case <-time.After(5 * time.Second): t.Error("no ticker runs while the handler runs") return } select { case <-slow.said: case <-time.After(5 * time.Second): t.Error("a handler running past a third of the ack wait said no in progress") return } } } cons.handle(slow) if every != 10*time.Second || stopped != 1 { t.Errorf("in progress every %v (want 10s, a third of the ack wait), ticker stopped %d times (want 1)", every, stopped) } fast := &slowMsg{said: make(chan struct{}, 1)} handled = func(jetstream.Msg) {} cons.handle(fast) if len(fast.said) != 0 { t.Error("a handler that returned before a third of the ack wait said in progress") } }