As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit f7fe1cce authored by Ash McKenzie's avatar Ash McKenzie
Browse files

Add new CountingWriter struct

parent 86e51613
No related branches found
No related tags found
No related merge requests found
package readwriter
import "io"
import (
"io"
)
type ReadWriter struct {
Out io.Writer
In io.Reader
ErrOut io.Writer
}
// CountingWriter wraps an io.Writer and counts all the writes. Accessing
// the count N is not thread-safe.
type CountingWriter struct {
W io.Writer
N int64
}
func (cw *CountingWriter) Write(p []byte) (int, error) {
n, err := cw.W.Write(p)
cw.N += int64(n)
return n, err
}
package readwriter
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func TestCountingWriter_Write(t *testing.T) {
testString := []byte("test string")
buffer := &bytes.Buffer{}
cw := &CountingWriter{
W: buffer,
}
n, err := cw.Write(testString)
require.NoError(t, err)
require.Equal(t, 11, n)
require.Equal(t, int64(11), cw.N)
cw.Write(testString)
require.Equal(t, int64(22), cw.N)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment