iotest
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package iotest implements Readers and Writers useful mainly for testing.
Index¶
- Variables
- func DataErrReader(r io.Reader) io.Reader
- func ErrReader(err error) io.Reader
- func HalfReader(r io.Reader) io.Reader
- func NewReadLogger(prefix string, r io.Reader) io.Reader
- func NewWriteLogger(prefix string, w io.Writer) io.Writer
- func OneByteReader(r io.Reader) io.Reader
- func TestReader(r io.Reader, content []byte) error
- func TimeoutReader(r io.Reader) io.Reader
- func TruncateWriter(w io.Writer, n int64) io.Writer
Examples¶
Constants¶
This section is empty.
Variables¶
var ErrTimeout =errors.New("timeout")
ErrTimeout is a fake timeout error.
Functions¶
funcDataErrReader¶
DataErrReader changes the way errors are handled by a Reader. Normally, aReader returns an error (typically EOF) from the first Read call after thelast piece of data is read. DataErrReader wraps a Reader and changes itsbehavior so the final error is returned along with the final data, insteadof in the first call after the final data.
funcErrReader¶added ingo1.16
ErrReader returns anio.Reader that returns 0, err from all Read calls.
Example¶
package mainimport ("errors""fmt""testing/iotest")func main() {// A reader that always returns a custom error.r := iotest.ErrReader(errors.New("custom error"))n, err := r.Read(nil)fmt.Printf("n: %d\nerr: %q\n", n, err)}
Output:n: 0err: "custom error"
funcHalfReader¶
HalfReader returns a Reader that implements Readby reading half as many requested bytes from r.
funcNewReadLogger¶
NewReadLogger returns a reader that behaves like r exceptthat it logs (usinglog.Printf) each read to standard error,printing the prefix and the hexadecimal data read.
funcNewWriteLogger¶
NewWriteLogger returns a writer that behaves like w exceptthat it logs (usinglog.Printf) each write to standard error,printing the prefix and the hexadecimal data written.
funcOneByteReader¶
OneByteReader returns a Reader that implementseach non-empty Read by reading one byte from r.
funcTestReader¶added ingo1.16
TestReader tests that reading from r returns the expected file content.It does reads of different sizes, until EOF.If r implementsio.ReaderAt orio.Seeker, TestReader also checksthat those operations behave as they should.
If TestReader finds any misbehaviors, it returns an error reporting them.The error text may span multiple lines.
funcTimeoutReader¶
TimeoutReader returnsErrTimeout on the second readwith no data. Subsequent calls to read succeed.
Types¶
This section is empty.