2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2021-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <map>
|
2021-11-19 18:37:38 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#define LIBTRANSMISSION_PEER_MODULE
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/peer-mgr-wishlist.h>
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
class PeerMgrWishlistTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
2022-02-22 15:09:24 +00:00
|
|
|
struct MockMediator : public Wishlist::Mediator
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
mutable std::map<tr_block_index_t, size_t> active_request_count_;
|
|
|
|
mutable std::map<tr_piece_index_t, size_t> missing_block_count_;
|
2021-11-25 18:26:51 +00:00
|
|
|
mutable std::map<tr_piece_index_t, tr_block_span_t> block_span_;
|
2021-11-19 18:37:38 +00:00
|
|
|
mutable std::map<tr_piece_index_t, tr_priority_t> piece_priority_;
|
|
|
|
mutable std::set<tr_block_index_t> can_request_block_;
|
|
|
|
mutable std::set<tr_piece_index_t> can_request_piece_;
|
|
|
|
tr_piece_index_t piece_count_ = 0;
|
|
|
|
bool is_endgame_ = false;
|
2023-04-14 16:47:54 +00:00
|
|
|
bool is_sequential_download_ = false;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool clientCanRequestBlock(tr_block_index_t block) const final
|
|
|
|
{
|
|
|
|
return can_request_block_.count(block) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool clientCanRequestPiece(tr_piece_index_t piece) const final
|
|
|
|
{
|
|
|
|
return can_request_piece_.count(piece) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool isEndgame() const final
|
|
|
|
{
|
|
|
|
return is_endgame_;
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:47:54 +00:00
|
|
|
[[nodiscard]] bool isSequentialDownload() const final
|
|
|
|
{
|
|
|
|
return is_sequential_download_;
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:37:38 +00:00
|
|
|
[[nodiscard]] size_t countActiveRequests(tr_block_index_t block) const final
|
|
|
|
{
|
|
|
|
return active_request_count_[block];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t countMissingBlocks(tr_piece_index_t piece) const final
|
|
|
|
{
|
|
|
|
return missing_block_count_[piece];
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] tr_block_span_t blockSpan(tr_piece_index_t piece) const final
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return block_span_[piece];
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_piece_index_t countAllPieces() const final
|
|
|
|
{
|
|
|
|
return piece_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_priority_t priority(tr_piece_index_t piece) const final
|
|
|
|
{
|
|
|
|
return piece_priority_[piece];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, doesNotRequestPiecesThatCannotBeRequested)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, all missing
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.missing_block_count_[0] = 100;
|
|
|
|
mediator.missing_block_count_[1] = 100;
|
|
|
|
mediator.missing_block_count_[2] = 50;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 251 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// but we only want the first piece
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(0);
|
|
|
|
for (tr_block_index_t i = mediator.block_span_[0].begin; i < mediator.block_span_[0].end; ++i)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we should only get the first piece back
|
2023-03-22 15:24:10 +00:00
|
|
|
auto spans = Wishlist{ mediator }.next(1000);
|
2022-04-02 14:06:02 +00:00
|
|
|
ASSERT_EQ(1U, std::size(spans));
|
2022-02-22 15:09:24 +00:00
|
|
|
EXPECT_EQ(mediator.block_span_[0].begin, spans[0].begin);
|
|
|
|
EXPECT_EQ(mediator.block_span_[0].end, spans[0].end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, doesNotRequestBlocksThatCannotBeRequested)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, all missing
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.missing_block_count_[0] = 100;
|
|
|
|
mediator.missing_block_count_[1] = 100;
|
|
|
|
mediator.missing_block_count_[2] = 50;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 251 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// and we want all three pieces
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(0);
|
|
|
|
mediator.can_request_piece_.insert(1);
|
|
|
|
mediator.can_request_piece_.insert(2);
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// but we've already requested blocks [0..10) from someone else,
|
|
|
|
// so we don't want to send repeat requests
|
|
|
|
for (tr_block_index_t i = 10; i < 250; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// even if we ask wishlist for more blocks than exist,
|
|
|
|
// it should omit blocks 1-10 from the return set
|
2023-03-22 15:24:10 +00:00
|
|
|
auto spans = Wishlist{ mediator }.next(1000);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto requested = tr_bitfield(250);
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto const& span : spans)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
requested.setSpan(span.begin, span.end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(240U, requested.count());
|
|
|
|
EXPECT_EQ(0U, requested.count(0, 10));
|
|
|
|
EXPECT_EQ(240U, requested.count(10, 250));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, doesNotRequestTooManyBlocks)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, all missing
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.missing_block_count_[0] = 100;
|
|
|
|
mediator.missing_block_count_[1] = 100;
|
|
|
|
mediator.missing_block_count_[2] = 50;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 251 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// and we want everything
|
|
|
|
for (tr_piece_index_t i = 0; i < 3; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
for (tr_block_index_t i = 0; i < 250; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// but we only ask for 10 blocks,
|
|
|
|
// so that's how many we should get back
|
2022-04-02 14:06:02 +00:00
|
|
|
auto const n_wanted = 10U;
|
2023-03-22 15:24:10 +00:00
|
|
|
auto const spans = Wishlist{ mediator }.next(n_wanted);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto n_got = size_t{};
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto const& span : spans)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
n_got += span.end - span.begin;
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(n_wanted, n_got);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, prefersHighPriorityPieces)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, all missing
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.missing_block_count_[0] = 100;
|
|
|
|
mediator.missing_block_count_[1] = 100;
|
|
|
|
mediator.missing_block_count_[2] = 100;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 300 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// and we want everything
|
|
|
|
for (tr_piece_index_t i = 0; i < 3; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
for (tr_block_index_t i = 0; i < 299; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and the second piece is high priority
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_priority_[1] = TR_PRI_HIGH;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// wishlist should pick the high priority piece's blocks first.
|
|
|
|
//
|
|
|
|
// NB: when all other things are equal in the wishlist, pieces are
|
|
|
|
// picked at random so this test -could- pass even if there's a bug.
|
|
|
|
// So test several times to shake out any randomness
|
|
|
|
auto const num_runs = 1000;
|
|
|
|
for (int run = 0; run < num_runs; ++run)
|
|
|
|
{
|
2022-04-02 14:06:02 +00:00
|
|
|
auto const n_wanted = 10U;
|
2023-03-22 15:24:10 +00:00
|
|
|
auto spans = Wishlist{ mediator }.next(n_wanted);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto n_got = size_t{};
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto const& span : spans)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto block = span.begin; block < span.end; ++block)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
EXPECT_LE(mediator.block_span_[1].begin, block);
|
|
|
|
EXPECT_LT(block, mediator.block_span_[1].end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
n_got += span.end - span.begin;
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(n_wanted, n_got);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, onlyRequestsDupesDuringEndgame)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, all missing
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.missing_block_count_[0] = 100;
|
|
|
|
mediator.missing_block_count_[1] = 100;
|
|
|
|
mediator.missing_block_count_[2] = 100;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 300 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// and we want everything
|
|
|
|
for (tr_piece_index_t i = 0; i < 3; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
for (tr_block_index_t i = 0; i < 300; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and we've already requested blocks [0-150)
|
|
|
|
for (tr_block_index_t i = 0; i < 150; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.active_request_count_[i] = 1;
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// even if we ask wishlist to list more blocks than exist,
|
|
|
|
// those first 150 should be omitted from the return list
|
2023-03-22 15:24:10 +00:00
|
|
|
auto spans = Wishlist{ mediator }.next(1000);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto requested = tr_bitfield(300);
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto const& span : spans)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
requested.setSpan(span.begin, span.end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(150U, requested.count());
|
|
|
|
EXPECT_EQ(0U, requested.count(0, 150));
|
|
|
|
EXPECT_EQ(150U, requested.count(150, 300));
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// BUT during endgame it's OK to request dupes,
|
|
|
|
// so then we _should_ see the first 150 in the list
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.is_endgame_ = true;
|
2023-03-22 15:24:10 +00:00
|
|
|
spans = Wishlist{ mediator }.next(1000);
|
2021-11-19 18:37:38 +00:00
|
|
|
requested = tr_bitfield(300);
|
2021-11-25 18:26:51 +00:00
|
|
|
for (auto const& span : spans)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
requested.setSpan(span.begin, span.end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(300U, requested.count());
|
|
|
|
EXPECT_EQ(150U, requested.count(0, 150));
|
|
|
|
EXPECT_EQ(150U, requested.count(150, 300));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PeerMgrWishlistTest, prefersNearlyCompletePieces)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto mediator = MockMediator{};
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// setup: three pieces, same size
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.piece_count_ = 3;
|
|
|
|
mediator.block_span_[0] = { 0, 100 };
|
|
|
|
mediator.block_span_[1] = { 100, 200 };
|
|
|
|
mediator.block_span_[2] = { 200, 300 };
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
// and we want everything
|
|
|
|
for (tr_piece_index_t i = 0; i < 3; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_piece_.insert(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// but some pieces are closer to completion than others
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.missing_block_count_[0] = 10;
|
|
|
|
mediator.missing_block_count_[1] = 20;
|
|
|
|
mediator.missing_block_count_[2] = 100;
|
2021-11-19 18:37:38 +00:00
|
|
|
for (tr_piece_index_t piece = 0; piece < 3; ++piece)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
auto const& span = mediator.block_span_[piece];
|
|
|
|
auto const& n_missing = mediator.missing_block_count_[piece];
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n_missing; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
mediator.can_request_block_.insert(span.begin + i);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wishlist prefers to get pieces completed ASAP, so it
|
|
|
|
// should pick the ones with the fewest missing blocks first.
|
|
|
|
// NB: when all other things are equal in the wishlist, pieces are
|
|
|
|
// picked at random so this test -could- pass even if there's a bug.
|
|
|
|
// So test several times to shake out any randomness
|
|
|
|
auto const num_runs = 1000;
|
|
|
|
for (int run = 0; run < num_runs; ++run)
|
|
|
|
{
|
2023-03-22 15:24:10 +00:00
|
|
|
auto const ranges = Wishlist{ mediator }.next(10);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto requested = tr_bitfield(300);
|
|
|
|
for (auto const& range : ranges)
|
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
requested.setSpan(range.begin, range.end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(10U, requested.count());
|
|
|
|
EXPECT_EQ(10U, requested.count(0, 100));
|
|
|
|
EXPECT_EQ(0U, requested.count(100, 300));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same premise as previous test, but ask for more blocks.
|
|
|
|
// Since the second piece is also the second-closest to completion,
|
|
|
|
// those blocks should be next in line.
|
|
|
|
for (int run = 0; run < num_runs; ++run)
|
|
|
|
{
|
2023-03-22 15:24:10 +00:00
|
|
|
auto const ranges = Wishlist{ mediator }.next(20);
|
2021-11-19 18:37:38 +00:00
|
|
|
auto requested = tr_bitfield(300);
|
|
|
|
for (auto const& range : ranges)
|
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
requested.setSpan(range.begin, range.end);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(20U, requested.count());
|
|
|
|
EXPECT_EQ(10U, requested.count(0, 100));
|
|
|
|
EXPECT_EQ(10U, requested.count(100, 200));
|
|
|
|
EXPECT_EQ(0U, requested.count(200, 300));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|