Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
BufferWriter for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tirsen committed Sep 23, 2020
1 parent 5e81640 commit 1d86f29
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/storage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,39 @@ func newUploaderWriter(uploader Uploader, chunkSize int) *uploaderWriter {
uploader: uploader,
buf: bytes.NewBuffer(make([]byte, 0, chunkSize))}
}

// BufferWriter is a Writer implementation on top of bytes.Buffer that is useful for testing
type BufferWriter struct {
buf *bytes.Buffer
}

// Write delegates to bytes.Buffer
func (u *BufferWriter) Write(ctx context.Context, p []byte) (int, error) {
return u.buf.Write(p)
}

// Close delegates to bytes.Buffer
func (u *BufferWriter) Close(ctx context.Context) error {
// noop
return nil
}

// Bytes delegates to bytes.Buffer
func (u *BufferWriter) Bytes() []byte {
return u.buf.Bytes()
}

// String delegates to bytes.Buffer
func (u *BufferWriter) String() string {
return u.buf.String()
}

// Reset delegates to bytes.Buffer
func (u *BufferWriter) Reset() {
u.buf.Reset()
}

// NewBufferWriter creates a Writer that simply writes to a buffer (useful for testing)
func NewBufferWriter() *BufferWriter {
return &BufferWriter{buf: &bytes.Buffer{}}
}

0 comments on commit 1d86f29

Please sign in to comment.