Use iotest.TestReader

This commit is contained in:
Matt Joiner 2021-01-29 23:32:01 +11:00
parent 887db4a276
commit f5915ad12c
6 changed files with 16 additions and 20 deletions

View File

@ -5,6 +5,7 @@ import (
"math/rand"
"strings"
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/storage"
)
@ -48,7 +49,7 @@ func (p badStoragePiece) MarkNotComplete() error {
}
func (p badStoragePiece) randomlyTruncatedDataString() string {
return "hello, world\n"[:rand.Intn(14)]
return testutil.GreetingFileContents[:rand.Intn(14)]
}
func (p badStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {

View File

@ -9,9 +9,11 @@ import (
"path/filepath"
"reflect"
"testing"
"testing/iotest"
"time"
"github.com/bradfitz/iter"
"github.com/frankban/quicktest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -202,9 +204,7 @@ func TestCompletedPieceWrongSize(t *testing.T) {
assert.True(t, new)
r := tt.NewReader()
defer r.Close()
b, err = ioutil.ReadAll(r)
assert.Len(t, b, 13)
assert.NoError(t, err)
quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
}
func BenchmarkAddLargeTorrent(b *testing.B) {
@ -411,9 +411,7 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf
assert.Equal(t, alreadyCompleted, psrs[0].Complete)
if alreadyCompleted {
r := tt.NewReader()
b, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.EqualValues(t, testutil.GreetingFileContents, b)
quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
}
}

View File

@ -144,7 +144,7 @@ func (r *reader) ReadContext(ctx context.Context, b []byte) (n int, err error) {
defer r.opMu.Unlock()
n, err = r.readOnceAt(b, r.pos, &ctxErr)
if n == 0 {
if err == nil {
if err == nil && len(b) > 0 {
panic("expected error")
} else {
return
@ -185,7 +185,7 @@ func (r *reader) waitAvailable(pos, wanted int64, ctxErr *error, wait bool) (ava
err = errors.New("downloading disabled and data not already available")
return
}
if !wait {
if !wait || wanted == 0 {
return
}
r.waitReadable(pos)

View File

@ -3,10 +3,10 @@ package test
import (
"errors"
"io"
"io/ioutil"
"os"
"sync"
"testing"
"testing/iotest"
"github.com/anacrolix/log"
pp "github.com/anacrolix/torrent/peer_protocol"
@ -86,12 +86,10 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
// We don't seem to need to seek, but that's probably just because the storage failure is
// happening on the first read.
r.Seek(0, io.SeekStart)
output, err := ioutil.ReadAll(r)
if err != nil {
if err := iotest.TestReader(r, []byte(testutil.GreetingFileContents)); err != nil {
t.Logf("got error while reading: %v", err)
return true
}
assert.EqualValues(t, testutil.GreetingFileContents, output)
return false
}() {
}

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"sync"
"testing"
"testing/iotest"
"time"
"github.com/anacrolix/missinggo/v2/filecache"
@ -15,6 +16,7 @@ import (
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/storage"
sqliteStorage "github.com/anacrolix/torrent/storage/sqlite"
"github.com/frankban/quicktest"
"golang.org/x/time/rate"
"github.com/stretchr/testify/assert"
@ -39,9 +41,7 @@ func assertReadAllGreeting(t *testing.T, r io.ReadSeeker) {
pos, err := r.Seek(0, io.SeekStart)
assert.NoError(t, err)
assert.EqualValues(t, 0, pos)
_greeting, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.EqualValues(t, testutil.GreetingFileContents, string(_greeting))
quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
}
// Creates a seeder and a leecher, and ensures the data transfers when a read
@ -413,9 +413,7 @@ func TestSeedAfterDownloading(t *testing.T) {
defer wg.Done()
r := llg.NewReader()
defer r.Close()
b, err := ioutil.ReadAll(r)
require.NoError(t, err)
assert.EqualValues(t, testutil.GreetingFileContents, b)
quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil)
}()
done := make(chan struct{})
defer close(done)

View File

@ -2,6 +2,7 @@ package torrent
import (
"fmt"
"io"
"net"
"os"
"path/filepath"
@ -92,7 +93,7 @@ func BenchmarkUpdatePiecePriorities(b *testing.B) {
for range iter.N(7) {
r := t.NewReader()
r.SetReadahead(32 << 20)
r.Seek(3500000, 0)
r.Seek(3500000, io.SeekStart)
}
assert.Len(b, t.readers, 7)
for i := 0; i < int(t.numPieces()); i += 3 {