views: wire the unhandled-event count once, and prove it on the bus
The hub's hook, the path the live fault took, had no test that failed without it, and each Start appended it again, so a second Start counted every event twice. Start now wires it once per hub and keeps the time of the first start; the shared-durable integration test holds the reader's finding of the one event nobody takes, counted once across two starts. Builds: design 47 H H-A-169
This commit is contained in:
@@ -77,6 +77,8 @@ type ProcessReader struct {
|
|||||||
unheard int
|
unheard int
|
||||||
lastUnheard string
|
lastUnheard string
|
||||||
startedAt time.Time
|
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.
|
// 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 {
|
if r.Findings != nil {
|
||||||
// H-A-169: the events no handler of the module takes are its finding, cleared at each start
|
// H-A-169: the events no handler of the module takes are its finding, cleared at each start
|
||||||
r.clearUnhandled()
|
r.clearUnhandled()
|
||||||
|
r.mu.Lock()
|
||||||
|
wire := r.hooked != hub
|
||||||
|
r.hooked = hub
|
||||||
|
r.mu.Unlock()
|
||||||
|
if wire {
|
||||||
hub.OnUnhandled(r.unhandled)
|
hub.OnUnhandled(r.unhandled)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
r.mu.Lock()
|
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
|
r.status = status
|
||||||
if !own {
|
if !own {
|
||||||
// an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one
|
// an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -157,7 +158,8 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
|
|||||||
t.Fatal(err)
|
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) {
|
Call: func(tool string, body any) (json.RawMessage, error) {
|
||||||
raw, _ := json.Marshal(body)
|
raw, _ := json.Marshal(body)
|
||||||
m, err := nc.Request("mesh.mod.o.call.o_read_modules.n0", raw, 5*time.Second)
|
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 {
|
if info, err := cons.Info(ctx); err != nil || info.NumAckPending != 0 {
|
||||||
t.Fatalf("a message was left unacknowledged: %+v %v", info, err)
|
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
|
// 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.
|
// order in which the bus makes its consumer.
|
||||||
late := process.Account{Node: "n1", Module: "late"}
|
late := process.Account{Node: "n1", Module: "late"}
|
||||||
@@ -270,3 +304,10 @@ func TestAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) {
|
|||||||
t.Run("TheViewsReaderAndTwoHandlersShareTheModulesOneDurable", subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable)
|
t.Run("TheViewsReaderAndTwoHandlersShareTheModulesOneDurable", subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable)
|
||||||
t.Run("WriterSaysItsEventsAndHearsAsksOnTheBus", subWriterSaysItsEventsAndHearsAsksOnTheBus)
|
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" }
|
||||||
|
|||||||
Reference in New Issue
Block a user