Files
mesh-catalog/modules/mesh-config/cmd/mesh-config/ownerviews_test.go
T

91 lines
3.8 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"strings"
"sync/atomic"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/views"
)
// CAT-C11 (ADR 0615 §1): a followed state of the delivery owners is read from the picture of its owner's tool and events:
// a key asked before the first view is answered is an error (a reader waits), never a read of the owner's bucket; after
// it, the owner's value; an event at the next revision changes it, one past it is a gap and asks the first view again; a
// state that is no owner's falls through to the bus.
func TestAFollowedStateIsReadFromItsOwnersFirstViewAndEvents(t *testing.T) {
created := time.Date(2026, 11, 4, 9, 0, 0, 0, time.UTC)
var asks atomic.Int32
hold := make(chan struct{})
ask := func(key string, body any) (json.RawMessage, error) {
if key != "mesh-delivery-assigner.assigner_read_wants" || body.(map[string]any)["values"] != true {
return nil, errors.New("asked " + key)
}
<-hold
n := asks.Add(1)
a := views.Answer{V: 1, Owner: "mesh-delivery-assigner", State: "wants", Revision: uint64(n) * 10, Created: created,
Values: map[string]json.RawMessage{"want.g14.dunst": json.RawMessage(`{"fp":"a"}`)}}
raw, _ := json.Marshal(a)
return raw, nil
}
var changed atomic.Int32
o := newOwnerViews(ask, func() { changed.Add(1) }, nopFindings{})
if _, followed, err := o.get(WantsRead, "want.g14.dunst"); !followed || err == nil || !strings.Contains(err.Error(), "not answered yet") {
t.Fatalf("before the first view: followed %v, %v", followed, err)
}
if !o.reader.Follows("mesh-delivery-assigner.wants") {
t.Error("the wants view is not one of the reader's (A-28)")
}
// The indexer's modules and manifests and the builder's builds are no states this module follows: which modules exist is
// the catalog half's (design 47 G §196).
for _, gone := range []string{"mesh-delivery-indexer.modules", "mesh-delivery-indexer.manifests", "mesh-delivery-builder.builds"} {
if _, followed, _ := o.get(gone, "x"); followed {
t.Errorf("%s is followed", gone)
}
}
ctx := context.Background()
go func() { _ = o.by[WantsRead].Start(ctx) }()
hold <- struct{}{}
<-o.by[WantsRead].Ready()
e, isFollowed, err := o.get(WantsRead, "want.g14.dunst")
if err != nil || !isFollowed || e == nil || string(e.Value) != `{"fp":"a"}` {
t.Fatalf("after the first view: %+v, %v, %v", e, isFollowed, err)
}
ev := func(rev uint64, key string, deleted bool) views.Event {
return views.Event{V: 1, Owner: "mesh-delivery-assigner", State: "wants", Key: key, Revision: rev, Created: created, Deleted: deleted,
Value: json.RawMessage(`{"fp":"b"}`)}
}
if gap, why := o.by[WantsRead].Apply(ev(11, "want.novox.dunst", false)); gap {
t.Fatalf("the next revision is a gap: %s", why)
}
if keys, _, _ := o.keys(WantsRead); strings.Join(keys, ",") != "want.g14.dunst,want.novox.dunst" {
t.Errorf("keys %v", keys)
}
if gap, _ := o.by[WantsRead].Apply(ev(11, "want.novox.dunst", false)); gap {
t.Error("an event already in the picture is a gap")
}
if gap, why := o.by[WantsRead].Apply(ev(13, "want.x.y", false)); !gap || !strings.Contains(why, "changes missed") {
t.Errorf("a missed revision: gap %v, %s", gap, why)
}
o.by[WantsRead].Apply(ev(12, "want.g14.dunst", true))
if e, _, _ := o.get(WantsRead, "want.g14.dunst"); e != nil {
t.Error("a deleted key is still held")
}
if changed.Load() < 3 {
t.Errorf("the resolution was told of %d changes", changed.Load())
}
if got := o.statuses(); len(got) != len(followed) {
t.Errorf("statuses %d, want %d", len(got), len(followed))
}
}
// nopFindings takes a finding and keeps none.
type nopFindings struct{}
func (nopFindings) WriteWire(context.Context, map[string]any) error { return nil }
func (nopFindings) Clear(_ context.Context, _, _, _, _ string) error { return nil }
func (nopFindings) ModuleNode() (string, string) { return Module, "" }