Skip to content

Commit

Permalink
RIPD-1848 Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Mar 11, 2024
1 parent e6c0b03 commit 25cdc6c
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/test/peerfinder/PeerFinder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <ripple/peerfinder/impl/Logic.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/SecretKey.h>
#include "ripple/beast/unit_test/suite.hpp"
#include <test/unit_test/SuiteJournal.h>

namespace ripple {
Expand Down Expand Up @@ -157,6 +158,108 @@ class PeerFinder_test : public beast::unit_test::suite
BEAST_EXPECT(n <= (seconds + 59) / 60);
}

void
test_duplicateOutIn()
{
auto const seconds = 10000;
testcase("duplicate out/in");
TestStore store;
TestChecker checker;
TestStopwatch clock;
Logic<TestChecker> logic(clock, store, checker, journal_);
logic.addFixedPeer(
"test", beast::IP::Endpoint::from_string("65.0.0.1:5"));
{
Config c;
c.autoConnect = false;
c.listeningPort = 1024;
c.ipLimit = 2;
logic.config(c);
}

PublicKey const pk(randomKeyPair(KeyType::secp256k1).first);
std::size_t n = 0;

for (std::size_t i = 0; i < seconds; ++i)
{
auto const list = logic.autoconnect();
if (!list.empty())
{
BEAST_EXPECT(list.size() == 1);
auto const remote = list.front();
auto const slot1 = logic.new_outbound_slot(remote);
if (BEAST_EXPECT(slot1 != nullptr))
{
BEAST_EXPECT(
logic.connectedAddresses_.count(remote.address()) == 1);
auto const local =
beast::IP::Endpoint::from_string("65.0.0.2:1024");
auto const slot2 = logic.new_inbound_slot(local, remote);
BEAST_EXPECT(
logic.connectedAddresses_.count(remote.address()) == 1);
if (!BEAST_EXPECT(slot2 == nullptr))
logic.on_closed(slot2);
logic.on_closed(slot1);
}
++n;
}
clock.advance(std::chrono::seconds(1));
logic.once_per_second();
}
BEAST_EXPECT(n <= (seconds + 59) / 60);
}

void
test_duplicateInOut()
{
auto const seconds = 10000;
testcase("duplicate in/out");
TestStore store;
TestChecker checker;
TestStopwatch clock;
Logic<TestChecker> logic(clock, store, checker, journal_);
logic.addFixedPeer(
"test", beast::IP::Endpoint::from_string("65.0.0.1:5"));
{
Config c;
c.autoConnect = false;
c.listeningPort = 1024;
c.ipLimit = 2;
logic.config(c);
}

PublicKey const pk(randomKeyPair(KeyType::secp256k1).first);
std::size_t n = 0;

for (std::size_t i = 0; i < seconds; ++i)
{
auto const list = logic.autoconnect();
if (!list.empty())
{
BEAST_EXPECT(list.size() == 1);
auto const remote = list.front();
auto const local =
beast::IP::Endpoint::from_string("65.0.0.2:1024");
auto const slot1 = logic.new_inbound_slot(local, remote);
if (BEAST_EXPECT(slot1 != nullptr))
{
BEAST_EXPECT(
logic.connectedAddresses_.count(remote.address()) == 1);
auto const slot2 = logic.new_outbound_slot(remote);
BEAST_EXPECT(
logic.connectedAddresses_.count(remote.address()) == 1);
if (!BEAST_EXPECT(slot2 == nullptr))
logic.on_closed(slot2);
logic.on_closed(slot1);
}
++n;
}
clock.advance(std::chrono::seconds(1));
logic.once_per_second();
}
BEAST_EXPECT(n <= (seconds + 59) / 60);
}

void
test_config()
{
Expand Down Expand Up @@ -279,6 +382,8 @@ class PeerFinder_test : public beast::unit_test::suite
{
test_backoff1();
test_backoff2();
test_duplicateOutIn();
test_duplicateInOut();
test_config();
test_invalid_config();
}
Expand Down

0 comments on commit 25cdc6c

Please sign in to comment.