package link import ( "context" "encoding/json" "io" "log" "testing" "time" "github.com/novox/mesh-controller/internal/broker" ) type ensured struct{ consumers []broker.Consumer } func (e *ensured) EnsureConsumer(c broker.Consumer) error { e.consumers = append(e.consumers, c) return nil } type acceptsAs string func (n acceptsAs) Enrol(context.Context, EnrolRequest) (EnrolReply, error) { return EnrolReply{Accepted: true, Node: string(n)}, nil } type anEnrolment struct{ body []byte } func (m anEnrolment) Kind() string { return "enrol" } func (m anEnrolment) Body() []byte { return m.body } func (m anEnrolment) Redelivered() bool { return false } func (m anEnrolment) HeldFor() time.Duration { return 0 } func (m anEnrolment) Answer(context.Context, []byte) error { return nil } func (m anEnrolment) Took() error { return nil } func (m anEnrolment) Hold(time.Duration) error { return nil } func (m anEnrolment) Drop() error { return nil } // **A node that enrols can hear its declarations at once** (novox/hq 04-ISSUES/146): its consumer is // made as it enrols, not only when the control plane next starts — the first machine of a mesh // enrols after the control plane is up, and heard nothing. func TestAnEnrolledNodeIsGivenHowItHearsItsDeclarations(t *testing.T) { made := &ensured{} s := &Server{enroller: acceptsAs("anchor"), consumers: made, log: log.New(io.Discard, "", 0)} body, _ := json.Marshal(EnrolRequest{Node: "anchor"}) s.enrolling(context.Background(), anEnrolment{body: body}) want := broker.NodeConsumer("anchor") if len(made.consumers) != 1 || made.consumers[0].Name != want.Name || made.consumers[0].Stream != want.Stream { t.Fatalf("the enrolled node was given %v, want its own declaration consumer %v", made.consumers, want) } }