Denis Kirillov
414f3943e2
All checks were successful
/ DCO (pull_request) Successful in 2m1s
/ Vulncheck (pull_request) Successful in 2m31s
/ Builds (1.20) (pull_request) Successful in 2m39s
/ Builds (1.21) (pull_request) Successful in 2m31s
/ Lint (pull_request) Successful in 3m14s
/ Tests (1.20) (pull_request) Successful in 2m34s
/ Tests (1.21) (pull_request) Successful in 2m10s
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package layer
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWrapReader(t *testing.T) {
|
|
src := make([]byte, 1024*1024+1)
|
|
_, err := rand.Read(src)
|
|
require.NoError(t, err)
|
|
h := sha256.Sum256(src)
|
|
|
|
streamHash := sha256.New()
|
|
reader := bytes.NewReader(src)
|
|
wrappedReader := wrapReader(reader, 64*1024, func(buf []byte) {
|
|
streamHash.Write(buf)
|
|
})
|
|
|
|
dst, err := io.ReadAll(wrappedReader)
|
|
require.NoError(t, err)
|
|
require.Equal(t, src, dst)
|
|
require.Equal(t, h[:], streamHash.Sum(nil))
|
|
}
|
|
|
|
func TestGoroutinesDontLeakInPutAndHash(t *testing.T) {
|
|
tc := prepareContext(t)
|
|
|
|
content := make([]byte, 128*1024)
|
|
_, err := rand.Read(content)
|
|
require.NoError(t, err)
|
|
payload := bytes.NewReader(content)
|
|
|
|
prm := PrmObjectCreate{
|
|
Filepath: tc.obj,
|
|
Payload: payload,
|
|
}
|
|
|
|
expErr := errors.New("some error")
|
|
tc.testFrostFS.SetObjectPutError(tc.obj, expErr)
|
|
_, _, _, _, err = tc.layer.objectPutAndHash(tc.ctx, prm, tc.bktInfo)
|
|
require.ErrorIs(t, err, expErr)
|
|
require.Empty(t, payload.Len(), "body must be read out otherwise goroutines can leak in wrapReader")
|
|
}
|