Skip to content

tests: Add tests cases where writing fails #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,57 @@ func FuzzChainWriter(f *testing.F) {
}
})
}

// https://github.com/golang/go/issues/54111
type LimitedWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
Err error // error to be returned once limit is reached
}

func (lw *LimitedWriter) Write(p []byte) (int, error) {
if lw.N < 1 {
return 0, lw.Err
}
if lw.N < int64(len(p)) {
p = p[:lw.N]
}
n, err := lw.W.Write(p)
lw.N -= int64(n)
return n, err
}

func TestEncodeError(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := NewEncoder(&LimitedWriter{io.Discard, 0, io.EOF})

_, err := e.Write(tc.dec)
// err can be nil if no groups have been flushed, call close
if err == nil {
err = e.Close()
}
if err != io.EOF {
t.Errorf("Unexpected error: %v", err)
}
})
}
}

func TestDecodeError(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// The empty string is expected to have no writes
if len(tc.dec) == 0 {
t.SkipNow()
}

d := NewDecoder(&LimitedWriter{io.Discard, 0, io.EOF})

_, err := d.Write(tc.enc)
if err != io.EOF {
t.Errorf("Unexpected error: %v", err)
}
})
}
}