2014-05-28 15:27:48 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-05-07 08:56:44 +00:00
|
|
|
"io"
|
2018-06-11 02:20:51 +00:00
|
|
|
"net"
|
2016-09-11 05:43:57 +00:00
|
|
|
"sync"
|
2014-05-28 15:27:48 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-12-26 06:17:00 +00:00
|
|
|
|
2016-09-11 05:43:57 +00:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
2019-08-21 10:58:40 +00:00
|
|
|
"github.com/bradfitz/iter"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2016-09-11 05:43:57 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2016-11-22 03:20:48 +00:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2016-09-11 05:43:57 +00:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-05-28 15:27:48 +00:00
|
|
|
)
|
|
|
|
|
2016-05-11 13:50:21 +00:00
|
|
|
// Ensure that no race exists between sending a bitfield, and a subsequent
|
|
|
|
// Have that would potentially alter it.
|
|
|
|
func TestSendBitfieldThenHave(t *testing.T) {
|
2018-06-16 06:30:04 +00:00
|
|
|
cl := Client{
|
2019-10-03 09:09:55 +00:00
|
|
|
config: TestingConfig(),
|
2018-06-16 06:30:04 +00:00
|
|
|
}
|
2018-02-03 04:09:38 +00:00
|
|
|
cl.initLogger()
|
2018-11-15 23:35:30 +00:00
|
|
|
c := cl.newConnection(nil, false, IpPort{}, "")
|
2018-02-03 04:09:38 +00:00
|
|
|
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
|
2018-02-04 08:10:25 +00:00
|
|
|
c.t.setInfo(&metainfo.Info{
|
|
|
|
Pieces: make([]byte, metainfo.HashSize*3),
|
|
|
|
})
|
2019-10-03 09:09:55 +00:00
|
|
|
r, w := io.Pipe()
|
2018-02-03 04:09:38 +00:00
|
|
|
c.r = r
|
|
|
|
c.w = w
|
2016-05-11 13:50:21 +00:00
|
|
|
go c.writer(time.Minute)
|
|
|
|
c.mu().Lock()
|
2018-02-04 08:10:25 +00:00
|
|
|
c.t.completedPieces.Add(1)
|
|
|
|
c.PostBitfield( /*[]bool{false, true, false}*/ )
|
2016-05-11 13:50:21 +00:00
|
|
|
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 05:43:57 +00: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 05:09:32 +00:00
|
|
|
func (me *torrentStorage) Completion() storage.Completion {
|
|
|
|
return storage.Completion{}
|
2016-09-11 05:43:57 +00: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) {
|
2018-06-11 02:20:51 +00:00
|
|
|
cl := &Client{
|
2018-06-16 06:30:04 +00:00
|
|
|
config: &ClientConfig{
|
|
|
|
DownloadRateLimiter: unlimited,
|
|
|
|
},
|
2018-06-11 02:20:51 +00:00
|
|
|
}
|
2019-10-04 02:38:46 +00:00
|
|
|
cl.initLogger()
|
2016-09-11 05:43:57 +00:00
|
|
|
ts := &torrentStorage{}
|
|
|
|
t := &Torrent{
|
2018-01-25 06:10:37 +00:00
|
|
|
cl: cl,
|
2016-09-11 05:43:57 +00:00
|
|
|
storage: &storage.Torrent{ts},
|
|
|
|
pieceStateChanges: pubsub.NewPubSub(),
|
|
|
|
}
|
2018-01-25 06:10:37 +00:00
|
|
|
require.NoError(b, t.setInfo(&metainfo.Info{
|
|
|
|
Pieces: make([]byte, 20),
|
|
|
|
Length: 1 << 20,
|
|
|
|
PieceLength: 1 << 20,
|
|
|
|
}))
|
2016-10-05 04:57:00 +00:00
|
|
|
t.setChunkSize(defaultChunkSize)
|
2018-01-12 01:24:37 +00:00
|
|
|
t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
|
2018-06-11 02:20:51 +00:00
|
|
|
r, w := net.Pipe()
|
2018-11-15 23:35:30 +00:00
|
|
|
cn := cl.newConnection(r, true, IpPort{}, "")
|
2018-06-11 02:20:51 +00:00
|
|
|
cn.setTorrent(t)
|
2016-09-11 05:43:57 +00:00
|
|
|
mrlErr := make(chan error)
|
2018-06-25 04:09:08 +00:00
|
|
|
msg := pp.Message{
|
|
|
|
Type: pp.Piece,
|
|
|
|
Piece: make([]byte, defaultChunkSize),
|
|
|
|
}
|
2016-09-11 05:43:57 +00:00
|
|
|
go func() {
|
2018-07-25 03:41:50 +00:00
|
|
|
cl.lock()
|
2016-09-11 05:43:57 +00:00
|
|
|
err := cn.mainReadLoop()
|
|
|
|
if err != nil {
|
|
|
|
mrlErr <- err
|
|
|
|
}
|
|
|
|
close(mrlErr)
|
|
|
|
}()
|
2018-06-25 04:09:08 +00:00
|
|
|
wb := msg.MustMarshalBinary()
|
2016-09-11 05:43:57 +00:00
|
|
|
b.SetBytes(int64(len(msg.Piece)))
|
2018-06-25 04:09:08 +00:00
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2016-09-11 05:43:57 +00:00
|
|
|
ts.writeSem.Lock()
|
2018-06-25 04:09:08 +00:00
|
|
|
for range iter.N(b.N) {
|
2018-07-25 03:41:50 +00:00
|
|
|
cl.lock()
|
2018-06-25 04:09:08 +00:00
|
|
|
// The chunk must be written to storage everytime, to ensure the
|
|
|
|
// writeSem is unlocked.
|
|
|
|
t.pieces[0].dirtyChunks.Clear()
|
2019-08-19 02:15:54 +00:00
|
|
|
cn.validReceiveChunks = map[request]struct{}{newRequestFromMessage(&msg): {}}
|
2018-07-25 03:41:50 +00:00
|
|
|
cl.unlock()
|
2018-06-25 04:09:08 +00:00
|
|
|
n, err := w.Write(wb)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.EqualValues(b, len(wb), n)
|
|
|
|
ts.writeSem.Lock()
|
|
|
|
}
|
|
|
|
}()
|
2016-09-11 05:43:57 +00:00
|
|
|
require.NoError(b, <-mrlErr)
|
2018-06-12 12:47:46 +00:00
|
|
|
require.EqualValues(b, b.N, cn.stats.ChunksReadUseful.Int64())
|
2016-09-11 05:43:57 +00:00
|
|
|
}
|