package testio
import "git.wntrmute.dev/kyle/goutils/testio"
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 ¶
- type BrokenCloser
- func NewBrokenCloser(buf []byte) *BrokenCloser
- func NewBrokenCloserString(s string) *BrokenCloser
- func (buf *BrokenCloser) Bytes() []byte
- func (buf *BrokenCloser) Close() error
- func (buf *BrokenCloser) Read(p []byte) (int, error)
- func (buf *BrokenCloser) Reset()
- func (buf *BrokenCloser) Write(p []byte) (int, error)
- type BrokenReadWriter
- func NewBrokenReadWriter(wlimit, rlimit int) *BrokenReadWriter
- func (brw *BrokenReadWriter) Extend(w, r int)
- func (brw *BrokenReadWriter) Read(p []byte) (int, error)
- func (brw *BrokenReadWriter) Reset()
- func (brw *BrokenReadWriter) Write(p []byte) (int, error)
- type BrokenWriter
- func NewBrokenWriter(limit int) *BrokenWriter
- func (w *BrokenWriter) Close() error
- func (w *BrokenWriter) Extend(n int)
- func (w *BrokenWriter) Reset()
- func (w *BrokenWriter) Write(p []byte) (int, error)
- type BufCloser
- func NewBufCloser(buf []byte) *BufCloser
- func NewBufCloserString(s string) *BufCloser
- func (buf *BufCloser) Bytes() []byte
- func (buf *BufCloser) Close() error
- func (buf *BufCloser) Len() int
- func (buf *BufCloser) Read(p []byte) (int, error)
- func (buf *BufCloser) Reset()
- func (buf *BufCloser) Write(p []byte) (int, error)
- type BufferConn
- func NewBufferConn() *BufferConn
- func (bc *BufferConn) Close() error
- func (bc *BufferConn) Read(p []byte) (int, error)
- func (bc *BufferConn) ReadClient(p []byte) (int, error)
- func (bc *BufferConn) Write(p []byte) (int, error)
- func (bc *BufferConn) WritePeer(p []byte) (int, error)
- type LoggingBuffer
- func NewLoggingBuffer(rw io.ReadWriter) *LoggingBuffer
- func (lb *LoggingBuffer) LogTo(w io.Writer)
- func (lb *LoggingBuffer) Read(p []byte) (int, error)
- func (lb *LoggingBuffer) SetName(name string)
- func (lb *LoggingBuffer) Write(p []byte) (int, error)
- type SilentBrokenWriter
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 ¶
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 ¶
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 ¶
Bytes returns the contents of the buffer as a byte slice.
func (*BufCloser) Close ¶
Close is a stub function to satisfy the io.Closer interface.
func (*BufCloser) Len ¶
Len returns the length of the buffer.
func (*BufCloser) Read ¶
Read reads data from the BufCloser.
func (*BufCloser) Reset ¶
func (buf *BufCloser) Reset()
Reset clears the internal buffer.
func (*BufCloser) Write ¶
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.
Source Files ¶
testio.go
- Version
- v1.7.7 (latest)
- Published
- Jun 15, 2024
- Platform
- linux/amd64
- Imports
- 5 packages
- Last checked
- 4 days ago –
Tools for package owners.