Stream discarded CI log lines without retaining them
This commit is contained in:
@@ -115,9 +115,10 @@ func ReadResult(reader io.Reader, expected Manifest) (JobResult, error) {
|
||||
var canonical []byte
|
||||
r := bufio.NewReader(reader)
|
||||
for {
|
||||
line, err := r.ReadString('\n')
|
||||
if strings.HasPrefix(line, ResultPrefix) {
|
||||
parsed, parseErr := decodeResult([]byte(strings.TrimSuffix(strings.TrimSuffix(line, "\n"), "\r")[len(ResultPrefix):]))
|
||||
line, err := readResultLine(r)
|
||||
if line != nil {
|
||||
payload := bytes.TrimSuffix(bytes.TrimSuffix(line[len(ResultPrefix):], []byte{'\n'}), []byte{'\r'})
|
||||
parsed, parseErr := decodeResult(payload)
|
||||
if parseErr != nil {
|
||||
return JobResult{}, fmt.Errorf("malformed rehearsal result: %w", parseErr)
|
||||
}
|
||||
@@ -146,3 +147,20 @@ func ReadResult(reader io.Reader, expected Manifest) (JobResult, error) {
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
// readResultLine retains candidate frames and discards ordinary lines a
|
||||
// fragment at a time. The fixed prefix fits in the default reader buffer.
|
||||
func readResultLine(reader *bufio.Reader) ([]byte, error) {
|
||||
prefix, _ := reader.Peek(len(ResultPrefix))
|
||||
candidate := bytes.Equal(prefix, []byte(ResultPrefix))
|
||||
var line []byte
|
||||
for {
|
||||
fragment, err := reader.ReadSlice('\n')
|
||||
if candidate {
|
||||
line = append(line, fragment...)
|
||||
}
|
||||
if err != bufio.ErrBufferFull {
|
||||
return line, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package rehearsal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -10,17 +11,27 @@ import (
|
||||
func TestReadResultHandlesLongChunkedLogsAndDuplicateEvidence(t *testing.T) {
|
||||
_, profile, _, identity, attempt := rehearsalFixture(t)
|
||||
manifest := NewManifest(identity, attempt, "root", "releasekit.json", profile)
|
||||
result := JobResult{Manifest: manifest, Scope: "page", Build: "not_requested", Delivery: "complete", Verification: "complete", PageID: 12, Artifacts: []ArtifactEvidence{}}
|
||||
result := JobResult{Manifest: manifest, Scope: "page", Build: "not_requested", Delivery: "complete", Verification: "complete", PageID: 12, Artifacts: []ArtifactEvidence{}, Diagnostics: []string{strings.Repeat("safe phase detail ", 1000)}}
|
||||
frame, err := EncodeResult(result)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
log := append([]byte(strings.Repeat("x", 100000)+"\n"), frame...)
|
||||
log = append(log, frame...)
|
||||
log = append(log, []byte(ResultPrefix[:len(ResultPrefix)-1]+strings.Repeat("ordinary ", 20000)+"\n")...)
|
||||
log = append(log, bytes.TrimSuffix(frame, []byte{'\n'})...)
|
||||
log = append(log, '\r', '\n')
|
||||
log = append(log, bytes.TrimSuffix(frame, []byte{'\n'})...)
|
||||
got, err := ReadResult(&oneByteReader{data: log}, manifest)
|
||||
if err != nil || got.PageID != 12 || got.Verification != "complete" {
|
||||
if err != nil || got.PageID != 12 || got.Verification != "complete" || len(got.Diagnostics) != 1 || got.Diagnostics[0] != result.Diagnostics[0] {
|
||||
t.Fatalf("result=%+v error=%v", got, err)
|
||||
}
|
||||
for _, suffix := range []string{strings.Repeat("z", 100000), ResultPrefix[:len(ResultPrefix)-1]} {
|
||||
withFinalLog := append(append([]byte(nil), frame...), suffix...)
|
||||
got, err := ReadResult(bytes.NewReader(withFinalLog), manifest)
|
||||
if err != nil || got.PageID != result.PageID {
|
||||
t.Fatalf("unterminated ordinary line: result=%+v error=%v", got, err)
|
||||
}
|
||||
}
|
||||
conflict := result
|
||||
conflict.PageID = 13
|
||||
other, _ := EncodeResult(conflict)
|
||||
@@ -30,9 +41,13 @@ func TestReadResultHandlesLongChunkedLogsAndDuplicateEvidence(t *testing.T) {
|
||||
if _, err := ReadResult(strings.NewReader(ResultPrefix+`{}`+"\n"), manifest); err == nil {
|
||||
t.Fatal("accepted malformed result")
|
||||
}
|
||||
if _, err := ReadResult(strings.NewReader("ordinary log\n"), manifest); err == nil {
|
||||
if _, err := ReadResult(strings.NewReader("ordinary log\n"), manifest); !errors.Is(err, ErrResultAbsent) {
|
||||
t.Fatal("accepted absent result")
|
||||
}
|
||||
readFailure := errors.New("fixture log read failed")
|
||||
if _, err := ReadResult(io.MultiReader(bytes.NewReader(frame), failingLogReader{err: readFailure}), manifest); !errors.Is(err, readFailure) {
|
||||
t.Fatalf("log read error lost after valid evidence: %v", err)
|
||||
}
|
||||
otherManifest := manifest
|
||||
otherManifest.Selector = "other"
|
||||
otherManifest.Tag = "releasekit-test/other/" + attempt
|
||||
@@ -44,6 +59,10 @@ func TestReadResultHandlesLongChunkedLogsAndDuplicateEvidence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type failingLogReader struct{ err error }
|
||||
|
||||
func (r failingLogReader) Read([]byte) (int, error) { return 0, r.err }
|
||||
|
||||
type oneByteReader struct{ data []byte }
|
||||
|
||||
func (r *oneByteReader) Read(p []byte) (int, error) {
|
||||
|
||||
Reference in New Issue
Block a user