10 "github.com/anacrolix/missinggo/pubsub"
11 "github.com/bradfitz/iter"
12 "github.com/stretchr/testify/require"
14 "github.com/anacrolix/torrent/metainfo"
15 pp "github.com/anacrolix/torrent/peer_protocol"
16 "github.com/anacrolix/torrent/storage"
19 // Ensure that no race exists between sending a bitfield, and a subsequent
20 // Have that would potentially alter it.
21 func TestSendBitfieldThenHave(t *testing.T) {
24 config: &ClientConfig{DownloadRateLimiter: unlimited},
27 c := cl.newConnection(nil, false)
28 c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
29 c.t.setInfo(&metainfo.Info{
30 Pieces: make([]byte, metainfo.HashSize*3),
34 go c.writer(time.Minute)
36 c.t.completedPieces.Add(1)
37 c.PostBitfield( /*[]bool{false, true, false}*/ )
43 n, err := io.ReadFull(r, b)
45 // This will cause connection.writer to terminate.
48 require.NoError(t, err)
49 require.EqualValues(t, 15, n)
50 // Here we see that the bitfield doesn't have piece 2 set, as that should
51 // arrive in the following Have message.
52 require.EqualValues(t, "\x00\x00\x00\x02\x05@\x00\x00\x00\x05\x04\x00\x00\x00\x02", string(b))
55 type torrentStorage struct {
59 func (me *torrentStorage) Close() error { return nil }
61 func (me *torrentStorage) Piece(mp metainfo.Piece) storage.PieceImpl {
65 func (me *torrentStorage) Completion() storage.Completion {
66 return storage.Completion{}
69 func (me *torrentStorage) MarkComplete() error {
73 func (me *torrentStorage) MarkNotComplete() error {
77 func (me *torrentStorage) ReadAt([]byte, int64) (int, error) {
78 panic("shouldn't be called")
81 func (me *torrentStorage) WriteAt(b []byte, _ int64) (int, error) {
82 if len(b) != defaultChunkSize {
89 func BenchmarkConnectionMainReadLoop(b *testing.B) {
91 config: &ClientConfig{
92 DownloadRateLimiter: unlimited,
95 ts := &torrentStorage{}
98 storage: &storage.Torrent{ts},
99 pieceStateChanges: pubsub.NewPubSub(),
101 require.NoError(b, t.setInfo(&metainfo.Info{
102 Pieces: make([]byte, 20),
104 PieceLength: 1 << 20,
106 t.setChunkSize(defaultChunkSize)
107 t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
109 cn := cl.newConnection(r, true)
111 mrlErr := make(chan error)
114 Piece: make([]byte, defaultChunkSize),
118 err := cn.mainReadLoop()
124 wb := msg.MustMarshalBinary()
125 b.SetBytes(int64(len(msg.Piece)))
129 for range iter.N(b.N) {
131 // The chunk must be written to storage everytime, to ensure the
132 // writeSem is unlocked.
133 t.pieces[0].dirtyChunks.Clear()
134 cn.validReceiveChunks = map[request]struct{}{newRequestFromMessage(&msg): struct{}{}}
136 n, err := w.Write(wb)
137 require.NoError(b, err)
138 require.EqualValues(b, len(wb), n)
142 require.NoError(b, <-mrlErr)
143 require.EqualValues(b, b.N, cn.stats.ChunksReadUseful.Int64())