transmission/tests/libtransmission/peer-mgr-active-requests-te...

189 lines
6.4 KiB
C++
Raw Normal View History

// 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),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#define LIBTRANSMISSION_PEER_MODULE
#include <algorithm>
#include <type_traits>
#include "transmission.h"
#include "peer-mgr-active-requests.h"
#include "gtest/gtest.h"
class PeerMgrActiveRequestsTest : public ::testing::Test
{
protected:
tr_peer* peer_a_ = reinterpret_cast<tr_peer*>(0xCAFE);
tr_peer* peer_b_ = reinterpret_cast<tr_peer*>(0xDEAD);
tr_peer* peer_c_ = reinterpret_cast<tr_peer*>(0xBEEF);
};
// consider: making it a templated class so that tr_peer can be replaced with X
TEST_F(PeerMgrActiveRequestsTest, requestsAreNotAddedTwice)
{
auto requests = ActiveRequests{};
auto const block = tr_block_index_t{ 100 };
auto const peer = static_cast<tr_peer*>(nullptr);
auto const when = time_t{};
EXPECT_TRUE(requests.add(block, peer, when));
EXPECT_FALSE(requests.add(block, peer, when));
EXPECT_FALSE(requests.add(block, peer, when));
EXPECT_FALSE(requests.add(block, peer, when + 1));
}
TEST_F(PeerMgrActiveRequestsTest, requestsMadeAreCounted)
{
auto requests = ActiveRequests{};
auto const block = tr_block_index_t{ 100 };
auto const peer = static_cast<tr_peer*>(nullptr);
auto const when = time_t{};
EXPECT_EQ(0U, requests.count(block));
EXPECT_EQ(0U, requests.count(peer));
EXPECT_EQ(0U, requests.size());
EXPECT_TRUE(requests.add(block, peer, when));
EXPECT_EQ(1U, requests.count(block));
EXPECT_EQ(1U, requests.count(peer));
EXPECT_EQ(1U, requests.size());
}
TEST_F(PeerMgrActiveRequestsTest, requestsAreRemoved)
{
auto requests = ActiveRequests{};
auto const block = tr_block_index_t{ 100 };
auto const peer = static_cast<tr_peer*>(nullptr);
auto const when = time_t{};
EXPECT_TRUE(requests.add(block, peer, when));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(1U, requests.count(block));
EXPECT_EQ(1U, requests.count(peer));
EXPECT_EQ(1U, requests.size());
EXPECT_TRUE(requests.remove(block, peer));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, requests.count(block));
EXPECT_EQ(0U, requests.count(peer));
EXPECT_EQ(0U, requests.size());
EXPECT_FALSE(requests.remove(block, peer));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, requests.count(block));
EXPECT_EQ(0U, requests.count(peer));
EXPECT_EQ(0U, requests.size());
}
TEST_F(PeerMgrActiveRequestsTest, peersAreRemoved)
{
auto requests = ActiveRequests{};
auto const block = tr_block_index_t{ 100 };
auto const peer = static_cast<tr_peer*>(nullptr);
auto const when = time_t{};
// setup: add a request
EXPECT_TRUE(requests.add(block, peer, when));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(1U, requests.count(block));
EXPECT_EQ(1U, requests.count(peer));
EXPECT_EQ(1U, requests.size());
// try removing requests for that block (should remove the 1 active request)
auto const removed = requests.remove(block);
EXPECT_EQ(std::vector<tr_peer*>{ peer }, removed);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, requests.count(block));
EXPECT_EQ(0U, requests.count(peer));
EXPECT_EQ(0U, requests.size());
// try removing requests for that block again (should remove nothing)
EXPECT_EQ(std::vector<tr_peer*>{}, requests.remove(block));
}
TEST_F(PeerMgrActiveRequestsTest, multiplePeersAreRemoved)
{
// setup
auto requests = ActiveRequests{};
auto const block_a = tr_block_index_t{ 128 };
auto const when_a = 100;
EXPECT_TRUE(requests.add(block_a, peer_a_, when_a));
auto const block_b = block_a;
auto const when_b = 200;
EXPECT_TRUE(requests.add(block_b, peer_b_, when_b));
auto const block_c = tr_block_index_t{ 256 };
auto const when_c = when_b;
EXPECT_TRUE(requests.add(block_c, peer_c_, when_c));
EXPECT_EQ(block_a, block_b);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(2U, requests.count(block_a));
EXPECT_EQ(1U, requests.count(block_c));
EXPECT_EQ(3U, requests.size());
// now remove block_a, which was req'd by peer_a_ and peer_b_
auto expected = std::vector<tr_peer*>{ peer_a_, peer_b_ };
std::sort(std::begin(expected), std::end(expected));
auto removed = requests.remove(block_a);
std::sort(std::begin(removed), std::end(removed));
EXPECT_EQ(expected, removed);
}
TEST_F(PeerMgrActiveRequestsTest, multipleBlocksAreRemoved)
{
// setup
auto requests = ActiveRequests{};
auto const block_a1 = tr_block_index_t{ 128 };
auto const when_a1 = 300;
EXPECT_TRUE(requests.add(block_a1, peer_a_, when_a1));
auto const block_a2 = tr_block_index_t{ 256 };
auto const when_a2 = 400;
EXPECT_TRUE(requests.add(block_a2, peer_a_, when_a2));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(2U, requests.size());
EXPECT_EQ(2U, requests.count(peer_a_));
EXPECT_EQ(1U, requests.count(block_a1));
EXPECT_EQ(0U, requests.count(peer_b_));
EXPECT_EQ(0U, requests.count(tr_block_index_t{ 512 }));
// confirm that removing peer_a_ removes all of its requests
auto expected = std::vector<tr_block_index_t>{ block_a1, block_a2 };
std::sort(std::begin(expected), std::end(expected));
auto removed = requests.remove(peer_a_);
std::sort(std::begin(removed), std::end(removed));
EXPECT_EQ(expected, removed);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, requests.size());
EXPECT_EQ(0U, requests.count(peer_a_));
EXPECT_EQ(0U, requests.count(block_a1));
}
TEST_F(PeerMgrActiveRequestsTest, sentBefore)
{
// setup
auto requests = ActiveRequests{};
auto const block_a1 = tr_block_index_t{ 128 };
auto const when_a1 = 300;
EXPECT_TRUE(requests.add(block_a1, peer_a_, when_a1));
auto const block_a2 = tr_block_index_t{ 256 };
auto const when_a2 = 400;
EXPECT_TRUE(requests.add(block_a2, peer_a_, when_a2));
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(2U, requests.size());
EXPECT_EQ(2U, requests.count(peer_a_));
EXPECT_EQ(1U, requests.count(block_a1));
// test that the timestamps are counted correctly
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, std::size(requests.sentBefore(when_a1 - 1)));
EXPECT_EQ(0U, std::size(requests.sentBefore(when_a1)));
EXPECT_EQ(1U, std::size(requests.sentBefore(when_a1 + 1)));
EXPECT_EQ(1U, std::size(requests.sentBefore(when_a2 - 1)));
EXPECT_EQ(1U, std::size(requests.sentBefore(when_a2)));
EXPECT_EQ(2U, std::size(requests.sentBefore(when_a2 + 1)));
// test that the returned block + peer pairs are correct
auto items = requests.sentBefore(when_a1 + 1);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
ASSERT_EQ(1U, std::size(items));
EXPECT_EQ(block_a1, items[0].first);
EXPECT_EQ(peer_a_, items[0].second);
}