Standard library / testing/iotest

testing/iotest.TimeoutReader()

Inject ErrTimeout on the second read, then delegate subsequent reads to the underlying reader. Expected stdout: '0 true\nb true\n'.

Formal Syntax & Structure

go syntaxgo
func TimeoutReader(r io.Reader) io.Reader

Parameters & Attributes

NameTypeDescription
rio.ReaderUnderlying reader.

Return Value

io.Reader

Result of the operation described above.

Working Example

Example Snippetgo
package main
import (  "fmt"  "strings"  "testing/iotest")
func main() {  r := iotest.TimeoutReader(strings.NewReader("abc"))  b := make([]byte, 1)  r.Read(b)  n, err := r.Read(b)  fmt.Println(n, err == iotest.ErrTimeout)  n, err = r.Read(b)  fmt.Println(string(b[:n]), err == nil)}
Go Reference