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
diff --git a/src/pkg/bytes/reader.go b/src/pkg/bytes/reader.go
index cdc3233..61845e3 100644
--- a/src/pkg/bytes/reader.go
+++ b/src/pkg/bytes/reader.go
@@ -43,6 +43,7 @@
}
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")
}
diff --git a/src/pkg/bytes/reader_test.go b/src/pkg/bytes/reader_test.go
index a25f8ff..7abaee7 100644
--- a/src/pkg/bytes/reader_test.go
+++ b/src/pkg/bytes/reader_test.go
@@ -10,6 +10,7 @@
"io"
"io/ioutil"
"os"
+ "sync"
"testing"
)
@@ -98,6 +99,22 @@
}
}
+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
diff --git a/src/pkg/strings/reader.go b/src/pkg/strings/reader.go
index 93ff804..c02d33b 100644
--- a/src/pkg/strings/reader.go
+++ b/src/pkg/strings/reader.go
@@ -42,6 +42,7 @@
}
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")
}
diff --git a/src/pkg/strings/reader_test.go b/src/pkg/strings/reader_test.go
index c7a3412..5995f21 100644
--- a/src/pkg/strings/reader_test.go
+++ b/src/pkg/strings/reader_test.go
@@ -10,6 +10,7 @@
"io"
"os"
"strings"
+ "sync"
"testing"
)
@@ -98,6 +99,22 @@
}
}
+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++ {