tests: Do not allow forced type asserts (#20905)

This commit is contained in:
Klaus Post
2025-02-18 08:25:55 -08:00
committed by GitHub
parent aeabac9181
commit 90f5e1e5f6
100 changed files with 371 additions and 358 deletions

View File

@@ -25,10 +25,10 @@ import (
"io"
"os"
"runtime/debug"
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/minio/minio/internal/bpool"
"github.com/minio/minio/internal/disk"
)
@@ -39,28 +39,39 @@ const (
LargeBlock = 1 * humanize.MiByte // Default r/w block size for normal objects.
)
// AlignedBytePool is a pool of fixed size aligned blocks
type AlignedBytePool struct {
size int
p bpool.Pool[*[]byte]
}
// NewAlignedBytePool creates a new pool with the specified size.
func NewAlignedBytePool(sz int) *AlignedBytePool {
return &AlignedBytePool{size: sz, p: bpool.Pool[*[]byte]{New: func() *[]byte {
b := disk.AlignedBlock(sz)
return &b
}}}
}
// aligned sync.Pool's
var (
ODirectPoolLarge = sync.Pool{
New: func() interface{} {
b := disk.AlignedBlock(LargeBlock)
return &b
},
}
ODirectPoolMedium = sync.Pool{
New: func() interface{} {
b := disk.AlignedBlock(MediumBlock)
return &b
},
}
ODirectPoolSmall = sync.Pool{
New: func() interface{} {
b := disk.AlignedBlock(SmallBlock)
return &b
},
}
ODirectPoolLarge = NewAlignedBytePool(LargeBlock)
ODirectPoolMedium = NewAlignedBytePool(MediumBlock)
ODirectPoolSmall = NewAlignedBytePool(SmallBlock)
)
// Get a block.
func (p *AlignedBytePool) Get() *[]byte {
return p.p.Get()
}
// Put a block.
func (p *AlignedBytePool) Put(pb *[]byte) {
if pb != nil && len(*pb) == p.size {
p.p.Put(pb)
}
}
// WriteOnCloser implements io.WriteCloser and always
// executes at least one write operation if it is closed.
//
@@ -250,15 +261,6 @@ func NopCloser(w io.Writer) io.WriteCloser {
return nopCloser{w}
}
const copyBufferSize = 32 * 1024
var copyBufPool = sync.Pool{
New: func() interface{} {
b := make([]byte, copyBufferSize)
return &b
},
}
// SkipReader skips a given number of bytes and then returns all
// remaining data.
type SkipReader struct {
@@ -274,12 +276,11 @@ func (s *SkipReader) Read(p []byte) (int, error) {
}
if s.skipCount > 0 {
tmp := p
if s.skipCount > l && l < copyBufferSize {
if s.skipCount > l && l < SmallBlock {
// We may get a very small buffer, so we grab a temporary buffer.
bufp := copyBufPool.Get().(*[]byte)
buf := *bufp
tmp = buf[:copyBufferSize]
defer copyBufPool.Put(bufp)
bufp := ODirectPoolSmall.Get()
tmp = *bufp
defer ODirectPoolSmall.Put(bufp)
l = int64(len(tmp))
}
for s.skipCount > 0 {
@@ -309,7 +310,7 @@ type writerOnly struct {
// Copy is exactly like io.Copy but with reusable buffers.
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
bufp := ODirectPoolMedium.Get().(*[]byte)
bufp := ODirectPoolMedium.Get()
defer ODirectPoolMedium.Put(bufp)
buf := *bufp

View File

@@ -102,7 +102,7 @@ func TestCloseOnWriter(t *testing.T) {
// Test for AppendFile.
func TestAppendFile(t *testing.T) {
f, err := os.CreateTemp("", "")
f, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Fatal(err)
}
@@ -111,7 +111,7 @@ func TestAppendFile(t *testing.T) {
f.WriteString("aaaaaaaaaa")
f.Close()
f, err = os.CreateTemp("", "")
f, err = os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Fatal(err)
}
@@ -162,7 +162,7 @@ func TestSkipReader(t *testing.T) {
}
func TestSameFile(t *testing.T) {
f, err := os.CreateTemp("", "")
f, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
}
@@ -193,7 +193,7 @@ func TestSameFile(t *testing.T) {
}
func TestCopyAligned(t *testing.T) {
f, err := os.CreateTemp("", "")
f, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
}
@@ -202,7 +202,7 @@ func TestCopyAligned(t *testing.T) {
r := strings.NewReader("hello world")
bufp := ODirectPoolSmall.Get().(*[]byte)
bufp := ODirectPoolSmall.Get()
defer ODirectPoolSmall.Put(bufp)
written, err := CopyAligned(f, io.LimitReader(r, 5), *bufp, r.Size(), f)