Indexer: the branch consumer fake answers its CachedInfo and ends when stopped

With the SDK's guard of one pull per durable (pullOnce reads the consumer's
CachedInfo and releases on the consume's Closed), the fake of a branch's
durable embedded a nil consumer and a nil consume context, so
TestABranchDurableNotThereIsMadeAndHeard panicked. The fake now answers a
ConsumerInfo with its stream, name and a created time of its own, and its
consume context closes when stopped. No other fake in mesh-delivery embeds
jetstream.Consumer; the assigner's, runner's, builder's, watchman's and
grants' unit suites pass with the new SDK unchanged.

Builds: design 47 H-A-196, H §774
This commit is contained in:
2026-11-21 01:18:22 +01:00
parent d6c209a10c
commit d4d7895392
@@ -7,6 +7,8 @@ import (
"reflect" "reflect"
"slices" "slices"
"strings" "strings"
"sync"
"sync/atomic"
"testing" "testing"
"time" "time"
@@ -172,25 +174,43 @@ func (f *fakeBranchJS) Consumer(_ context.Context, stream, name string) (jetstre
func (f *fakeBranchJS) CreateConsumer(_ context.Context, stream string, cfg jetstream.ConsumerConfig) (jetstream.Consumer, error) { func (f *fakeBranchJS) CreateConsumer(_ context.Context, stream string, cfg jetstream.ConsumerConfig) (jetstream.Consumer, error) {
f.made = append(f.made, cfg) f.made = append(f.made, cfg)
c := &fakeBranchConsumer{} c := newFakeBranchConsumer(stream, cfg.Durable)
f.held[stream+"/"+cfg.Durable] = c f.held[stream+"/"+cfg.Durable] = c
return c, nil return c, nil
} }
// fakeBranchConsumer hands one source-moved to the hearing as soon as it is consumed. // fakeBranchConsumer hands one source-moved to the hearing as soon as it is consumed. It answers its CachedInfo as the bus
// does, with its stream, name and created time, which the SDK's guard of one pull per durable keys by (design 47 H-A-196).
type fakeBranchConsumer struct { type fakeBranchConsumer struct {
jetstream.Consumer jetstream.Consumer
info jetstream.ConsumerInfo
heard int heard int
} }
type fakeConsumeContext struct{ jetstream.ConsumeContext } // fakeConsumersMade numbers the fakes, so each is created at a time of its own, as each durable the bus makes is.
var fakeConsumersMade atomic.Int64
func (fakeConsumeContext) Stop() {} func newFakeBranchConsumer(stream, durable string) *fakeBranchConsumer {
return &fakeBranchConsumer{info: jetstream.ConsumerInfo{Stream: stream, Name: durable, Config: jetstream.ConsumerConfig{Durable: durable},
Created: time.Unix(0, fakeConsumersMade.Add(1)).UTC()}}
}
func (c *fakeBranchConsumer) CachedInfo() *jetstream.ConsumerInfo { return &c.info }
// fakeConsumeContext is a consume that ends when stopped, as the guard's release waits for (H-A-196).
type fakeConsumeContext struct {
jetstream.ConsumeContext
stop sync.Once
closed chan struct{}
}
func (c *fakeConsumeContext) Stop() { c.stop.Do(func() { close(c.closed) }) }
func (c *fakeConsumeContext) Closed() <-chan struct{} { return c.closed }
func (c *fakeBranchConsumer) Consume(handle jetstream.MessageHandler, _ ...jetstream.PullConsumeOpt) (jetstream.ConsumeContext, error) { func (c *fakeBranchConsumer) Consume(handle jetstream.MessageHandler, _ ...jetstream.PullConsumeOpt) (jetstream.ConsumeContext, error) {
c.heard++ c.heard++
handle(msgOf{f: &fakeMsg{subject: "mesh.seat.mesh-delivery.event.source-moved.main.2"}}) handle(msgOf{f: &fakeMsg{subject: "mesh.seat.mesh-delivery.event.source-moved.main.2"}})
return fakeConsumeContext{}, nil return &fakeConsumeContext{closed: make(chan struct{})}, nil
} }
// Design 47 H §743, H-A-134: a branch's durable the bus does not hold the indexer makes, as a module's seat-event consumer is // Design 47 H §743, H-A-134: a branch's durable the bus does not hold the indexer makes, as a module's seat-event consumer is
@@ -207,7 +227,7 @@ func TestABranchDurableNotThereIsMadeAndHeard(t *testing.T) {
name := "n1_mesh-delivery-indexer_" + keys.EscapeName("main") name := "n1_mesh-delivery-indexer_" + keys.EscapeName("main")
var existing *fakeBranchConsumer var existing *fakeBranchConsumer
if tc.held { if tc.held {
existing = &fakeBranchConsumer{} existing = newFakeBranchConsumer(meshdelivery.StreamSourceMoved, name)
js.held[meshdelivery.StreamSourceMoved+"/"+name] = existing js.held[meshdelivery.StreamSourceMoved+"/"+name] = existing
} }
var heard []string var heard []string