diff --git a/node-tools/internal/bus/consume_test.go b/node-tools/internal/bus/consume_test.go index e5cc2e5..944a106 100644 --- a/node-tools/internal/bus/consume_test.go +++ b/node-tools/internal/bus/consume_test.go @@ -77,6 +77,8 @@ func TestAConsumerSaysItIsBoundWhileIdleAndOnceWhenTheBusEndedIt(t *testing.T) { } })) mesh.Consumer(t, "anchor", "quiet", []string{"mesh.mod.alpha.event.>"}, time.Second) + // The unasked consume below pulls a durable of its own: a second loop on quiet's is refused (H-A-196, H-A-197). + mesh.Consumer(t, "anchor", "unasked", []string{"mesh.mod.alpha.event.>"}, time.Second) c, err := bus.Connect(bus.Credential{URL: mt.URL(t), Module: "node-tools", Node: "anchor"}) if err != nil { t.Fatal(err) @@ -97,7 +99,7 @@ func TestAConsumerSaysItIsBoundWhileIdleAndOnceWhenTheBusEndedIt(t *testing.T) { t.Cleanup(stop) // A consume nobody asked a heartbeat of says nothing and asks the bus nothing, however many intervals pass. var unaskedBeats atomic.Int32 - unasked, err := c.ConsumeAsBeatingWhen("quiet", func(bus.Envelope) error { return nil }, + unasked, err := c.ConsumeAsBeatingWhen("unasked", func(bus.Envelope) error { return nil }, func(bus.Beat) { unaskedBeats.Add(1) }, func() bool { return false }) if err != nil { t.Fatal(err) diff --git a/node-tools/internal/bus/streams.go b/node-tools/internal/bus/streams.go index 4f7b09e..b9b70aa 100644 --- a/node-tools/internal/bus/streams.go +++ b/node-tools/internal/bus/streams.go @@ -10,6 +10,8 @@ import ( "time" "github.com/nats-io/nats.go/jetstream" + + "git.novox.be/novox/mesh-sdk/go/process" ) // A module's own stream, as the runtime publishes to it and hears it (novox/hq ADR 0363 §8, issue 725). @@ -129,7 +131,10 @@ func (h *hearing) bind(stream, durable string, onErr func(error)) (jetstream.Con if onErr != nil { opts = append(opts, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { onErr(err) })) } - reading, err := consumer.Consume(h.handle, opts...) + // Claimed through the SDK's one guard as it is pulled (design 47 H, H-A-196, H-A-197): a second pull loop on a durable + // this process pulls already would take every other event from the first, so it is refused here, loud. The claim is + // released as the loop is stopped, or once the bus ended it, before its Closed is closed. + reading, err := process.ConsumeOnce(consumer, "the tool runner's hearing of "+h.module, h.handle, opts...) if err != nil { return nil, time.Time{}, "reading", err } diff --git a/node-tools/internal/bus/streams_test.go b/node-tools/internal/bus/streams_test.go index 94a6d7c..6b6901b 100644 --- a/node-tools/internal/bus/streams_test.go +++ b/node-tools/internal/bus/streams_test.go @@ -258,6 +258,34 @@ func TestAConsumerNamedBeforeItExistsIsBoundOnceItDoes(t *testing.T) { } return nil }) + + // A second pull loop on the durable the runner pulls is refused, naming it and both binders (design 47 H, H-A-196, + // H-A-197); a re-serve that stops its loop first binds again without a refusal, its module stream's durable too. + if _, err := conn.ConsumeAs("reader", func(bus.Envelope) error { return nil }); err == nil || + !strings.Contains(err.Error(), "pulled in this process already") || !strings.Contains(err.Error(), "anchor_reader") || + strings.Count(err.Error(), "the tool runner's hearing of reader") != 2 { + t.Fatalf("a second pull loop on the durable the runner pulls was not refused naming it and both binders: %v", err) + } + stop() + again, err := conn.ConsumeAs("reader", func(env bus.Envelope) error { + mu.Lock() + heard[env.Key]++ + mu.Unlock() + return nil + }) + if err != nil { + t.Fatalf("the re-serve was refused after its old loop stopped: %v", err) + } + t.Cleanup(again) + mesh.Emit(t, "mesh.stream.mail.event.mail.home.message.added", map[string]any{}) + mt.Until(t, func() error { + mu.Lock() + defer mu.Unlock() + if heard["mail.mail.home.message.added"] != 2 { + return fmt.Errorf("after the re-serve heard %v", heard) + } + return nil + }) } // novox/hq ADR 0363 §6 (issue 725): a consumer the controller deletes and makes again under the same name, its