package main import ( "fmt" "strings" "sync" "testing" "time" ) // A bus operation that never answers becomes an error naming its step, within the bound. func TestAHungStepAnswersWithItsName(t *testing.T) { block := make(chan struct{}) defer close(block) _, err := step(10*time.Millisecond, "the write of the idle key", func() (int, error) { <-block; return 0, nil }) if err == nil || !strings.Contains(err.Error(), "the write of the idle key did not answer within") { t.Fatalf("err = %v", err) } if v, err := step(time.Second, "x", func() (int, error) { return 7, nil }); v != 7 || err != nil { t.Fatalf("%d %v", v, err) } } // 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{}) defer close(block) say := boundedFinding(func(string, string, string, bool) { <-block }) done := make(chan struct{}) go func() { say("incoming-full", "incoming", "w", false) say("incoming-full", "incoming", "w", false) close(done) }() select { case <-done: case <-time.After(5 * time.Second): t.Fatal("the call was held by the finding") } }