diff --git a/internal/selfcompose/selfcompose_test.go b/internal/selfcompose/selfcompose_test.go index 6c7892e4..8fc5ddeb 100644 --- a/internal/selfcompose/selfcompose_test.go +++ b/internal/selfcompose/selfcompose_test.go @@ -1292,7 +1292,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 }, @@ -1327,11 +1327,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 { @@ -1482,8 +1490,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)