The seat's bodies hold TakeInTaken (branch, source, entries with repository and pull) and TakeInPending (branch); revision's answer gains none-yet and its revision is optional; meshcatalogclient reads the field, never the error's words. The holders' and readers' uses of the answer follow (a pointer revision). Builds: design 47 G §476, §493, §530, §454, §648 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
165 lines
4.9 KiB
Go
165 lines
4.9 KiB
Go
package meshcatalogclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// fakeRevisions answers the verb revision per branch from the newest revision each branch holds, and counts each ask.
|
|
type fakeRevisions struct {
|
|
mu sync.Mutex
|
|
newest map[string]int64 // by escaped branch
|
|
asks map[string]int
|
|
branches []string
|
|
}
|
|
|
|
func (f *fakeRevisions) call(key string, body any) (json.RawMessage, error) {
|
|
in := body.(seat.RevisionInput)
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
rev, ok := f.newest[in.Branch]
|
|
if ok && rev == 0 {
|
|
return json.Marshal(seat.RevisionAnswer{NoneYet: true})
|
|
}
|
|
if !ok {
|
|
return nil, fmt.Errorf("asked for the branch %q", in.Branch)
|
|
}
|
|
f.asks[in.Branch]++
|
|
return json.Marshal(seat.RevisionAnswer{
|
|
Revision: &seat.CatalogRevision{Revision: rev, Kind: KindTrunk, Branch: in.Branch},
|
|
Entries: []seat.CatalogEntry{{Module: fmt.Sprintf("m-of-%s-%d", in.Branch, rev)}},
|
|
})
|
|
}
|
|
|
|
func (f *fakeRevisions) set(branch string, rev int64) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.newest[keys.EscapeName(branch)] = rev
|
|
}
|
|
|
|
func (f *fakeRevisions) asked(branch string) int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.asks[keys.EscapeName(branch)]
|
|
}
|
|
|
|
func revisionEvent(branch, kind string, rev int64) []byte {
|
|
b, _ := json.Marshal(seat.CatalogRevisionEvent{V: 1, Branch: keys.EscapeName(branch), Kind: kind, Revision: rev})
|
|
return b
|
|
}
|
|
|
|
// CT10 (design 47 G §358, §479, §196): the catalog half is a view per watched branch, named by its caller: two branches
|
|
// give two views, each named `mesh-catalog.catalog-revisions.<branch>`, each following only its own branch's
|
|
// `catalog-revision`; a revision the view missed asks `revision` once more, said in the log.
|
|
func TestCatalogIsAViewPerWatchedBranch(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
f := &fakeRevisions{newest: map[string]int64{"main": 3, keys.EscapeName("release/1"): 7}, asks: map[string]int{}}
|
|
var logMu sync.Mutex
|
|
var logged []string
|
|
logf := func(format string, args ...any) {
|
|
logMu.Lock()
|
|
defer logMu.Unlock()
|
|
logged = append(logged, fmt.Sprintf(format, args...))
|
|
}
|
|
main, err := NewCatalog(f.call, "main")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rel, err := NewCatalog(f.call, "release/1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, c := range []*Catalog{main, rel} {
|
|
c.Follower.Wait = time.Nanosecond
|
|
c.Follower.Logf = logf
|
|
}
|
|
if main.Follower.State == rel.Follower.State || main.Follower.State != "catalog-revisions.main" || rel.Follower.State != views.BranchState(seat.StateCatalogRevisions, "release/1") {
|
|
t.Fatalf("views are named %q and %q", main.Follower.State, rel.Follower.State)
|
|
}
|
|
for _, c := range []*Catalog{main, rel} {
|
|
if err := c.Start(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-c.Ready()
|
|
}
|
|
if h, ok := main.View(); !ok || h.Revision.Revision != 3 || len(h.Entries) != 1 {
|
|
t.Fatalf("main holds %+v, %v", h, ok)
|
|
}
|
|
if h, ok := rel.View(); !ok || h.Revision.Revision != 7 {
|
|
t.Fatalf("release/1 holds %+v, %v", h, ok)
|
|
}
|
|
|
|
// An event of the other branch, a candidate's and one the view holds ask nothing.
|
|
for _, data := range [][]byte{revisionEvent("release/1", KindTrunk, 8), revisionEvent("main", KindCandidate, 4), revisionEvent("main", KindTrunk, 3)} {
|
|
if err := main.Hear(ctx, data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if n := f.asked("main"); n != 1 {
|
|
t.Errorf("main asked %d times, want only its first view", n)
|
|
}
|
|
|
|
// A missed revision asks `revision` once more, and is said.
|
|
f.set("main", 4)
|
|
if err := main.Hear(ctx, revisionEvent("main", KindTrunk, 4)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
if h, _ := main.View(); h.Revision.Revision == 4 {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("main never held revision 4")
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
if n := f.asked("release/1"); n != 1 {
|
|
t.Errorf("release/1 asked %d times after main's revision", n)
|
|
}
|
|
logMu.Lock()
|
|
said := false
|
|
for _, l := range logged {
|
|
if strings.Contains(l, "catalog revision 4 of main said") {
|
|
said = true
|
|
}
|
|
}
|
|
logMu.Unlock()
|
|
if !said {
|
|
t.Errorf("the gap was not said in the log: %q", logged)
|
|
}
|
|
|
|
// A branch with no revision yet is ready and empty, and nothing waits for it.
|
|
f.set("new", 0)
|
|
none, err := NewCatalog(f.call, "new")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := none.Start(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-none.Ready()
|
|
if _, ok := none.View(); ok || none.Follower.Status().WaitsFor != "" {
|
|
t.Errorf("a branch with no revision yet holds a view or waits: %+v", none.Follower.Status())
|
|
}
|
|
|
|
for _, data := range []string{`{}`, `not json`} {
|
|
if err := main.Hear(ctx, []byte(data)); err == nil {
|
|
t.Errorf("Hear(%s) took it", data)
|
|
}
|
|
}
|
|
if _, err := NewCatalog(f.call, ""); err == nil {
|
|
t.Error("a Catalog of no branch was made")
|
|
}
|
|
}
|