testio

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2018 License: ISC, ISC Imports: 5 Imported by: 0

README

testio

This is a collection of various utility io types:

  • BrokenReadWriter
  • BrokenWriter
  • BufCloser
  • BufferConn
  • LoggingBuffer

You can check out the godoc for dtails.

It was imported from kisom/testio. The original Git directory is preserved in git-hist.tar.xz.

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 BrokenCloser

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

BrokenCloser is a BufCloser that fails to close.

func NewBrokenCloser

func NewBrokenCloser(buf []byte) *BrokenCloser

NewBrokenCloser creates and initializes a new BrokenCloser using buf as its initial contents. It is intended to prepare a BrokenCloser 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 NewBrokenCloserString

func NewBrokenCloserString(s string) *BrokenCloser

NewBrokenCloserString 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 (*BrokenCloser) Bytes

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

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

func (*BrokenCloser) Close

func (buf *BrokenCloser) Close() error

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

func (*BrokenCloser) Read

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

Read reads data from the BrokenCloser.

func (*BrokenCloser) Reset

func (buf *BrokenCloser) Reset()

Reset clears the internal buffer.

func (*BrokenCloser) Write

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

Write writes the data to the BrokenCloser.

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) Close

func (w *BrokenWriter) Close() error

Close is provided to satisfy the Closer interface.

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() error

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) Close

func (bc *BufferConn) Close() error

Close is a dummy operation that allows the BufferConn to be used as an io.Closer.

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.

type SilentBrokenWriter

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

SilentBrokenWriter implements an io.Writer that fails after a certain number of bytes. However, this failure is silent: it just reports fewer bytes written than p. It doesn't actually store any data, and is used to verify that io.Writer implementations properly return errors on short writes.

func NewSilentBrokenWriter

func NewSilentBrokenWriter(limit int) *SilentBrokenWriter

NewSilentBrokenWriter creates a new SilentBrokenWriter that can store only limit bytes.

func (*SilentBrokenWriter) Close

func (w *SilentBrokenWriter) Close() error

Close is provided to satisfy the Closer interface.

func (*SilentBrokenWriter) Extend

func (w *SilentBrokenWriter) Extend(n int)

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

func (*SilentBrokenWriter) Reset

func (w *SilentBrokenWriter) Reset()

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

func (*SilentBrokenWriter) Write

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

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

Jump to

Keyboard shortcuts

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