iotest provides utilities for testing io.Reader and io.Writer implementations. It exposes incorrect assumptions about read counts, buffer filling, interface behavior, and error handling. section 11.7.2
Use its utilities to exercise boundary behavior without rewriting test doubles:
- Call iotest.TestReader with a custom reader and expected bytes; it checks returned byte counts, buffer filling, and additional interfaces such as io.ReaderAt. section 11.7.2
- Wrap readers with ErrReader, HalfReader, OneByteReader, or TimeoutReader to inject errors, short reads, one-byte reads, or a second-read failure. section 11.7.2
- Wrap writers with TruncateWriter to stop writing silently after a configured number of bytes. section 11.7.2
Testing a reader that transforms input
A LowerCaseReader can be checked against expected output such as acegi. Passing it to TestReader verifies that its transformed stream still obeys reader behavior; the runnable pattern appears in b1. section 11.7.2
Testing tolerance for read failures
Passing a TimeoutReader to a function that uses io.ReadAll exposes an error on the second read. A retrying readAll loop can tolerate a configured number of non-EOF errors, as shown in b3, b4, and b5. section 11.7.2
