Count repeated report write failures once
This commit is contained in:
@@ -53,6 +53,10 @@ One best-effort pass updates earlier reports with stale evidence. Diagnostics
|
|||||||
tests cover both initially failed runs and failures that arise during
|
tests cover both initially failed runs and failures that arise during
|
||||||
publication of a completed run.
|
publication of a completed run.
|
||||||
|
|
||||||
|
The follow-up fix counts repeated write failures for one report as one
|
||||||
|
delivery failure and retains both causes. The regression makes both writes to
|
||||||
|
`recoveries.yml` fail and verifies that the surviving reports agree on the count.
|
||||||
|
|
||||||
**Affected plan:** Stage 4, item 3, and Stage 17, item 1.
|
**Affected plan:** Stage 4, item 3, and Stage 17, item 1.
|
||||||
|
|
||||||
**Evidence:** [Report publication](../../internal/core/diagnostics/output_session.go#L311)
|
**Evidence:** [Report publication](../../internal/core/diagnostics/output_session.go#L311)
|
||||||
|
|||||||
@@ -329,6 +329,7 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle
|
|||||||
var rendered reportState
|
var rendered reportState
|
||||||
var written [3]reportState
|
var written [3]reportState
|
||||||
var attemptedFailed [3]bool
|
var attemptedFailed [3]bool
|
||||||
|
failureIndexes := [3]int{-1, -1, -1}
|
||||||
refresh := func() bool {
|
refresh := func() bool {
|
||||||
state := observed()
|
state := observed()
|
||||||
if state == rendered {
|
if state == rendered {
|
||||||
@@ -359,7 +360,14 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle
|
|||||||
attemptedFailed[i] = true
|
attemptedFailed[i] = true
|
||||||
}
|
}
|
||||||
if err := s.replace(writeCtx, s.reportDestinations[i], bundleParts(reports)[i]); err != nil {
|
if err := s.replace(writeCtx, s.reportDestinations[i], bundleParts(reports)[i]); err != nil {
|
||||||
failures = append(failures, FileFailure{Path: s.reports[i], Phase: phase, Kind: kind, Err: err})
|
// A retry adds a cause to the same delivery failure. Reports count
|
||||||
|
// the failed destination, so the rendered facts stay current.
|
||||||
|
if previous := failureIndexes[i]; previous >= 0 {
|
||||||
|
failures[previous].Err = errors.Join(failures[previous].Err, err)
|
||||||
|
} else {
|
||||||
|
failureIndexes[i] = len(failures)
|
||||||
|
failures = append(failures, FileFailure{Path: s.reports[i], Phase: phase, Kind: kind, Err: err})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
written[i] = rendered
|
written[i] = rendered
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -427,6 +428,40 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRepeatedFailureOfOneReportCountsOnce(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
initial, retry := errors.New("initial write failed"), errors.New("retry failed")
|
||||||
|
replace := session.replace
|
||||||
|
attempts := 0
|
||||||
|
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
||||||
|
if filepath.Base(path) == RecoveriesFile {
|
||||||
|
attempts++
|
||||||
|
if attempts == 1 {
|
||||||
|
return initial
|
||||||
|
}
|
||||||
|
return retry
|
||||||
|
}
|
||||||
|
return replace(ctx, path, payload)
|
||||||
|
}
|
||||||
|
err = session.Finish(context.Background(), nil, nil, func(_ error, failures []FileFailure) (ReportBundle, error) {
|
||||||
|
payload := []byte(strconv.Itoa(len(failures)))
|
||||||
|
return ReportBundle{Errors: payload, Recoveries: payload, Markdown: payload}, nil
|
||||||
|
})
|
||||||
|
if !errors.Is(err, initial) || !errors.Is(err, retry) || attempts != 2 {
|
||||||
|
t.Fatalf("writes = %d, error = %v", attempts, err)
|
||||||
|
}
|
||||||
|
for _, name := range []string{ErrorsFile, MarkdownFile} {
|
||||||
|
got, readErr := os.ReadFile(filepath.Join(dir, name))
|
||||||
|
if readErr != nil || string(got) != "1" {
|
||||||
|
t.Errorf("%s recorded %q failures, error = %v", name, got, readErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type writerFunc func([]byte) (int, error)
|
type writerFunc func([]byte) (int, error)
|
||||||
|
|
||||||
func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) }
|
func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) }
|
||||||
|
|||||||
Reference in New Issue
Block a user