Files
mesh-catalog/modules/mesh-vault/cmd/mesh-vault/assignmentsview_test.go
T
jschoubben d03ffa8038 G findings: mesh-config's and mesh-vault's owner views are followed by the reader (Follow), not adopted: they hear the owner's own views event
Adopt subscribes a seat's event with further parts (<seat>.<event>.>), which never matches the plain views event of an owner; the reader's Follow hears it and asks again on a gap. Also the distribution whole test counts its own two events by subject, as the writer's are said beside them.

Builds: design 47 G A-28, A-26, A-30
2026-11-11 22:22:24 +01:00

79 lines
2.5 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/views"
)
// ADR 0615 §6: the vault started while the assigner is down keeps asking, never reads its bucket, and past the bound
// writes a finding naming the owner and the tool for the condition keeper; the answer deletes it. Before the answer a read
// of the assignments is an error.
func TestAnUnansweredFirstViewOfTheAssignmentsIsAFindingTheAnswerClears(t *testing.T) {
var mu sync.Mutex
up := false
held := map[string]json.RawMessage{}
ask := func(string, any) (json.RawMessage, error) {
mu.Lock()
defer mu.Unlock()
if !up {
return nil, errors.New("nats: no responders available for request")
}
raw, _ := json.Marshal(views.Answer{V: 1, Owner: "mesh-delivery-assigner", State: "assignments", Revision: 1, Created: time.Now(),
Values: map[string]json.RawMessage{"g14.node": json.RawMessage(`{}`)}})
return raw, nil
}
f, _ := newAssignmentsView(ask, func(string) {}, nil, heldFindings{&mu, held})
f.Bound, f.Wait = 30*time.Millisecond, 10*time.Millisecond
reader := viewAssignments(func() *views.Follower { return f })
if _, err := reader.Keys(); err == nil {
t.Fatal("the assignments was read before its first view")
}
done := make(chan error, 1)
go func() { done <- f.Start(context.Background()) }()
count := func() int { mu.Lock(); defer mu.Unlock(); return len(held) }
for end := time.Now().Add(5 * time.Second); count() == 0 && time.Now().Before(end); time.Sleep(10 * time.Millisecond) {
}
if count() != 1 {
t.Fatalf("past its bound the vault wrote %d findings, want one", count())
}
mu.Lock()
up = true
mu.Unlock()
if err := <-done; err != nil {
t.Fatal(err)
}
if count() != 0 {
t.Errorf("the answer left %d findings", count())
}
if keys, err := reader.Keys(); err != nil || len(keys) != 1 || keys[0] != "g14.node" {
t.Errorf("keys %v, %v", keys, err)
}
}
// heldFindings keeps the findings written, by the finding's kind and subject.
type heldFindings struct {
mu *sync.Mutex
held map[string]json.RawMessage
}
func (h heldFindings) WriteWire(_ context.Context, f map[string]any) error {
h.mu.Lock()
defer h.mu.Unlock()
h.held[fmt.Sprint(f["id"], ".", f["kind"])] = nil
return nil
}
func (h heldFindings) Clear(_ context.Context, _, id, kind, _ string) error {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.held, id+"."+kind)
return nil
}
func (h heldFindings) ModuleNode() (string, string) { return Seat, "" }