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
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
@@ -36,20 +35,13 @@ var assignmentsView *views.Follower
|
||||
|
||||
// newAssignmentsView makes the follower: ask calls the tool, gone is told of a deleted key, changed of every change after
|
||||
// it is held (a first view's keys too).
|
||||
func newAssignmentsView(ask func(key string, body any) (json.RawMessage, error), gone func(key string), changed func()) *views.Follower {
|
||||
return &views.Follower{Owner: assignerModule, Tool: AssignmentsTool, State: "assignments", Logf: say,
|
||||
Ask: func(_ context.Context, after string) (views.Answer, error) {
|
||||
body := map[string]any{"values": true}
|
||||
if after != "" {
|
||||
body["after"] = after
|
||||
}
|
||||
raw, err := ask(AssignmentsTool, body)
|
||||
if err != nil {
|
||||
return views.Answer{}, err
|
||||
}
|
||||
return views.DecodeAnswer(unwrapToolAnswer(raw))
|
||||
},
|
||||
Changed: func(key string, _ json.RawMessage, deleted bool, _ time.Time) {
|
||||
func newAssignmentsView(ask func(key string, body any) (json.RawMessage, error), gone func(key string), changed func(),
|
||||
findings views.WireWriter) (*views.Follower, *views.Reader) {
|
||||
// A views.Reader holding the vault's writer follows it (design 47 G A-28), so the reader writes and clears its findings
|
||||
// (past its bound the finding names the owner and the tool, and the answer clears it; no secret) and hears the owner's
|
||||
// views event, asking again on a gap.
|
||||
r := &views.Reader{Module: Seat, Findings: findings, Logf: say,
|
||||
Changed: func(_, key string, _ json.RawMessage, deleted bool) {
|
||||
if deleted {
|
||||
gone(key)
|
||||
}
|
||||
@@ -57,6 +49,19 @@ func newAssignmentsView(ask func(key string, body any) (json.RawMessage, error),
|
||||
changed()
|
||||
}
|
||||
}}
|
||||
f := r.Follow(assignerModule, "assignments", AssignmentsTool)
|
||||
f.Ask = func(_ context.Context, after string) (views.Answer, error) {
|
||||
body := map[string]any{"values": true}
|
||||
if after != "" {
|
||||
body["after"] = after
|
||||
}
|
||||
raw, err := ask(AssignmentsTool, body)
|
||||
if err != nil {
|
||||
return views.Answer{}, err
|
||||
}
|
||||
return views.DecodeAnswer(unwrapToolAnswer(raw))
|
||||
}
|
||||
return f, r
|
||||
}
|
||||
|
||||
// unwrapToolAnswer takes a tool's answer out of the protocol's content list when it comes in one.
|
||||
@@ -72,24 +77,6 @@ func unwrapToolAnswer(raw json.RawMessage) []byte {
|
||||
return raw
|
||||
}
|
||||
|
||||
// adoptAssignmentsView holds the follower in a views.Reader with the vault's writer (design 47 G A-28), so the reader writes
|
||||
// and clears its findings (past its bound the finding names the owner and the tool, and the answer clears it; no secret).
|
||||
func adoptAssignmentsView(f *views.Follower, findings views.WireWriter) (*views.Reader, error) {
|
||||
r := &views.Reader{Module: Seat, Findings: findings, Logf: say}
|
||||
hear := func(ctx context.Context, body []byte) error {
|
||||
ev, err := views.DecodeEvent(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if gap, why := f.Apply(ev); gap {
|
||||
say("a gap in the assigner's assignments (%s): its first view is asked once more", why)
|
||||
go func() { _ = f.Start(ctx) }()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return r, r.Adopt(f, assignerModule, views.EventName("assignments"), hear)
|
||||
}
|
||||
|
||||
// viewAssignments is the assignments reader over the picture: a key asked before the first view is answered is an error, so
|
||||
// the caller waits (the "not yet" of the vault's other reads), never a read of the owner's bucket.
|
||||
func viewAssignments(f func() *views.Follower) AssignerReader {
|
||||
|
||||
@@ -29,10 +29,7 @@ func TestAnUnansweredFirstViewOfTheAssignmentsIsAFindingTheAnswerClears(t *testi
|
||||
Values: map[string]json.RawMessage{"g14.node": json.RawMessage(`{}`)}})
|
||||
return raw, nil
|
||||
}
|
||||
f := newAssignmentsView(ask, func(string) {}, nil)
|
||||
if _, err := adoptAssignmentsView(f, heldFindings{&mu, held}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
|
||||
@@ -132,16 +132,11 @@ func watchLeavers(env *Env) bool {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
f := newAssignmentsView(stdio.Ask, func(key string) {
|
||||
f, r := newAssignmentsView(stdio.Ask, func(key string) {
|
||||
if node, ok := leaverKey(key); ok {
|
||||
env.Vault.NodeLeft(node)
|
||||
}
|
||||
}, env.Vault.AssignmentsChanged)
|
||||
r, err := adoptAssignmentsView(f, w)
|
||||
if err != nil {
|
||||
say("the assignments view is not adopted: %v", err)
|
||||
return false
|
||||
}
|
||||
}, env.Vault.AssignmentsChanged, w)
|
||||
assignmentsView = f
|
||||
r.Start(context.Background())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user