S7 event-overrun: the hub times each handling, the finding event-overrun, ViewKinds names both

Builds: design 47 H2 H-A-683 (3), H-A-697 (1) (2) (3)
This commit is contained in:
2026-11-25 17:16:27 +01:00
parent fd01ec91a2
commit 065e73e1dd
9 changed files with 364 additions and 9 deletions
@@ -345,6 +345,82 @@ func TestAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) {
t.Run("AReaderHearsAnOwnersChangesAsOrdinaryEvents", subAReaderHearsAnOwnersChangesAsOrdinaryEvents)
t.Run("TheViewsReaderAndTwoHandlersShareTheModulesOneDurable", subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable)
t.Run("WriterSaysItsEventsAndHearsAsksOnTheBus", subWriterSaysItsEventsAndHearsAsksOnTheBus)
t.Run("AHandlingPastEventOverrunRaisesItsFindingAndTheNextStartClearsIt", subAHandlingPastEventOverrunRaisesItsFindingAndTheNextStartClearsIt)
}
// clearedFindings is a findings writer that hands each finding written and each cleared to the test.
type clearedFindings struct{ put, cleared chan string }
func (f *clearedFindings) WriteWire(_ context.Context, w map[string]any) error {
f.put <- fmt.Sprintf("%v %v %v", w["kind"], w["id"], w["summary"])
return nil
}
func (f *clearedFindings) Clear(_ context.Context, _, id, kind, _ string) error {
f.cleared <- kind + " " + id
return nil
}
func (f *clearedFindings) ModuleNode() (string, string) { return "reader", "n1" }
// Design 47 H2 H-A-683 (3), H-A-697: a handling past a short event-overrun raises the module's finding event-overrun, naming its
// key and since when, clears it when the handling ends, and the process started again clears it at its start.
func subAHandlingPastEventOverrunRaisesItsFindingAndTheNextStartClearsIt(t *testing.T) {
t.Setenv("MESH_CONFIG_EVENT_OVERRUN", "1s")
nc, err := nats.Connect(testbus.URL(t))
if err != nil {
t.Fatal(err)
}
defer nc.Close()
js, err := jetstream.New(nc)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: process.EventsStream, Subjects: []string{"mesh.mod.*.event.>"}}); err != nil {
t.Fatal(err)
}
cred := process.Account{Node: "n1", Module: "reader"}
if _, err := js.CreateOrUpdateConsumer(ctx, process.EventsStream, jetstream.ConsumerConfig{Durable: cred.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy}); err != nil {
t.Fatal(err)
}
hub := process.NewEventHub(t.Logf)
release := make(chan struct{})
if err := hub.Handle("mesh.mod.p.event.slow", func(m jetstream.Msg) { <-release; _ = m.Ack() },
func(string, []byte) string { return "the-slow-key" }); err != nil {
t.Fatal(err)
}
found := &clearedFindings{put: make(chan string, 8), cleared: make(chan string, 8)}
r := &views.ProcessReader{Module: "reader", Hub: hub, Logf: t.Logf, Findings: func() views.WireWriter { return found }}
r.Start(ctx, nil, cred, nil)
if err := hub.Start(ctx, js, cred); err != nil {
t.Fatal(err)
}
defer hub.Stop()
for range 2 { // the start clears event-unhandled and event-overrun
<-found.cleared
}
if _, err := js.Publish(ctx, "mesh.mod.p.event.slow", []byte("{}")); err != nil {
t.Fatal(err)
}
select {
case got := <-found.put:
if !strings.HasPrefix(got, "event-overrun reader ") || !strings.Contains(got, "mesh.mod.p.event.slow (key the-slow-key) since ") {
t.Fatalf("the finding: %s", got)
}
case <-ctx.Done():
t.Fatal("a handling past event-overrun raised no finding")
}
close(release)
if got := <-found.cleared; got != "event-overrun reader" {
t.Fatalf("the handling ended and %s was cleared", got)
}
again := &views.ProcessReader{Module: "reader", Hub: process.NewEventHub(t.Logf), Logf: t.Logf, Findings: func() views.WireWriter { return found }}
again.Start(ctx, nil, cred, nil)
cleared := []string{<-found.cleared, <-found.cleared}
slices.Sort(cleared)
if cleared[0] != "event-overrun reader" || cleared[1] != "event-unhandled reader" {
t.Fatalf("the start cleared %v", cleared)
}
}
// seenFindings is a findings writer that hands each finding written to the test.