Skip to content

Commit

Permalink
util: fix flaky test TestRequestBuffers (#8117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman authored May 13, 2024
1 parent 25c9837 commit 31edde1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions pkg/util/requestbuffers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package util

import (
"runtime/debug"
"testing"
"unsafe"

Expand All @@ -11,15 +10,9 @@ import (
)

func TestRequestBuffers(t *testing.T) {
// Disable GC
originalGCPercent := debug.SetGCPercent(-1)
defer debug.SetGCPercent(originalGCPercent)

const maxBufferSize = 32 * 1024

p := NewBucketedBufferPool(1024, maxBufferSize, 2)

rb := NewRequestBuffers(p)
rb := NewRequestBuffers(&fakePool{maxBufferSize: maxBufferSize})
t.Cleanup(rb.CleanUp)

b := rb.Get(1024)
Expand Down Expand Up @@ -66,3 +59,25 @@ func TestRequestBuffers(t *testing.T) {
assert.Zero(t, b.Len())
})
}

type fakePool struct {
maxBufferSize int
buffers [][]byte
}

func (p *fakePool) Get(sz int) []byte {
if sz <= p.maxBufferSize {
for i, b := range p.buffers {
if cap(b) < sz {
continue
}
p.buffers = append(p.buffers[:i], p.buffers[i+1:]...)
return b
}
}
return make([]byte, 0, sz)
}

func (p *fakePool) Put(s []byte) {
p.buffers = append(p.buffers, s[:0])
}

0 comments on commit 31edde1

Please sign in to comment.