From ffb98097fa5117a987980062b2acf958e871a0b8 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 15 Dec 2021 19:09:22 +1100 Subject: [PATCH] Fix BenchmarkUpdatePiecePriorities --- torrent-piece-request-order.go | 12 ++++++++++++ torrent.go | 3 +++ torrent_test.go | 1 + 3 files changed, 16 insertions(+) diff --git a/torrent-piece-request-order.go b/torrent-piece-request-order.go index 7b1ef978..86c98398 100644 --- a/torrent-piece-request-order.go +++ b/torrent-piece-request-order.go @@ -5,6 +5,9 @@ import ( ) func (t *Torrent) updatePieceRequestOrder(pieceIndex int) { + if t.storage == nil { + return + } t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Update( t.pieceRequestOrderKey(pieceIndex), t.requestStrategyPieceOrderState(pieceIndex)) @@ -18,6 +21,9 @@ func (t *Torrent) clientPieceRequestOrderKey() interface{} { } func (t *Torrent) deletePieceRequestOrder() { + if t.storage == nil { + return + } cpro := t.cl.pieceRequestOrder key := t.clientPieceRequestOrderKey() pro := cpro[key] @@ -30,6 +36,9 @@ func (t *Torrent) deletePieceRequestOrder() { } func (t *Torrent) initPieceRequestOrder() { + if t.storage == nil { + return + } if t.cl.pieceRequestOrder == nil { t.cl.pieceRequestOrder = make(map[interface{}]*request_strategy.PieceRequestOrder) } @@ -41,6 +50,9 @@ func (t *Torrent) initPieceRequestOrder() { } func (t *Torrent) addRequestOrderPiece(i int) { + if t.storage == nil { + return + } t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Add( t.pieceRequestOrderKey(i), t.requestStrategyPieceOrderState(i)) diff --git a/torrent.go b/torrent.go index 5c8dca25..9db3a794 100644 --- a/torrent.go +++ b/torrent.go @@ -243,6 +243,9 @@ func (t *Torrent) pieceComplete(piece pieceIndex) bool { } func (t *Torrent) pieceCompleteUncached(piece pieceIndex) storage.Completion { + if t.storage == nil { + return storage.Completion{Complete: false, Ok: true} + } return t.pieces[piece].Storage().Completion() } diff --git a/torrent_test.go b/torrent_test.go index 2b96f191..38ea3d86 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -89,6 +89,7 @@ func BenchmarkUpdatePiecePriorities(b *testing.B) { PieceLength: pieceLength, Length: pieceLength * numPieces, })) + t.onSetInfo() assert.EqualValues(b, 13410, t.numPieces()) for i := 0; i < 7; i += 1 { r := t.NewReader()