diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2014-04-10 15:46:07 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2014-04-10 15:46:07 -0700 |
| commit | 2dbc5d26c773e4400c0adfc25d9160eeaf6530b0 (patch) | |
| tree | 53be230ef35a8aabe78891219fdd03795d2affdd /src/pkg/strings | |
| parent | 1e68e6ae21ec4e88f7f59635831be74e39b26f7c (diff) | |
| download | go-2dbc5d26c773e4400c0adfc25d9160eeaf6530b0.tar.xz | |
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
Diffstat (limited to 'src/pkg/strings')
| -rw-r--r-- | src/pkg/strings/reader.go | 1 | ||||
| -rw-r--r-- | src/pkg/strings/reader_test.go | 17 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/pkg/strings/reader.go b/src/pkg/strings/reader.go index 93ff804ed1..c02d33bd61 100644 --- a/src/pkg/strings/reader.go +++ b/src/pkg/strings/reader.go @@ -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") } diff --git a/src/pkg/strings/reader_test.go b/src/pkg/strings/reader_test.go index c7a34123ac..5995f21038 100644 --- a/src/pkg/strings/reader_test.go +++ b/src/pkg/strings/reader_test.go @@ -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++ { |
