2017-09-12 19:12:56 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-09-30 09:52:50 +08:00
|
|
|
"bytes"
|
2017-09-12 19:12:56 +08:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-30 09:52:50 +08:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-07-12 07:54:06 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-09-12 19:12:56 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkDecodePieces(t *testing.B) {
|
2017-09-12 19:32:01 +08:00
|
|
|
const pieceLen = 1 << 14
|
2021-09-30 09:52:50 +08:00
|
|
|
inputMsg := Message{
|
2017-09-12 19:12:56 +08:00
|
|
|
Type: Piece,
|
|
|
|
Index: 0,
|
|
|
|
Begin: 1,
|
2017-09-12 19:32:01 +08:00
|
|
|
Piece: make([]byte, pieceLen),
|
2017-09-12 19:12:56 +08:00
|
|
|
}
|
2021-09-30 09:52:50 +08:00
|
|
|
b := inputMsg.MustMarshalBinary()
|
2017-09-12 19:12:56 +08:00
|
|
|
t.SetBytes(int64(len(b)))
|
2021-09-30 09:52:50 +08:00
|
|
|
var r bytes.Reader
|
|
|
|
// Try to somewhat emulate what torrent.Client would do. But the goal is to get decoding as fast
|
|
|
|
// as possible and let consumers apply their own adjustments.
|
2017-09-12 19:12:56 +08:00
|
|
|
d := Decoder{
|
2021-09-30 09:52:50 +08:00
|
|
|
R: bufio.NewReaderSize(&r, 1<<10),
|
2017-09-12 19:12:56 +08:00
|
|
|
MaxLength: 1 << 18,
|
|
|
|
Pool: &sync.Pool{
|
|
|
|
New: func() interface{} {
|
2018-01-25 14:43:45 +08:00
|
|
|
b := make([]byte, pieceLen)
|
|
|
|
return &b
|
2017-09-12 19:12:56 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-09-30 09:52:50 +08:00
|
|
|
c := qt.New(t)
|
|
|
|
t.ReportAllocs()
|
|
|
|
t.ResetTimer()
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < t.N; i += 1 {
|
2021-09-30 09:52:50 +08:00
|
|
|
r.Reset(b)
|
2017-09-12 19:12:56 +08:00
|
|
|
var msg Message
|
2021-09-30 09:52:50 +08:00
|
|
|
err := d.Decode(&msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// This is very expensive, and should be discovered in tests rather than a benchmark.
|
|
|
|
if false {
|
|
|
|
c.Assert(msg, qt.DeepEquals, inputMsg)
|
|
|
|
}
|
2017-09-12 19:32:01 +08:00
|
|
|
// WWJD
|
2017-11-07 13:11:59 +08:00
|
|
|
d.Pool.Put(&msg.Piece)
|
2017-09-12 19:12:56 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 07:54:06 +08:00
|
|
|
|
|
|
|
func TestDecodeShortPieceEOF(t *testing.T) {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
w.Write(Message{Type: Piece, Piece: make([]byte, 1)}.MustMarshalBinary())
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
d := Decoder{
|
|
|
|
R: bufio.NewReader(r),
|
|
|
|
MaxLength: 1 << 15,
|
|
|
|
Pool: &sync.Pool{New: func() interface{} {
|
|
|
|
b := make([]byte, 2)
|
|
|
|
return &b
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
var m Message
|
|
|
|
require.NoError(t, d.Decode(&m))
|
|
|
|
assert.Len(t, m.Piece, 1)
|
2021-09-30 07:59:18 +08:00
|
|
|
assert.ErrorIs(t, d.Decode(&m), io.EOF)
|
2018-07-12 07:54:06 +08:00
|
|
|
}
|
2018-07-14 09:37:56 +08:00
|
|
|
|
|
|
|
func TestDecodeOverlongPiece(t *testing.T) {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
w.Write(Message{Type: Piece, Piece: make([]byte, 3)}.MustMarshalBinary())
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
d := Decoder{
|
|
|
|
R: bufio.NewReader(r),
|
|
|
|
MaxLength: 1 << 15,
|
|
|
|
Pool: &sync.Pool{New: func() interface{} {
|
|
|
|
b := make([]byte, 2)
|
|
|
|
return &b
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
var m Message
|
2018-07-14 09:50:43 +08:00
|
|
|
require.Error(t, d.Decode(&m))
|
2018-07-14 09:37:56 +08:00
|
|
|
}
|