1
0
mirror of https://github.com/golang/go synced 2024-11-23 13:40:04 -07:00

bytes, strings: add Reader.ReadAt race tests

Tests for the race detector to catch anybody
trying to mutate Reader in ReadAt.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/86700043
This commit is contained in:
Brad Fitzpatrick 2014-04-10 15:46:07 -07:00
parent 1e68e6ae21
commit 2dbc5d26c7
4 changed files with 36 additions and 0 deletions

View File

@ -43,6 +43,7 @@ func (r *Reader) Read(b []byte) (n int, err error) {
}
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
// cannot modify state - see io.ReaderAt
if off < 0 {
return 0, errors.New("bytes: invalid offset")
}

View File

@ -10,6 +10,7 @@ import (
"io"
"io/ioutil"
"os"
"sync"
"testing"
)
@ -98,6 +99,22 @@ func TestReaderAt(t *testing.T) {
}
}
func TestReaderAtConcurrent(t *testing.T) {
// Test for the race detector, to verify ReadAt doesn't mutate
// any state.
r := NewReader([]byte("0123456789"))
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
var buf [1]byte
r.ReadAt(buf[:], int64(i))
}(i)
}
wg.Wait()
}
func TestReaderWriteTo(t *testing.T) {
for i := 0; i < 30; i += 3 {
var l int

View File

@ -42,6 +42,7 @@ func (r *Reader) Read(b []byte) (n int, err error) {
}
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
// cannot modify state - see io.ReaderAt
if off < 0 {
return 0, errors.New("strings: invalid offset")
}

View File

@ -10,6 +10,7 @@ import (
"io"
"os"
"strings"
"sync"
"testing"
)
@ -98,6 +99,22 @@ func TestReaderAt(t *testing.T) {
}
}
func TestReaderAtConcurrent(t *testing.T) {
// Test for the race detector, to verify ReadAt doesn't mutate
// any state.
r := strings.NewReader("0123456789")
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
var buf [1]byte
r.ReadAt(buf[:], int64(i))
}(i)
}
wg.Wait()
}
func TestWriteTo(t *testing.T) {
const str = "0123456789"
for i := 0; i <= len(str); i++ {