2014-05-28 23:27:48 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-05-07 16:56:44 +08:00
|
|
|
"io"
|
2016-09-11 13:43:57 +08:00
|
|
|
"sync"
|
2014-05-28 23:27:48 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-12-26 14:17:00 +08:00
|
|
|
|
2016-09-11 13:43:57 +08:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
|
|
|
"github.com/bradfitz/iter"
|
2016-03-22 09:07:03 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-05-07 16:56:44 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-11 13:43:57 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2016-11-22 11:20:48 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2016-09-11 13:43:57 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-05-28 23:27:48 +08:00
|
|
|
)
|
|
|
|
|
2016-05-11 21:50:21 +08:00
|
|
|
// Ensure that no race exists between sending a bitfield, and a subsequent
|
|
|
|
// Have that would potentially alter it.
|
|
|
|
func TestSendBitfieldThenHave(t *testing.T) {
|
|
|
|
r, w := io.Pipe()
|
2018-02-03 12:09:38 +08:00
|
|
|
var cl Client
|
|
|
|
cl.initLogger()
|
|
|
|
c := cl.newConnection(nil)
|
|
|
|
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
|
|
|
|
c.r = r
|
|
|
|
c.w = w
|
2016-05-11 21:50:21 +08:00
|
|
|
go c.writer(time.Minute)
|
|
|
|
c.mu().Lock()
|
|
|
|
c.Bitfield([]bool{false, true, false})
|
|
|
|
c.mu().Unlock()
|
|
|
|
c.mu().Lock()
|
|
|
|
c.Have(2)
|
|
|
|
c.mu().Unlock()
|
|
|
|
b := make([]byte, 15)
|
|
|
|
n, err := io.ReadFull(r, b)
|
|
|
|
c.mu().Lock()
|
|
|
|
// This will cause connection.writer to terminate.
|
|
|
|
c.closed.Set()
|
|
|
|
c.mu().Unlock()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 15, n)
|
|
|
|
// Here we see that the bitfield doesn't have piece 2 set, as that should
|
|
|
|
// arrive in the following Have message.
|
|
|
|
require.EqualValues(t, "\x00\x00\x00\x02\x05@\x00\x00\x00\x05\x04\x00\x00\x00\x02", string(b))
|
|
|
|
}
|
2016-09-11 13:43:57 +08:00
|
|
|
|
|
|
|
type torrentStorage struct {
|
|
|
|
writeSem sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *torrentStorage) Close() error { return nil }
|
|
|
|
|
|
|
|
func (me *torrentStorage) Piece(mp metainfo.Piece) storage.PieceImpl {
|
|
|
|
return me
|
|
|
|
}
|
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
func (me *torrentStorage) Completion() storage.Completion {
|
|
|
|
return storage.Completion{}
|
2016-09-11 13:43:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *torrentStorage) MarkComplete() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *torrentStorage) MarkNotComplete() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *torrentStorage) ReadAt([]byte, int64) (int, error) {
|
|
|
|
panic("shouldn't be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *torrentStorage) WriteAt(b []byte, _ int64) (int, error) {
|
|
|
|
if len(b) != defaultChunkSize {
|
|
|
|
panic(len(b))
|
|
|
|
}
|
|
|
|
me.writeSem.Unlock()
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkConnectionMainReadLoop(b *testing.B) {
|
|
|
|
cl := &Client{}
|
|
|
|
ts := &torrentStorage{}
|
|
|
|
t := &Torrent{
|
2018-01-25 14:10:37 +08:00
|
|
|
cl: cl,
|
2016-09-11 13:43:57 +08:00
|
|
|
storage: &storage.Torrent{ts},
|
|
|
|
pieceStateChanges: pubsub.NewPubSub(),
|
|
|
|
}
|
2018-01-25 14:10:37 +08:00
|
|
|
require.NoError(b, t.setInfo(&metainfo.Info{
|
|
|
|
Pieces: make([]byte, 20),
|
|
|
|
Length: 1 << 20,
|
|
|
|
PieceLength: 1 << 20,
|
|
|
|
}))
|
2016-10-05 12:57:00 +08:00
|
|
|
t.setChunkSize(defaultChunkSize)
|
2018-01-12 09:24:37 +08:00
|
|
|
t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
|
2016-09-11 13:43:57 +08:00
|
|
|
r, w := io.Pipe()
|
|
|
|
cn := &connection{
|
|
|
|
t: t,
|
2016-10-10 13:30:51 +08:00
|
|
|
r: r,
|
2016-09-11 13:43:57 +08:00
|
|
|
}
|
|
|
|
mrlErr := make(chan error)
|
|
|
|
cl.mu.Lock()
|
|
|
|
go func() {
|
|
|
|
err := cn.mainReadLoop()
|
|
|
|
if err != nil {
|
|
|
|
mrlErr <- err
|
|
|
|
}
|
|
|
|
close(mrlErr)
|
|
|
|
}()
|
2016-11-22 11:20:48 +08:00
|
|
|
msg := pp.Message{
|
|
|
|
Type: pp.Piece,
|
2016-09-11 13:43:57 +08:00
|
|
|
Piece: make([]byte, defaultChunkSize),
|
|
|
|
}
|
|
|
|
wb, err := msg.MarshalBinary()
|
|
|
|
require.NoError(b, err)
|
|
|
|
b.SetBytes(int64(len(msg.Piece)))
|
|
|
|
ts.writeSem.Lock()
|
|
|
|
for range iter.N(b.N) {
|
|
|
|
cl.mu.Lock()
|
2017-09-15 17:35:16 +08:00
|
|
|
t.pieces[0].dirtyChunks.Clear()
|
2016-09-11 13:43:57 +08:00
|
|
|
cl.mu.Unlock()
|
|
|
|
n, err := w.Write(wb)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.EqualValues(b, len(wb), n)
|
|
|
|
ts.writeSem.Lock()
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
require.NoError(b, <-mrlErr)
|
2018-02-02 16:04:56 +08:00
|
|
|
require.EqualValues(b, b.N, cn.stats.ChunksReadUseful)
|
2016-09-11 13:43:57 +08:00
|
|
|
}
|
2016-11-22 11:20:48 +08:00
|
|
|
|
|
|
|
func TestConnectionReceiveBadChunkIndex(t *testing.T) {
|
|
|
|
cn := connection{
|
|
|
|
t: &Torrent{},
|
|
|
|
}
|
|
|
|
require.False(t, cn.t.haveInfo())
|
2018-02-02 16:19:14 +08:00
|
|
|
assert.NotPanics(t, func() { cn.receiveChunk(&pp.Message{Type: pp.Piece}) })
|
2016-11-22 11:20:48 +08:00
|
|
|
cn.t.info = &metainfo.Info{}
|
|
|
|
require.True(t, cn.t.haveInfo())
|
2018-02-02 16:19:14 +08:00
|
|
|
assert.NotPanics(t, func() { cn.receiveChunk(&pp.Message{Type: pp.Piece}) })
|
2016-11-22 11:20:48 +08:00
|
|
|
}
|