A module may watch a role's events, and the catch-up turns out to be unnecessary
Moving the build outcome onto its role broke the one module that consumes it, and my own agreement check passed anyway. The catalogue's subscription derived `mesh.mod.mesh-build-machine.event.built` — a module namespace for a role's event, which no such module owns — so it started, connected, and its graph stayed empty. The check compared names, and the names agreed: the build machine does emit `built`. Only the subjects disagreed, and a subscription that matches nothing is silence. A consumed name is a module's event unless it names a role, and this package cannot tell by looking — so whoever resolved the declaration says which, the way it already does for a seat held or used. A module that watches a role gets the role's event subject and a consumer filtered on it; watching grants subscribe and nothing else, because hearing what a role announced is not taking part in it. The check now compares the two halves that actually have to match — the subject a consumer subscribes against the subject an emitter publishes — with a case pinning that it catches this exact confusion. Comparing names was checking the easy half. **And that answered the open question about catch-up: there is nothing to build.** The mechanism exists because a queue on the old bus receives only what is published after it is bound, so everything built before the catalogue existed was announced to nobody. A stream is a log and a consumer is a position in it: a consumer created afterwards starts at the beginning, so the builds are simply there. Asked of a real server, since the whole decision rested on it — three builds published with nothing listening, then a consumer created, and all three waiting for it.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -122,3 +123,114 @@ func theCataloguesEvents(t *testing.T) ([]AnEmitter, []AConsumer, []DeclaredSeat
|
||||
}
|
||||
return emitters, consumers, seats
|
||||
}
|
||||
|
||||
// **Do the derived subjects meet, not just the names?**
|
||||
//
|
||||
// The check above compares what a consumer asks for against what an emitter says it emits, by name. It
|
||||
// passed while the catalogue's subscription pointed at `mesh.mod.mesh-build-machine.event.built` — a
|
||||
// module namespace for a role's event, which no emitter owns. The names agreed; the subjects did not,
|
||||
// and the graph stayed empty.
|
||||
//
|
||||
// So this compares the thing that actually has to match: the subject a consumer subscribes against the
|
||||
// subject an emitter publishes. It is the last place the two halves can be held together, because
|
||||
// after this the server is the only thing that knows and it says nothing — a subscription that matches
|
||||
// nothing is silence.
|
||||
func TestTheCataloguesDerivedSubjectsMeet(t *testing.T) {
|
||||
emitters, consumers, seats := theCataloguesEvents(t)
|
||||
|
||||
// Every subject something publishes: a module's own events, and the events of every role.
|
||||
published := map[string]bool{}
|
||||
for _, e := range emitters {
|
||||
for _, name := range e.Emits {
|
||||
published["mesh.mod."+e.Module+".event."+name] = true
|
||||
}
|
||||
}
|
||||
for _, s := range seats {
|
||||
for _, name := range s.Emits {
|
||||
published["mesh.seat."+s.Name+".event."+name] = true
|
||||
}
|
||||
}
|
||||
|
||||
byName := map[string]DeclaredSeat{}
|
||||
for _, s := range seats {
|
||||
byName[s.Name] = s
|
||||
}
|
||||
|
||||
var lonely []string
|
||||
for _, c := range consumers {
|
||||
principal := Principal{Kind: KindModule, Node: "one", Module: c.Module, PasswordHash: "x"}
|
||||
for _, want := range c.Consumes {
|
||||
emitter, event, named := strings.Cut(want, ".")
|
||||
if named {
|
||||
if s, isASeat := byName[emitter]; isASeat {
|
||||
principal.Watches = append(principal.Watches,
|
||||
Seat{Name: s.Name, Emits: []string{event}})
|
||||
continue
|
||||
}
|
||||
}
|
||||
principal.Consumes = append(principal.Consumes, want)
|
||||
}
|
||||
perms, err := PermissionsFor(principal)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", c.Module, err)
|
||||
}
|
||||
for _, subject := range perms.Subscribe {
|
||||
if !strings.Contains(subject, ".event.") {
|
||||
continue
|
||||
}
|
||||
if reaches(subject, published) {
|
||||
continue
|
||||
}
|
||||
// A wildcard over emitters reaches whatever arrives later, and an emitter that is not
|
||||
// installed is ordinary — both are already excused by the check above, so only a subject
|
||||
// that can never match anything gets here.
|
||||
if strings.Contains(subject, "*") || strings.Contains(subject, ">") {
|
||||
continue
|
||||
}
|
||||
lonely = append(lonely, c.Module+" subscribes "+subject+", which nothing publishes")
|
||||
}
|
||||
}
|
||||
if len(lonely) > 0 {
|
||||
sort.Strings(lonely)
|
||||
t.Fatalf("%d subscription(s) derive to a subject no emitter owns:\n %s",
|
||||
len(lonely), strings.Join(lonely, "\n "))
|
||||
}
|
||||
}
|
||||
|
||||
// And it catches the thing it exists for: a role's event read as a module's.
|
||||
func TestTheDerivedSubjectCheckCatchesARolesEventReadAsAModules(t *testing.T) {
|
||||
published := map[string]bool{"mesh.seat.mesh-build-machine.event.built": true}
|
||||
// What the derivation produced before a consumed seat name was resolved as one.
|
||||
if reaches("mesh.mod.mesh-build-machine.event.built", published) {
|
||||
t.Fatal("a module namespace was treated as reaching a role's event, which is the bug")
|
||||
}
|
||||
// And the corrected one does reach it.
|
||||
if !reaches("mesh.seat.mesh-build-machine.event.built", published) {
|
||||
t.Fatal("the role's own subject does not reach the role's event")
|
||||
}
|
||||
}
|
||||
|
||||
// reaches says whether a subscribed subject admits any published one.
|
||||
func reaches(subject string, published map[string]bool) bool {
|
||||
for p := range published {
|
||||
if admitsSubject(strings.Split(subject, "."), strings.Split(p, ".")) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func admitsSubject(pattern, subject []string) bool {
|
||||
for i, token := range pattern {
|
||||
if token == ">" {
|
||||
return i < len(subject)
|
||||
}
|
||||
if i >= len(subject) {
|
||||
return false
|
||||
}
|
||||
if token != "*" && token != subject[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(pattern) == len(subject)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user