views: hear a seat's event on its bare subject as on its further parts
A seat's contract says an event on its bare subject (the seat mesh-catalog's registration-changed), while both view readers adopted it on `<subject>.>` alone, so the event hub acknowledged it unhandled and the part never acted. The process reader now registers one handler on both forms of the hub (the two patterns do not overlap, so a message reaches it once), and the stdio reader subscribes both forms with the one handler. Builds: design 47 H H-A-166
This commit is contained in:
@@ -175,8 +175,7 @@ func (r *ProcessReader) Adopt(f *Follower, seat, event string, hear func(context
|
||||
hub, handle := r.hub, r.handle
|
||||
r.mu.Unlock()
|
||||
if subject != "" && hub != nil {
|
||||
// one subscription per event, with every further part (design 47 G §532)
|
||||
if err := hub.Handle(subject+".>", handle); err != nil {
|
||||
if err := handleEvent(hub, subject, handle); err != nil {
|
||||
r.logf("views: %s's event %s is not heard (%v): the view %s is its first view's alone, asked again by hand", r.Module, subject, err, p.bucket)
|
||||
}
|
||||
}
|
||||
@@ -202,8 +201,7 @@ func (r *ProcessReader) AdoptEvent(seat, event string, hear func(context.Context
|
||||
hub, handle := r.hub, r.handle
|
||||
r.mu.Unlock()
|
||||
if fresh && hub != nil {
|
||||
// one subscription per event, with every further part (design 47 G §532)
|
||||
if err := hub.Handle(subject+".>", handle); err != nil {
|
||||
if err := handleEvent(hub, subject, handle); err != nil {
|
||||
r.logf("views: %s's event %s is not heard (%v)", r.Module, subject, err)
|
||||
}
|
||||
}
|
||||
@@ -861,9 +859,8 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
|
||||
subjects := slices.Sorted(maps.Keys(r.adopted))
|
||||
r.mu.Unlock()
|
||||
for _, subject := range subjects {
|
||||
// one subscription per event, with every further part (design 47 G §532)
|
||||
if err == nil {
|
||||
err = hub.Handle(subject+".>", hear)
|
||||
err = handleEvent(hub, subject, hear)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -891,6 +888,16 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
|
||||
r.Ask(ctx, said)
|
||||
}
|
||||
|
||||
// handleEvent registers handle on the hub for a seat's event on its bare subject and on every further part after it
|
||||
// (`<subject>.>`): one handler, both forms, as the seat's contract says the event on either (design 47 H, H-A-166; one
|
||||
// subscription per event, design 47 G §532). The two patterns do not overlap, so a message reaches the handler once.
|
||||
func handleEvent(hub *process.EventHub, subject string, handle func(jetstream.Msg)) error {
|
||||
if err := hub.Handle(subject, handle); err != nil {
|
||||
return err
|
||||
}
|
||||
return hub.Handle(subject+".>", handle)
|
||||
}
|
||||
|
||||
// adoptedHears are the handlers of every follower adopted on the event whose subject is subject or has further parts
|
||||
// after it (`<event>.<module>.<branch>.<commit or group>`, design 47 G §521, §532), or nil.
|
||||
func adoptedHears(adopted map[string][]adoptedHear, subject string) []adoptedHear {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -168,6 +169,20 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
||||
if _, err := r.Follow("o", "modules", "o.o_read_modules"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// H-A-166: a seat's event said on its bare subject (as the seat mesh-catalog says registration-changed) and one with
|
||||
// further parts both reach the one adopted handler, each once.
|
||||
if info, err := js.Stream(ctx, process.EventsStream); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if cfg := info.CachedInfo().Config; !slices.Contains(cfg.Subjects, "mesh.seat.*.event.>") {
|
||||
cfg.Subjects = append(cfg.Subjects, "mesh.seat.*.event.>")
|
||||
if _, err := js.UpdateStream(ctx, cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
seatHeard := make(chan string, 8)
|
||||
if err := r.AdoptEvent("s", "registration-changed", func(_ context.Context, d []byte) error { seatHeard <- string(d); return nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.Start(ctx, js, cred, nil)
|
||||
if err := hub.Start(ctx, js, cred); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -181,8 +196,9 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range []string{"mesh.mod.p.event.a", "mesh.mod.q.event.b.c", "mesh.mod.z.event.nobody", "mesh.mod.p.event.a"} {
|
||||
if _, err := js.Publish(ctx, s, []byte("{}")); err != nil {
|
||||
for _, s := range []string{"mesh.mod.p.event.a", "mesh.mod.q.event.b.c", "mesh.mod.z.event.nobody", "mesh.mod.p.event.a",
|
||||
"mesh.seat.s.event.registration-changed", "mesh.seat.s.event.registration-changed.m"} {
|
||||
if _, err := js.Publish(ctx, s, []byte(`"`+s+`"`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -199,8 +215,18 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
||||
t.Fatalf("handler %s got %s, wanted %s", want.who, s, want.subject)
|
||||
}
|
||||
}
|
||||
if len(got["a"])+len(got["b"]) != 0 {
|
||||
t.Fatal("a handler heard a message that was another's")
|
||||
for _, want := range []string{`"mesh.seat.s.event.registration-changed"`, `"mesh.seat.s.event.registration-changed.m"`} {
|
||||
select {
|
||||
case d := <-seatHeard:
|
||||
if d != want {
|
||||
t.Fatalf("the adopted seat event heard %s, wanted %s", d, want)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatalf("the adopted seat event never heard %s", want)
|
||||
}
|
||||
}
|
||||
if len(got["a"])+len(got["b"])+len(seatHeard) != 0 {
|
||||
t.Fatal("a handler heard a message that was another's, or one twice")
|
||||
}
|
||||
cons, err := js.Consumer(ctx, process.EventsStream, cred.EventConsumerName())
|
||||
if err != nil {
|
||||
|
||||
+20
-10
@@ -74,6 +74,10 @@ type adoption struct {
|
||||
// further part, `<seat>.<event>.>` (design 47 G §532, §521).
|
||||
func (a adoption) local() string { return a.seat + "." + a.event + ".>" }
|
||||
|
||||
// bare is the adopted follower's event on its bare subject, `<seat>.<event>`, heard by the same handler as local: the seat's
|
||||
// contract says the event on either form (design 47 H, H-A-166).
|
||||
func (a adoption) bare() string { return a.seat + "." + a.event }
|
||||
|
||||
func (r *Reader) logf(format string, args ...any) {
|
||||
if r.Logf != nil {
|
||||
r.Logf(format, args...)
|
||||
@@ -316,17 +320,23 @@ func (r *Reader) begin(name string) {
|
||||
r.asking[pattern] = true
|
||||
ctx, f := r.ctx, r.followers[name]
|
||||
handle := r.handler(ctx, pattern)
|
||||
forms := []string{pattern}
|
||||
if a, ok := r.adopted[name]; ok {
|
||||
forms = []string{a.bare(), pattern} // one handler, both forms (H-A-166)
|
||||
}
|
||||
go func() {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := subscribe.get()(pattern, handle)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
r.logf("not hearing %s yet for %s's %s (%v); asking again in %s", pattern, f.Owner, f.State, err, wait)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(wait):
|
||||
for _, form := range forms {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := subscribe.get()(form, handle)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
r.logf("not hearing %s yet for %s's %s (%v); asking again in %s", form, f.Owner, f.State, err, wait)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(wait):
|
||||
}
|
||||
}
|
||||
}
|
||||
r.mu.Lock()
|
||||
|
||||
+11
-6
@@ -888,13 +888,17 @@ func subViews(t *testing.T) {
|
||||
}
|
||||
ready(next)
|
||||
hmu.Lock()
|
||||
h, n := handlers["s.revision-said.>"], len(handlers)
|
||||
h, bare, n := handlers["s.revision-said.>"], handlers["s.revision-said"], len(handlers)
|
||||
hmu.Unlock()
|
||||
if h == nil || n != 1 {
|
||||
t.Fatalf("subscriptions %v: want the event's one", handlers)
|
||||
if h == nil || bare == nil || n != 2 {
|
||||
t.Fatalf("subscriptions %v: want the event's bare subject and its further parts (H-A-166)", handlers)
|
||||
}
|
||||
// H-A-166: the event on its bare subject, as a seat's contract says it, reaches the same handler
|
||||
if _ = bare(stdio.Envelope{Key: "s.revision-said", Body: []byte(`{"revision":7}`)}); len(*heard["main"]) != 1 || len(*heard["next"]) != 1 {
|
||||
t.Errorf("the bare event reached %v and %v", *heard["main"], *heard["next"])
|
||||
}
|
||||
event := stdio.Envelope{Key: "s.revision-said.next", Body: []byte(`{"revision":8}`)}
|
||||
if _ = h(event); len(*heard["main"]) != 1 || len(*heard["next"]) != 1 {
|
||||
if _ = h(event); len(*heard["main"]) != 2 || len(*heard["next"]) != 2 {
|
||||
t.Errorf("the event reached %v and %v", *heard["main"], *heard["next"])
|
||||
}
|
||||
next.WaitFor(ctx, "9")
|
||||
@@ -911,7 +915,7 @@ func subViews(t *testing.T) {
|
||||
if st := r.Statuses(); len(st) != 1 || st[0].State != main.State || r.Follows("s."+next.State) {
|
||||
t.Errorf("first views after the drop: %+v", st)
|
||||
}
|
||||
if _ = h(event); len(*heard["next"]) != 1 || len(*heard["main"]) != 2 {
|
||||
if _ = h(event); len(*heard["next"]) != 2 || len(*heard["main"]) != 3 {
|
||||
t.Errorf("after the drop the event reached %v and %v", *heard["main"], *heard["next"])
|
||||
}
|
||||
if err := r.Drop("s." + next.State); err == nil {
|
||||
@@ -932,7 +936,8 @@ func subViews(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ready(pn)
|
||||
if !slices.Contains(pr.Hub.Patterns(), "mesh.seat.s.event.revision-other.>") {
|
||||
if ps := pr.Hub.Patterns(); !slices.Contains(ps, "mesh.seat.s.event.revision-other.>") || !slices.Contains(ps, "mesh.seat.s.event.revision-other") ||
|
||||
!slices.Contains(ps, "mesh.seat.s.event.revision-said") {
|
||||
t.Errorf("the hub handles %v", pr.Hub.Patterns())
|
||||
}
|
||||
msg := func() *seatMsg {
|
||||
|
||||
Reference in New Issue
Block a user