]> Sergey Matveev's repositories - btrtrc.git/blobdiff - test/issue377_test.go
Drop support for go 1.20
[btrtrc.git] / test / issue377_test.go
index c723e51a7228ada4e19c3db28d6e6b4077482e38..5b0e659fee5709e0dfbacf9c01d07955a58f354b 100644 (file)
@@ -2,19 +2,20 @@ package test
 
 import (
        "errors"
+       "io"
        "os"
        "sync"
        "testing"
+       "testing/iotest"
 
        "github.com/anacrolix/log"
-       pp "github.com/anacrolix/torrent/peer_protocol"
-
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
 
        "github.com/anacrolix/torrent"
        "github.com/anacrolix/torrent/internal/testutil"
        "github.com/anacrolix/torrent/metainfo"
+       pp "github.com/anacrolix/torrent/peer_protocol"
        "github.com/anacrolix/torrent/storage"
 )
 
@@ -34,7 +35,7 @@ func TestReceiveChunkStorageFailure(t *testing.T) {
 func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
        seederDataDir, metainfo := testutil.GreetingTestTorrent()
        defer os.RemoveAll(seederDataDir)
-       seederClientConfig := torrent.TestingConfig()
+       seederClientConfig := torrent.TestingConfig(t)
        seederClientConfig.Debug = true
        justOneNetwork(seederClientConfig)
        seederClientStorage := storage.NewMMap(seederDataDir)
@@ -46,14 +47,17 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
        seederClient, err := torrent.NewClient(seederClientConfig)
        require.NoError(t, err)
        defer seederClient.Close()
-       defer testutil.ExportStatusWriter(seederClient, "s")()
-       leecherClientConfig := torrent.TestingConfig()
+       defer testutil.ExportStatusWriter(seederClient, "s", t)()
+       leecherClientConfig := torrent.TestingConfig(t)
        leecherClientConfig.Debug = true
+       // Don't require fast extension, whether the seeder will provide it or not (so we can test mixed
+       // cases).
+       leecherClientConfig.MinPeerExtensions.SetBit(pp.ExtensionBitFast, false)
        justOneNetwork(leecherClientConfig)
        leecherClient, err := torrent.NewClient(leecherClientConfig)
        require.NoError(t, err)
        defer leecherClient.Close()
-       defer testutil.ExportStatusWriter(leecherClient, "l")()
+       defer testutil.ExportStatusWriter(leecherClient, "l", t)()
        info, err := metainfo.UnmarshalInfo()
        require.NoError(t, err)
        leecherStorage := diskFullStorage{
@@ -73,9 +77,27 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
        // Tell the seeder to find the leecher. Is it guaranteed seeders will always try to do this?
        seederTorrent.AddClientPeer(leecherClient)
        <-leecherTorrent.GotInfo()
-       assertReadAllGreeting(t, leecherTorrent.NewReader())
+       r := leecherTorrent.Files()[0].NewReader()
+       defer r.Close()
+       // We can't use assertReadAllGreeting here, because the default storage write error handler
+       // disables data downloads, which now causes Readers to error when they're blocked.
+       if false {
+               assertReadAllGreeting(t, leecherTorrent.NewReader())
+       } else {
+               for func() 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)
+                       if err := iotest.TestReader(r, []byte(testutil.GreetingFileContents)); err != nil {
+                               t.Logf("got error while reading: %v", err)
+                               return true
+                       }
+                       return false
+               }() {
+               }
+       }
        // TODO: Check that PeerConns fastEnabled matches seederFast?
-       //select {}
+       // select {}
 }
 
 type pieceState struct {
@@ -99,12 +121,12 @@ func (me *diskFullStorage) Piece(p metainfo.Piece) storage.PieceImpl {
        }
 }
 
-func (me diskFullStorage) Close() error {
+func (me *diskFullStorage) Close() error {
        return nil
 }
 
-func (d diskFullStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {
-       return &d, nil
+func (d *diskFullStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {
+       return storage.TorrentImpl{Piece: d.Piece, Close: d.Close}, nil
 }
 
 type pieceImpl struct {