diff --git a/go/views/process_reader.go b/go/views/process_reader.go index fecfee82..df862b12 100644 --- a/go/views/process_reader.go +++ b/go/views/process_reader.go @@ -77,6 +77,8 @@ type ProcessReader struct { unheard int lastUnheard string startedAt time.Time + // hooked is the hub the count of unhandled events is wired on, so a second Start counts no event twice. + hooked *process.EventHub } // adoptedHear is one adopted follower's handler of its seat event, by its picture's bucket. @@ -898,10 +900,18 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred if r.Findings != nil { // H-A-169: the events no handler of the module takes are its finding, cleared at each start r.clearUnhandled() - hub.OnUnhandled(r.unhandled) + r.mu.Lock() + wire := r.hooked != hub + r.hooked = hub + r.mu.Unlock() + if wire { + hub.OnUnhandled(r.unhandled) + } } r.mu.Lock() - r.startedAt = r.now() + if r.startedAt.IsZero() { + r.startedAt = r.now() // the process's start; a second Start counts on from it + } r.status = status if !own { // an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one diff --git a/go/views/process_reader_integration_test.go b/go/views/process_reader_integration_test.go index 0dc5789b..8d2d7b1f 100644 --- a/go/views/process_reader_integration_test.go +++ b/go/views/process_reader_integration_test.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "slices" + "strings" "testing" "time" @@ -157,7 +158,8 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) { t.Fatal(err) } } - r := &views.ProcessReader{Module: "reader", Hub: hub, Logf: t.Logf, + found := &seenFindings{put: make(chan map[string]any, 8)} + r := &views.ProcessReader{Module: "reader", Hub: hub, Logf: t.Logf, Findings: func() views.WireWriter { return found }, Call: func(tool string, body any) (json.RawMessage, error) { raw, _ := json.Marshal(body) m, err := nc.Request("mesh.mod.o.call.o_read_modules.n0", raw, 5*time.Second) @@ -235,6 +237,38 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) { if info, err := cons.Info(ctx); err != nil || info.NumAckPending != 0 { t.Fatalf("a message was left unacknowledged: %+v %v", info, err) } + // H-A-169: the one event no handler takes is the module's finding event-unhandled, counted once, naming its subject, + // though the reader started twice. + r.Start(ctx, js, cred, nil) + select { + case f := <-found.put: + summary, _ := f["summary"].(string) + if f["kind"] != views.EventUnhandledKind || f["id"] != "reader" || !strings.Contains(summary, "took 1 events") || + !strings.Contains(summary, "mesh.mod.z.event.nobody") { + t.Fatalf("the unhandled event's finding: %v", f) + } + case <-time.After(10 * time.Second): + t.Fatal("the unhandled event wrote no finding") + } + if _, err := js.Publish(ctx, "mesh.mod.z.event.nobody", []byte("{}")); err != nil { + t.Fatal(err) + } + if _, err := js.Publish(ctx, "mesh.mod.p.event.a", []byte("{}")); err != nil { + t.Fatal(err) + } + select { + case <-got["a"]: // the hub hands messages in order: every hook of the event before has run + case <-time.After(10 * time.Second): + t.Fatal("the handled event after the unhandled one was not heard") + } + select { + case f := <-found.put: + if summary, _ := f["summary"].(string); !strings.Contains(summary, "took 2 events") || len(found.put) != 0 { + t.Fatalf("after a second start one event is counted once: %v, and %d more", f, len(found.put)) + } + default: + t.Fatal("the unhandled event after the second start wrote no finding") + } // A hub started before the bus holds its durable says so, and binds at a later start: a module is not lost for the // order in which the bus makes its consumer. late := process.Account{Node: "n1", Module: "late"} @@ -270,3 +304,10 @@ func TestAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) { t.Run("TheViewsReaderAndTwoHandlersShareTheModulesOneDurable", subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable) t.Run("WriterSaysItsEventsAndHearsAsksOnTheBus", subWriterSaysItsEventsAndHearsAsksOnTheBus) } + +// seenFindings is a findings writer that hands each finding written to the test. +type seenFindings struct{ put chan map[string]any } + +func (f *seenFindings) WriteWire(_ context.Context, w map[string]any) error { f.put <- w; return nil } +func (f *seenFindings) Clear(context.Context, string, string, string, string) error { return nil } +func (f *seenFindings) ModuleNode() (string, string) { return "reader", "n1" }