The assigner and the forge module's merge switch on their first registrations (design 47 G §353), so the seat's client carries the typed follower on go/views: a first view asked of the verb registrations, a newer version heard asked again until the picture holds it, an answer read before the change asked once more. Builds: design 47 G §353, §245 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package meshcatalogclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
)
|
|
|
|
// fakeRegistrations answers the verb registrations from the versions it keeps, and counts each ask.
|
|
type fakeRegistrations struct {
|
|
mu sync.Mutex
|
|
versions map[string]int64
|
|
asks int
|
|
// stale answers the next ask from these versions, once: an answer read before a change was written.
|
|
stale map[string]int64
|
|
}
|
|
|
|
func (f *fakeRegistrations) call(key string, body any) (json.RawMessage, error) {
|
|
if key != RegistrationsVerb {
|
|
return nil, fmt.Errorf("asked %s", key)
|
|
}
|
|
if in := body.(seat.RegistrationsInput); in.Module != "" || in.History {
|
|
return nil, fmt.Errorf("asked %+v", in)
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.asks++
|
|
versions := f.versions
|
|
if f.stale != nil {
|
|
versions, f.stale = f.stale, nil
|
|
}
|
|
a := seat.RegistrationsAnswer{Registrations: []seat.Registration{}}
|
|
for m, v := range versions {
|
|
a.Registrations = append(a.Registrations, seat.Registration{Module: m, Version: v, Origin: "novox/" + m, Path: "module.json", Branches: []string{"main"}})
|
|
}
|
|
return json.Marshal(a)
|
|
}
|
|
|
|
func (f *fakeRegistrations) set(module string, version int64) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.versions[module] = version
|
|
}
|
|
|
|
func (f *fakeRegistrations) asked() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.asks
|
|
}
|
|
|
|
func changedEvent(module string, version int64) []byte {
|
|
b, _ := json.Marshal(seat.RegistrationChangedEvent{V: 1, Module: module, Version: version, Moved: []string{"branches"}, Warrant: "ask-1"})
|
|
return b
|
|
}
|
|
|
|
// CT10 (design 47 G §353): a part following the registrations takes a first view of the verb registrations, then each
|
|
// registration-changed: a newer version is asked and held, one it holds asks nothing, a change heard while an answer
|
|
// read before it was given asks again, and what is no such event is refused.
|
|
func TestRegistrationsAreAFirstViewThenTheirEvents(t *testing.T) {
|
|
changed := make(chan seat.Registration, 16)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
next := func() seat.Registration {
|
|
t.Helper()
|
|
select {
|
|
case reg := <-changed:
|
|
return reg
|
|
case <-ctx.Done():
|
|
t.Fatal("no registration changed")
|
|
return seat.Registration{}
|
|
}
|
|
}
|
|
f := &fakeRegistrations{versions: map[string]int64{"nats": 1, "zsh": 3}}
|
|
r := NewRegistrations(f.call)
|
|
r.Follower.Wait = time.Nanosecond
|
|
r.Follower.Changed = typedChanged(func(reg seat.Registration, gone bool) {
|
|
if !gone {
|
|
changed <- reg
|
|
}
|
|
})
|
|
select {
|
|
case <-r.Ready():
|
|
t.Fatal("ready before its first view")
|
|
default:
|
|
}
|
|
if err := r.Start(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-r.Ready()
|
|
if reg, ok := r.Registration("nats"); !ok || reg.Version != 1 || reg.Origin != "novox/nats" {
|
|
t.Fatalf("first view holds nats as %+v, %v", reg, ok)
|
|
}
|
|
if all := r.All(); len(all) != 2 || all[0].Module != "nats" || all[1].Module != "zsh" {
|
|
t.Fatalf("All = %+v", all)
|
|
}
|
|
for range 2 {
|
|
next()
|
|
}
|
|
|
|
if err := r.Hear(ctx, changedEvent("zsh", 3)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := f.asked(); n != 1 {
|
|
t.Errorf("a version held asked the first view again: %d asks", n)
|
|
}
|
|
|
|
f.set("nats", 2)
|
|
if err := r.Hear(ctx, changedEvent("nats", 2)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reg := next(); reg.Module != "nats" || reg.Version != 2 {
|
|
t.Fatalf("after registration-changed: %+v", reg)
|
|
}
|
|
|
|
// The answer to the ask the event starts was read before the change was written: it is asked once more.
|
|
f.mu.Lock()
|
|
f.stale = map[string]int64{"nats": 2, "zsh": 3}
|
|
f.mu.Unlock()
|
|
f.set("zsh", 4)
|
|
if err := r.Hear(ctx, changedEvent("zsh", 4)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reg := next(); reg.Module != "zsh" || reg.Version != 4 {
|
|
t.Fatalf("after a stale answer: %+v", reg)
|
|
}
|
|
if n := f.asked(); n != 4 {
|
|
t.Errorf("asks = %d, want 4 (first view, nats, a stale answer and the one after)", n)
|
|
}
|
|
|
|
for _, data := range []string{`{}`, `not json`, `{"module":"nats"}`} {
|
|
if err := r.Hear(ctx, []byte(data)); err == nil {
|
|
t.Errorf("Hear(%s) took it", data)
|
|
}
|
|
}
|
|
}
|