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:
parent
1e68e6ae21
commit
2dbc5d26c7
@ -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")
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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++ {
|
||||
|
Loading…
Reference in New Issue
Block a user