liveBook · Manning

Iotest

As explained in 100 Go Mistakes and How to Avoid Them

iotest is a Go standard-library package that provides utilities for testing readers and writers, including fault-injecting wrappers.

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

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Defines several testing wrappers and demonstrates TestReader plus configurable retry handling, without descending into package implementation internals.
Examples
Uses LowerCaseReader validation, TimeoutReader failures, and a retrying readAll implementation.
What it assumes
Assumes familiarity with io.Reader and io.Writer while demonstrating custom-reader testing and read-error handling.
What you can do after
Diagnose reader and writer contract violations and evaluate application behavior under short reads, truncation, and transient errors.
How it connects
Connects io.Reader, io.ReaderAt, io.Writer, io.ReadAll, custom readers, and retry-based error handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.