fix: do not mark peer as not connectable when we are currently connected (#5889)

This commit is contained in:
tearfur 2023-08-15 05:46:09 +08:00 committed by GitHub
parent 916534a687
commit 449549c84f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 3 deletions

View File

@ -273,6 +273,7 @@ public:
auto* const peer_info = peer->peer_info;
auto const socket_address = peer->socket_address();
auto const listen_socket_address = peer_info->listen_socket_address();
auto const was_incoming = peer->is_incoming_connection();
TR_ASSERT(peer_info != nullptr);
@ -295,7 +296,7 @@ public:
TR_ASSERT(port_empty);
}
}
graveyard_pool.erase(socket_address);
graveyard_pool.erase(listen_socket_address);
}
void remove_all_peers()
@ -1072,7 +1073,7 @@ void create_bit_torrent_peer(tr_torrent* tor, std::shared_ptr<tr_peerIo> io, tr_
}
/* FIXME: this is kind of a mess. */
[[nodiscard]] bool on_handshake_done(tr_peerMgr* manager, tr_handshake::Result const& result)
[[nodiscard]] bool on_handshake_done(tr_peerMgr* const manager, tr_handshake::Result const& result)
{
TR_ASSERT(result.io != nullptr);
@ -1097,7 +1098,7 @@ void create_bit_torrent_peer(tr_torrent* tor, std::shared_ptr<tr_peerIo> io, tr_
{
if (s != nullptr)
{
if (auto* const info = s->get_existing_peer_info(socket_address); info != nullptr)
if (auto* const info = s->get_existing_peer_info(socket_address); info != nullptr && !info->is_connected())
{
info->on_connection_failed();

View File

@ -52,6 +52,7 @@ target_sources(libtransmission-test
torrent-magnet-test.cc
torrent-metainfo-test.cc
torrents-test.cc
tr-peer-info-test.cc
utils-test.cc
variant-test.cc
watchdir-test.cc

View File

@ -0,0 +1,73 @@
// This file Copyright © 2008-2023 Mnemosyne LLC.
// 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.
#include <array>
#include <optional>
#include <tuple>
#include <utility>
#include <libtransmission/net.h>
#include <libtransmission/peer-mgr.h>
#include "gtest/gtest.h"
using PeerInfoTest = ::testing::Test;
TEST_F(PeerInfoTest, mergeConnectable)
{
// Same as the truth table in tr_peer_info::merge()
static auto constexpr Tests = std::array{
std::pair{ std::tuple{ std::optional{ true }, true, std::optional{ true }, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, true, std::optional{ true }, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, true, std::optional{ false }, false }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional{ true }, true, std::optional<bool>{}, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, true, std::optional<bool>{}, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, false, std::optional{ true }, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, false, std::optional{ true }, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, false, std::optional{ false }, false }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional{ true }, false, std::optional<bool>{}, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ true }, false, std::optional<bool>{}, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional{ false }, false, std::optional{ true }, true }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional{ false }, false, std::optional{ true }, false }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional{ false }, false, std::optional{ false }, false }, std::optional{ false } },
std::pair{ std::tuple{ std::optional{ false }, false, std::optional<bool>{}, true }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional{ false }, false, std::optional<bool>{}, false }, std::optional{ false } },
std::pair{ std::tuple{ std::optional<bool>{}, true, std::optional{ true }, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional<bool>{}, true, std::optional{ true }, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional<bool>{}, true, std::optional{ false }, false }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional<bool>{}, true, std::optional<bool>{}, true }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional<bool>{}, true, std::optional<bool>{}, false }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional<bool>{}, false, std::optional{ true }, true }, std::optional{ true } },
std::pair{ std::tuple{ std::optional<bool>{}, false, std::optional{ true }, false }, std::optional{ true } },
std::pair{ std::tuple{ std::optional<bool>{}, false, std::optional{ false }, false }, std::optional{ false } },
std::pair{ std::tuple{ std::optional<bool>{}, false, std::optional<bool>{}, true }, std::optional<bool>{} },
std::pair{ std::tuple{ std::optional<bool>{}, false, std::optional<bool>{}, false }, std::optional<bool>{} },
};
static_assert(std::size(Tests) == 25U);
for (auto const& [condition, result] : Tests)
{
auto const& [this_connectable, this_connected, that_connectable, that_connected] = condition;
auto info_this = tr_peer_info{ tr_address{}, 0, TR_PEER_FROM_PEX };
auto info_that = tr_peer_info{ tr_address{}, 0, TR_PEER_FROM_PEX };
if (this_connectable)
{
info_this.set_connectable(*this_connectable);
}
info_this.set_connected(time_t{}, this_connected);
if (that_connectable)
{
info_that.set_connectable(*that_connectable);
}
info_that.set_connected(time_t{}, that_connected);
info_this.merge(info_that);
EXPECT_EQ(info_this.is_connectable(), result);
}
}