2020-02-21 11:56:21 +08:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2020-02-22 16:40:50 +08:00
|
|
|
"sync"
|
2020-02-21 11:56:21 +08:00
|
|
|
"testing"
|
|
|
|
|
2020-04-23 09:30:19 +08:00
|
|
|
"github.com/anacrolix/log"
|
2020-04-23 11:03:35 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2020-04-23 09:30:19 +08:00
|
|
|
|
2020-02-21 11:56:21 +08:00
|
|
|
"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"
|
|
|
|
"github.com/anacrolix/torrent/storage"
|
|
|
|
)
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func justOneNetwork(cc *torrent.ClientConfig) {
|
|
|
|
cc.DisableTCP = true
|
|
|
|
cc.DisableIPv4 = true
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:03:35 +08:00
|
|
|
func TestReceiveChunkStorageFailureSeederFastExtensionDisabled(t *testing.T) {
|
|
|
|
testReceiveChunkStorageFailure(t, false)
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:56:21 +08:00
|
|
|
func TestReceiveChunkStorageFailure(t *testing.T) {
|
2020-04-23 11:03:35 +08:00
|
|
|
testReceiveChunkStorageFailure(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
|
2020-02-21 11:56:21 +08:00
|
|
|
seederDataDir, metainfo := testutil.GreetingTestTorrent()
|
|
|
|
defer os.RemoveAll(seederDataDir)
|
|
|
|
seederClientConfig := torrent.TestingConfig()
|
|
|
|
seederClientConfig.Debug = true
|
2020-02-22 16:40:50 +08:00
|
|
|
justOneNetwork(seederClientConfig)
|
2020-02-21 11:56:21 +08:00
|
|
|
seederClientStorage := storage.NewMMap(seederDataDir)
|
|
|
|
defer seederClientStorage.Close()
|
|
|
|
seederClientConfig.DefaultStorage = seederClientStorage
|
|
|
|
seederClientConfig.Seed = true
|
|
|
|
seederClientConfig.Debug = true
|
2020-04-23 11:03:35 +08:00
|
|
|
seederClientConfig.Extensions.SetBit(pp.ExtensionBitFast, seederFast)
|
2020-02-21 11:56:21 +08:00
|
|
|
seederClient, err := torrent.NewClient(seederClientConfig)
|
|
|
|
require.NoError(t, err)
|
2020-04-23 10:28:48 +08:00
|
|
|
defer seederClient.Close()
|
2020-02-21 11:56:21 +08:00
|
|
|
defer testutil.ExportStatusWriter(seederClient, "s")()
|
|
|
|
leecherClientConfig := torrent.TestingConfig()
|
|
|
|
leecherClientConfig.Debug = true
|
2020-02-22 16:40:50 +08:00
|
|
|
justOneNetwork(leecherClientConfig)
|
2020-02-21 11:56:21 +08:00
|
|
|
leecherClient, err := torrent.NewClient(leecherClientConfig)
|
|
|
|
require.NoError(t, err)
|
2020-04-23 10:28:48 +08:00
|
|
|
defer leecherClient.Close()
|
2020-02-21 11:56:21 +08:00
|
|
|
defer testutil.ExportStatusWriter(leecherClient, "l")()
|
2020-02-22 16:40:50 +08:00
|
|
|
info, err := metainfo.UnmarshalInfo()
|
|
|
|
require.NoError(t, err)
|
|
|
|
leecherStorage := diskFullStorage{
|
|
|
|
pieces: make([]pieceState, info.NumPieces()),
|
|
|
|
data: make([]byte, info.TotalLength()),
|
|
|
|
}
|
|
|
|
defer leecherStorage.Close()
|
2020-02-21 11:56:21 +08:00
|
|
|
leecherTorrent, new, err := leecherClient.AddTorrentSpec(&torrent.TorrentSpec{
|
|
|
|
InfoHash: metainfo.HashInfoBytes(),
|
2020-02-22 16:40:50 +08:00
|
|
|
Storage: &leecherStorage,
|
2020-02-21 11:56:21 +08:00
|
|
|
})
|
2020-02-22 16:40:50 +08:00
|
|
|
leecherStorage.t = leecherTorrent
|
2020-02-21 11:56:21 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, new)
|
|
|
|
seederTorrent, err := seederClient.AddTorrent(metainfo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// 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())
|
2020-04-23 11:03:35 +08:00
|
|
|
// TODO: Check that PeerConns fastEnabled matches seederFast?
|
|
|
|
//select {}
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
type pieceState struct {
|
|
|
|
complete bool
|
|
|
|
}
|
2020-02-21 11:56:21 +08:00
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
type diskFullStorage struct {
|
|
|
|
pieces []pieceState
|
|
|
|
t *torrent.Torrent
|
|
|
|
defaultHandledWriteChunkError bool
|
|
|
|
data []byte
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
diskNotFull bool
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (me *diskFullStorage) Piece(p metainfo.Piece) storage.PieceImpl {
|
|
|
|
return pieceImpl{
|
|
|
|
mip: p,
|
|
|
|
diskFullStorage: me,
|
|
|
|
}
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (me diskFullStorage) Close() error {
|
|
|
|
return nil
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (d diskFullStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {
|
|
|
|
return &d, nil
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
type pieceImpl struct {
|
|
|
|
mip metainfo.Piece
|
|
|
|
*diskFullStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me pieceImpl) state() *pieceState {
|
|
|
|
return &me.diskFullStorage.pieces[me.mip.Index()]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me pieceImpl) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
off += me.mip.Offset()
|
|
|
|
return copy(p, me.data[off:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me pieceImpl) WriteAt(p []byte, off int64) (int, error) {
|
|
|
|
off += me.mip.Offset()
|
|
|
|
if !me.defaultHandledWriteChunkError {
|
|
|
|
go func() {
|
|
|
|
me.t.SetOnWriteChunkError(func(err error) {
|
|
|
|
log.Printf("got write chunk error to custom handler: %v", err)
|
|
|
|
me.mu.Lock()
|
|
|
|
me.diskNotFull = true
|
|
|
|
me.mu.Unlock()
|
|
|
|
me.t.AllowDataDownload()
|
|
|
|
})
|
|
|
|
me.t.AllowDataDownload()
|
|
|
|
}()
|
|
|
|
me.defaultHandledWriteChunkError = true
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
2020-02-22 16:40:50 +08:00
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
|
|
|
if me.diskNotFull {
|
|
|
|
return copy(me.data[off:], p), nil
|
|
|
|
}
|
|
|
|
return copy(me.data[off:], p[:1]), errors.New("disk full")
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (me pieceImpl) MarkComplete() error {
|
|
|
|
me.state().complete = true
|
|
|
|
return nil
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (me pieceImpl) MarkNotComplete() error {
|
2020-02-21 11:56:21 +08:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-02-22 16:40:50 +08:00
|
|
|
func (me pieceImpl) Completion() storage.Completion {
|
|
|
|
return storage.Completion{
|
|
|
|
Complete: me.state().complete,
|
|
|
|
Ok: true,
|
|
|
|
}
|
2020-02-21 11:56:21 +08:00
|
|
|
}
|