diff --git a/modules/distribution/cmd/distribution-tools/bounded.go b/modules/distribution/cmd/distribution-tools/bounded.go index 14236ff47..7bf77f0dd 100644 --- a/modules/distribution/cmd/distribution-tools/bounded.go +++ b/modules/distribution/cmd/distribution-tools/bounded.go @@ -13,8 +13,9 @@ import ( // naming its step and the publish answers with it. const stepBound = 10 * time.Second -// step runs f, logs its start and its end with its time, and gives up on it after bound: the error names the step. An -// operation given up on keeps running in its goroutine until the bus answers it. +// step runs f, logs its start and its end with its time, and gives up on it after bound: the error names the step. A step +// given up on is not cancelled (the SDK's bus calls take no context): its call ends at the SDK's own bound of the ask, and +// the log says, by the step's name, that it was given up and later when the call behind it ended and how (A-443, A-443a). func step[T any](bound time.Duration, what string, f func() (T, error)) (T, error) { type res struct { v T @@ -31,7 +32,11 @@ func step[T any](bound time.Duration, what string, f func() (T, error)) (T, erro case <-time.After(bound): var zero T err := fmt.Errorf("%s did not answer within %s", what, bound) - logf("step %s: %v", what, err) + logf("step %s: %v; given up, its call is left to end at the SDK's own bound", what, err) + go func() { + r := <-ch + logf("step %s, given up after %s: its call ended after %s (%v)", what, bound, time.Since(start).Round(time.Millisecond), r.err) + }() return zero, err } } diff --git a/modules/distribution/cmd/distribution-tools/bounded_test.go b/modules/distribution/cmd/distribution-tools/bounded_test.go index e02f926ac..64cec0079 100644 --- a/modules/distribution/cmd/distribution-tools/bounded_test.go +++ b/modules/distribution/cmd/distribution-tools/bounded_test.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "strings" + "sync" "testing" "time" ) @@ -19,6 +21,36 @@ func TestAHungStepAnswersWithItsName(t *testing.T) { } } +// A step given up on is said given up by name, and the log says later when the call behind it ended and how (A-443a). +func TestAGivenUpStepSaysWhenItsCallEnded(t *testing.T) { + var mu sync.Mutex + var lines []string + ended := make(chan struct{}) + w := func(f string, a ...any) { + l := fmt.Sprintf(f, a...) + mu.Lock() + lines = append(lines, l) + mu.Unlock() + if strings.Contains(l, "the put of a chunk") && strings.Contains(l, "its call ended after") { + close(ended) + } + } + old := logTo.Swap(&w) + defer logTo.Store(old) + release := make(chan struct{}) + if _, err := step(10*time.Millisecond, "the put of a chunk", func() (int, error) { <-release; return 0, fmt.Errorf("bus closed") }); err == nil { + t.Fatal("not given up") + } + close(release) + <-ended + mu.Lock() + all := strings.Join(lines, "\n") + mu.Unlock() + if !strings.Contains(all, "given up") || !strings.Contains(all, "bus closed") { + t.Fatalf("log: %s", all) + } +} + // The finding is said off the caller's path: a writer that never answers does not hold the call that says it. func TestAFindingIsSaidOffThePath(t *testing.T) { block := make(chan struct{}) diff --git a/modules/distribution/cmd/distribution-tools/main.go b/modules/distribution/cmd/distribution-tools/main.go index b20db6f5d..ae4a503b4 100644 --- a/modules/distribution/cmd/distribution-tools/main.go +++ b/modules/distribution/cmd/distribution-tools/main.go @@ -16,6 +16,7 @@ import ( "os" "strings" "sync" + "sync/atomic" "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" @@ -29,7 +30,15 @@ import ( "distribution/internal/holder" ) -func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, "[distribution-tools] "+format+"\n", a...) } +// logTo is where logf writes; a test replaces it (atomically: a step given up on logs from its own goroutine). +var logTo atomic.Pointer[func(string, ...any)] + +func init() { + w := func(format string, a ...any) { fmt.Fprintf(os.Stderr, "[distribution-tools] "+format+"\n", a...) } + logTo.Store(&w) +} + +func logf(format string, a ...any) { (*logTo.Load())(format, a...) } // kept is one state of distribution's, through the runtime. type kept[T any] struct {