Commit 2dbc5d26 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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
parent 1e68e6ae
......@@ -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++ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment