diff --git a/internal/selfcompose/selfcompose_test.go b/internal/selfcompose/selfcompose_test.go index ed6c9be8..3a189668 100644 --- a/internal/selfcompose/selfcompose_test.go +++ b/internal/selfcompose/selfcompose_test.go @@ -281,6 +281,14 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) { if err != nil { t.Fatal(err) } + // The composer's loop has ended (its out closed) before the test's cleanup restores composeAgain, laterAgain, + // passSaysEvery and slowAfter, which its passes read (fix-a-bug-met-twice: a goroutine outliving the setting it reads, + // as the shadows below). + defer func() { + cancel() + for range out { + } + }() // oldie is placed and no want names it (a module at a build of the old form, nats on novox): the node runs it inside // its own composition, and composes it there again, even before the agent has applied anything (Running empty). oldie := `{"module":"oldie","version":"1","capabilities":["systemd"],"resources":[{"id":"service","type":"service","unit":"oldie.service","state":"running","boot":"enabled"}]}` @@ -1293,7 +1301,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) { } } readOnly.mu.Unlock() - for group, want := range map[string]func(nodeengine.CandidateAnswer) bool{ + groups := map[string]func(nodeengine.CandidateAnswer) bool{ "same": func(a nodeengine.CandidateAnswer) bool { return a.Verdict == nodeengine.CandidateAnswerVerdictSame && a.Proposal == "same" && a.Revision == 3 && len(a.Same) == 2 }, @@ -1328,11 +1336,19 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) { return a.Verdict == nodeengine.CandidateAnswerVerdictRefused && a.Returns == "" && a.Revision == 7 && strings.Contains(a.Refused, "composes nothing") }, - } { - if !want(answers[group]) { - t.Errorf("%s: %+v", group, answers[group]) - } } + // Answered as each group should be: the shadow answers again while what it reads comes in (what runs, an entry), so an + // answer before the last may still hold lemurs as it runs; the test waits for the answer that settles every group + // (fix-a-bug-met-twice: asserting on whichever answer came first flaked, and the settled one then read as "handed + // again for nothing changed"). + waitFor("every group answered as it should be", func(a map[string]nodeengine.CandidateAnswer) bool { + for group, want := range groups { + if !want(a[group]) { + return false + } + } + return true + }) // The node's own key changing (its answers written) composes nothing again; a group's key deleted drops its answer. sb.put(buckets.Have, "n1.node", rec, 2) select { @@ -1483,8 +1499,11 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) { nanswered := make(chan map[string]nodeengine.CandidateAnswer, 8) nsh := NewShadow(nenv, func(_ context.Context, a map[string]nodeengine.CandidateAnswer) { nanswered <- a }) nctx, ncancel := context.WithCancel(ctx) - defer ncancel() - go func() { _ = nsh.Run(nctx) }() + // The shadow has ended before the test's cleanup restores notReadyAfter, which its run reads (fix-a-bug-met-twice: + // a goroutine outliving the setting it reads, the race of the first shadow above too). + nDone := make(chan struct{}) + defer func() { ncancel(); <-nDone }() + go func() { _ = nsh.Run(nctx); close(nDone) }() nb.made(6) nb.putAssigned(tp, 1) nb.put(buckets.Have, "n1.node", rec, 1)