Help us shape the future of Go! Take the 2026 Go Developer Survey

testio

package module
v0.0.0-...-20addbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2014 License: ISC Imports: 5 Imported by: 0

Documentation

Overview

Package testio implements various io utility types. Included are BrokenWriter, which fails after writing a certain number of bytes; a BufCloser, which wraps a bytes.Buffer in a Close method; a BrokenReadWriter, which fails after writing a certain number of bytes and/or reading a certain number of bytes; a LoggingBuffer that logs all reads and writes; and a BufferConn, that is designed to simulate net.Conn.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BrokenReadWriter

type BrokenReadWriter struct {
	// contains filtered or unexported fields
}

BrokenReadWriter implements a broken reader and writer, backed by a bytes.Buffer.

func NewBrokenReadWriter

func NewBrokenReadWriter(wlimit, rlimit int) *BrokenReadWriter

NewBrokenReadWriter initialises a new BrokerReadWriter with an empty reader and the specified limits.

func (*BrokenReadWriter) Extend

func (brw *BrokenReadWriter) Extend(w, r int)

Extend increases the BrokenReadWriter limit.

func (*BrokenReadWriter) Read

func (brw *BrokenReadWriter) Read(p []byte) (int, error)

Read satisfies the Reader interface.

func (*BrokenReadWriter) Reset

func (brw *BrokenReadWriter) Reset()

Reset clears the internal buffer. It retains its original limit.

func (*BrokenReadWriter) Write

func (brw *BrokenReadWriter) Write(p []byte) (int, error)

Write satisfies the Writer interface.

type BrokenWriter

type BrokenWriter struct {
	// contains filtered or unexported fields
}

BrokenWriter implements an io.Writer that fails after a certain number of bytes. This can be used to simulate a network connection that breaks during write or a file on a filesystem that becomes full, for example. A BrokenWriter doesn't actually store any data.

func NewBrokenWriter

func NewBrokenWriter(limit int) *BrokenWriter

NewBrokenWriter creates a new BrokenWriter that can store only limit bytes.

func (*BrokenWriter) Extend

func (w *BrokenWriter) Extend(n int)

Extend increases the byte limit to allow more data to be written.

func (*BrokenWriter) Reset

func (w *BrokenWriter) Reset()

Reset clears the limit and bytes in the BrokenWriter. Extend needs to be called to allow data to be written.

func (*BrokenWriter) Write

func (w *BrokenWriter) Write(p []byte) (int, error)

Write will write the byte slice to the BrokenWriter, failing if the maximum number of bytes has been reached.

type BufCloser

type BufCloser struct {
	// contains filtered or unexported fields
}

BufCloser is a buffer wrapped with a Close method.

func NewBufCloser

func NewBufCloser(buf []byte) *BufCloser

NewBufCloser creates and initializes a new BufCloser using buf as its initial contents. It is intended to prepare a BufCloser to read existing data. It can also be used to size the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.

func NewBufCloserString

func NewBufCloserString(s string) *BufCloser

NewBufCloserString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.

func (*BufCloser) Bytes

func (buf *BufCloser) Bytes() []byte

Bytes returns the contents of the buffer as a byte slice.

func (*BufCloser) Close

func (buf *BufCloser) Close()

Close is a stub function to satisfy the io.Closer interface.

func (*BufCloser) Read

func (buf *BufCloser) Read(p []byte) (int, error)

Read reads data from the BufCloser.

func (*BufCloser) Reset

func (buf *BufCloser) Reset()

Reset clears the internal buffer.

func (*BufCloser) Write

func (buf *BufCloser) Write(p []byte) (int, error)

Write writes the data to the BufCloser.

type BufferConn

type BufferConn struct {
	// contains filtered or unexported fields
}

BufferConn is a type that can be used to simulate network connections between a "client" (the code that uses the BufferConn) and some simulated "peer". Writes go to a "client" buffer, which is used to record the data sent by the caller, which may be read with ReadPeer. The peer's responses may be simulated by calling WritePeer; when the client reads from the BufferConn, they will see this data.

func NewBufferConn

func NewBufferConn() *BufferConn

NewBufferConn initialises a new simulated network connection.

func (*BufferConn) Read

func (bc *BufferConn) Read(p []byte) (int, error)

Read reads from the peer buffer.

func (*BufferConn) ReadClient

func (bc *BufferConn) ReadClient(p []byte) (int, error)

ReadClient reads data from the client buffer.

func (*BufferConn) Write

func (bc *BufferConn) Write(p []byte) (int, error)

Write writes to the client buffer.

func (*BufferConn) WritePeer

func (bc *BufferConn) WritePeer(p []byte) (int, error)

WritePeer writes data to the peer buffer.

type LoggingBuffer

type LoggingBuffer struct {
	// contains filtered or unexported fields
}

A LoggingBuffer is an io.ReadWriter that prints the hex value of the data for all reads and writes.

func NewLoggingBuffer

func NewLoggingBuffer(rw io.ReadWriter) *LoggingBuffer

NewLoggingBuffer creates a logging buffer from an existing io.ReadWriter. By default, it will log to standard error.

func (*LoggingBuffer) LogTo

func (lb *LoggingBuffer) LogTo(w io.Writer)

LogTo sets the io.Writer that the buffer will write logs to.

func (*LoggingBuffer) Read

func (lb *LoggingBuffer) Read(p []byte) (int, error)

Read reads the data from the logging buffer and writes the data to the logging writer.

func (*LoggingBuffer) SetName

func (lb *LoggingBuffer) SetName(name string)

SetName gives a name to the logging buffer to help distinguish output from this buffer.

func (*LoggingBuffer) Write

func (lb *LoggingBuffer) Write(p []byte) (int, error)

Write writes the data to the logging buffer and writes the data to the logging writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL