Remove hardcoded ports in several unit tests:

Fixes: RIPD-1522
This commit is contained in:
Mike Ellery
2017-08-25 10:26:53 -07:00
committed by seelabs
parent e8d02c1333
commit 50b35e2090
4 changed files with 41 additions and 38 deletions

View File

@@ -106,6 +106,11 @@ public:
*/
void close();
endpoint_type get_endpoint() const
{
return acceptor_.local_endpoint();
}
private:
template <class ConstBufferSequence>
void create (bool ssl, ConstBufferSequence const& buffers,

View File

@@ -33,6 +33,8 @@
namespace ripple {
using Endpoints = std::vector<boost::asio::ip::tcp::endpoint>;
/** A multi-protocol server.
This server maintains multiple configured listening ports,
@@ -58,7 +60,7 @@ public:
This may only be called once.
*/
virtual
void
Endpoints
ports (std::vector<Port> const& v) = 0;
/** Close the server.
@@ -85,8 +87,6 @@ private:
historySize = 100
};
using Doors = std::vector <std::shared_ptr<Door<Handler>>>;
Handler& handler_;
beast::Journal j_;
boost::asio::io_service& io_service_;
@@ -113,7 +113,7 @@ public:
return j_;
}
void
Endpoints
ports (std::vector<Port> const& ports) override;
void
@@ -163,13 +163,15 @@ ServerImpl<Handler>::
}
template<class Handler>
void
Endpoints
ServerImpl<Handler>::
ports (std::vector<Port> const& ports)
{
if (closed())
Throw<std::logic_error> ("ports() on closed Server");
ports_.reserve(ports.size());
Endpoints eps;
eps.reserve(ports.size());
for(auto const& port : ports)
{
ports_.push_back(port);
@@ -177,9 +179,11 @@ ports (std::vector<Port> const& ports)
io_service_, ports_.back(), j_))
{
list_.push_back(sp);
eps.push_back(sp->get_endpoint());
sp->run();
}
}
return eps;
}
template<class Handler>

View File

@@ -433,12 +433,12 @@ private:
}
void
run()
run(endpoint_type const& ep)
{
timer_.expires_from_now(std::chrono::seconds(3));
timer_.async_wait(strand_.wrap(std::bind(&Connection::on_timer,
shared_from_this(), std::placeholders::_1)));
socket_.async_connect(ep_, strand_.wrap(std::bind(
socket_.async_connect(ep, strand_.wrap(std::bind(
&Connection::on_connect, shared_from_this(),
std::placeholders::_1)));
}
@@ -542,7 +542,7 @@ private:
{
auto const p = std::make_shared<Connection>(*this, ep);
add(p);
p->run();
p->run(ep);
}
~Client()

View File

@@ -39,10 +39,6 @@ namespace test {
class Server_test : public beast::unit_test::suite
{
public:
enum
{
testPort = 40000
};
class TestThread
{
@@ -91,7 +87,7 @@ public:
if (level < threshold())
return;
suite_.log << text;
suite_.log << text << std::endl;
}
};
@@ -156,12 +152,10 @@ public:
// Connect to an address
template <class Socket>
bool
connect (Socket& s, std::string const& addr, int port)
connect (Socket& s, typename Socket::endpoint_type const& ep)
{
try
{
typename Socket::endpoint_type ep (
boost::asio::ip::address::from_string (addr), port);
s.connect (ep);
pass();
return true;
@@ -222,13 +216,13 @@ public:
}
void
test_request()
test_request(boost::asio::ip::tcp::endpoint const& ep)
{
boost::asio::io_service ios;
using socket = boost::asio::ip::tcp::socket;
socket s (ios);
if (! connect (s, "127.0.0.1", testPort))
if (! connect (s, ep))
return;
if (! write (s,
@@ -247,13 +241,13 @@ public:
}
void
test_keepalive()
test_keepalive(boost::asio::ip::tcp::endpoint const& ep)
{
boost::asio::io_service ios;
using socket = boost::asio::ip::tcp::socket;
socket s (ios);
if (! connect (s, "127.0.0.1", testPort))
if (! connect (s, ep))
return;
if (! write (s,
@@ -280,6 +274,7 @@ public:
void basicTests()
{
testcase("Basic client/server");
TestSink sink {*this};
TestThread thread;
sink.threshold (beast::severities::Severity::kAll);
@@ -287,24 +282,23 @@ public:
TestHandler handler;
auto s = make_Server (handler,
thread.get_io_service(), journal);
std::vector<Port> list;
list.resize(1);
list.back().port = testPort;
list.back().ip = boost::asio::ip::address::from_string (
"127.0.0.1");
list.back().protocol.insert("http");
s->ports (list);
test_request();
//test_keepalive();
std::vector<Port> serverPort(1);
serverPort.back().ip =
boost::asio::ip::address::from_string ("127.0.0.1"),
serverPort.back().port = 0;
serverPort.back().protocol.insert("http");
auto eps = s->ports (serverPort);
log << "server listening on port " << eps[0].port() << std::endl;
test_request(eps[0]);
test_keepalive(eps[0]);
//s->close();
s = nullptr;
pass();
}
void stressTest()
{
testcase("stress test");
struct NullHandler
{
bool
@@ -360,14 +354,14 @@ public:
TestThread thread;
auto s = make_Server(h,
thread.get_io_service(), {});
std::vector<Port> list;
list.resize(1);
list.back().port = testPort;
list.back().ip = boost::asio::ip::address::from_string (
"127.0.0.1");
list.back().protocol.insert("http");
s->ports (list);
std::vector<Port> serverPort(1);
serverPort.back().ip =
boost::asio::ip::address::from_string ("127.0.0.1"),
serverPort.back().port = 0;
serverPort.back().protocol.insert("http");
s->ports (serverPort);
}
pass();
}
/**