distribution: a step given up at its bound is said given up, and its call's end is said later
Builds: design 47 G A-443, A-443a
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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{})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user