Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions issue23_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestIssue23(t *testing.T) {
v = []byte("0123456789") // 10 bytes
}
if fill {
for i := 0; i < b.Capacity(); i++ {
for range b.Capacity() {
if writeErr := b.WriteByte(' '); writeErr != nil {
err = writeErr
return
Expand All @@ -32,7 +32,7 @@ func TestIssue23(t *testing.T) {
}

var n int
for i := 0; i < 1000; i++ {
for range 1000 {
n, err = b.Write(v)
full = b.IsFull()
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestOverwriteVariousSizes(t *testing.T) {
data[i] = byte(i % 256)
}

for i := 0; i < tc.numWrites; i++ {
for i := range tc.numWrites {
n, err := b.Write(data)
if err != nil {
t.Errorf("Write failed at iteration %d: capacity=%d, writeSize=%d, err=%v",
Expand Down
18 changes: 9 additions & 9 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"bytes"
"fmt"
"io"
"sort"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestPipe2(t *testing.T) {
r, w := New(256).Pipe()
go reader(t, r, c)
var buf = make([]byte, 64)
for i := 0; i < 5; i++ {
for i := range 5 {
p := buf[0 : 5+i*10]
n, err := w.Write(p)
if n != len(p) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestPipe3(t *testing.T) {
c := make(chan pipeReturn)
r, w := New(256).Pipe()
var wdat = make([]byte, 128)
for i := 0; i < len(wdat); i++ {
for i := range wdat {
wdat[i] = byte(i)
}
go writer(w, wdat, c)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestPipe3(t *testing.T) {
if tot != 128 {
t.Fatalf("total read %d != 128", tot)
}
for i := 0; i < 128; i++ {
for i := range 128 {
if rdat[i] != byte(i) {
t.Fatalf("rdat[%d] = %d", i, rdat[i])
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestPipeConcurrent(t *testing.T) {
t.Run("Write", func(t *testing.T) {
r, w := New(256).Pipe()

for i := 0; i < count; i++ {
for range count {
go func() {
time.Sleep(time.Millisecond) // Increase probability of race
if n, err := w.Write([]byte(input)); n != len(input) || err != nil {
Expand Down Expand Up @@ -383,7 +383,7 @@ func TestPipeConcurrent(t *testing.T) {
r, w := New(256).Pipe()

c := make(chan []byte, count*len(input)/readSize)
for i := 0; i < cap(c); i++ {
for range cap(c) {
go func() {
time.Sleep(time.Millisecond) // Increase probability of race
buf := make([]byte, readSize)
Expand All @@ -394,7 +394,7 @@ func TestPipeConcurrent(t *testing.T) {
}()
}

for i := 0; i < count; i++ {
for range count {
if n, err := w.Write([]byte(input)); n != len(input) || err != nil {
t.Errorf("Write() = (%d, %v); want (%d, nil)", n, err, len(input))
}
Expand All @@ -403,7 +403,7 @@ func TestPipeConcurrent(t *testing.T) {
// Since each read is independent, the only guarantee about the output
// is that it is a permutation of the input in readSized groups.
got := make([]byte, 0, count*len(input))
for i := 0; i < cap(c); i++ {
for range cap(c) {
got = append(got, (<-c)...)
}
got = sortBytesInGroups(got, readSize)
Expand All @@ -421,6 +421,6 @@ func sortBytesInGroups(b []byte, n int) []byte {
groups = append(groups, b[:n])
b = b[n:]
}
sort.Slice(groups, func(i, j int) bool { return bytes.Compare(groups[i], groups[j]) < 0 })
slices.SortFunc(groups, bytes.Compare)
return bytes.Join(groups, nil)
}
46 changes: 18 additions & 28 deletions ring_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,8 @@ func TestRingBuffer_Blocking(t *testing.T) {
// Reader
var readErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Go(func() {
readRng := rand.New(rand.NewSource(1))
defer wg.Done()
defer rb.CloseWithError(readErr)
buf := make([]byte, 1024)
for {
Expand Down Expand Up @@ -483,13 +481,13 @@ func TestRingBuffer_Blocking(t *testing.T) {
time.Sleep(time.Duration(readRng.Intn(maxSleep)))
}
}
}()
})

// Writer
{
buf := make([]byte, 1024)
writeRng := rand.New(rand.NewSource(2))
for i := 0; i < 2500; i++ {
for range 2500 {
writeRng.Read(buf)
// Write
n, err := rb.Write(buf[:writeRng.Intn(len(buf))])
Expand Down Expand Up @@ -606,9 +604,7 @@ func TestRingBuffer_BlockingBig(t *testing.T) {
// Reader
var readErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
defer rb.CloseWithError(readErr)
readRng := rand.New(rand.NewSource(1))
buf := make([]byte, 64<<10)
Expand Down Expand Up @@ -646,13 +642,13 @@ func TestRingBuffer_BlockingBig(t *testing.T) {
time.Sleep(time.Duration(readRng.Intn(maxSleep)))
}
}
}()
})

// Writer
{
writeRng := rand.New(rand.NewSource(2))
buf := make([]byte, 64<<10)
for i := 0; i < 500; i++ {
for range 500 {
writeRng.Read(buf)
// Write
n, err := rb.Write(buf[:writeRng.Intn(len(buf))])
Expand Down Expand Up @@ -768,9 +764,7 @@ func TestRingBuffer_ReadFromBig(t *testing.T) {
// Reader
var readErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
defer rb.CloseWithError(readErr)
readRng := rand.New(rand.NewSource(1))
buf := make([]byte, 64<<10)
Expand Down Expand Up @@ -808,13 +802,13 @@ func TestRingBuffer_ReadFromBig(t *testing.T) {
time.Sleep(time.Duration(readRng.Intn(maxSleep)))
}
}
}()
})

// Writer
{
writeRng := rand.New(rand.NewSource(2))
buf := make([]byte, 100<<10)
for i := 0; i < 500; i++ {
for range 500 {
writeRng.Read(buf)
// Write
wroteBytes += len(buf)
Expand Down Expand Up @@ -1743,12 +1737,10 @@ func TestRingBuffer_TryContention(t *testing.T) {
errors := make(chan error, numGoroutines*2)

// Reader goroutines
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range numGoroutines {
wg.Go(func() {
buf := make([]byte, 16)
for j := 0; j < opsPerGoroutine; j++ {
for range opsPerGoroutine {
n, err := rb.TryRead(buf)
if err != nil && err != ErrAcquireLock && err != ErrIsEmpty {
errors <- fmt.Errorf("TryRead error: %w", err)
Expand All @@ -1759,16 +1751,14 @@ func TestRingBuffer_TryContention(t *testing.T) {
_ = buf[:n]
}
}
}()
})
}

// Writer goroutines
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range numGoroutines {
wg.Go(func() {
data := []byte("test data content")
for j := 0; j < opsPerGoroutine; j++ {
for range opsPerGoroutine {
n, err := rb.TryWrite(data)
if err != nil && err != ErrAcquireLock && err != ErrIsFull && err != ErrTooMuchDataToWrite {
errors <- fmt.Errorf("TryWrite error: %w", err)
Expand All @@ -1779,7 +1769,7 @@ func TestRingBuffer_TryContention(t *testing.T) {
_ = n
}
}
}()
})
}

wg.Wait()
Expand Down Expand Up @@ -2115,7 +2105,7 @@ func TestRingBuffer_ResetInBlockingMode_Multiple(t *testing.T) {

// Start multiple blocked readers
done := make(chan result, 3)
for i := 0; i < 3; i++ {
for range 3 {
go func() {
buf := make([]byte, 10)
n, err := rb.Read(buf)
Expand Down
Loading