mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-07-18 11:00:36 +02:00
HTTPServer: start an I/O loop in a new thread and accept connections
Socket handling methods are copied from CConnMan: `CConnman::GenerateWaitSockets()` `CConnman::SocketHandlerListening()` `CConnman::ThreadSocketHandler()` and `CConnman::SocketHandler()` are combined into ThreadSocketHandler()`. Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using http_bitcoin::HTTPHeaders;
|
||||
using http_bitcoin::HTTPRemoteClient;
|
||||
using http_bitcoin::HTTPRequest;
|
||||
using http_bitcoin::HTTPResponse;
|
||||
using http_bitcoin::HTTPServer;
|
||||
@@ -396,14 +397,35 @@ BOOST_AUTO_TEST_CASE(http_server_socket_tests)
|
||||
// We are bound and listening
|
||||
BOOST_REQUIRE_EQUAL(server.GetListeningSocketCount(), 1);
|
||||
|
||||
// Pick up the phone, there's no one there
|
||||
CService addr_connection;
|
||||
BOOST_REQUIRE(!server.AcceptConnectionFromListeningSocket(addr_connection));
|
||||
// Start the I/O loop
|
||||
server.StartSocketsThreads();
|
||||
|
||||
// No connections yet
|
||||
BOOST_CHECK_EQUAL(server.GetConnectionsCount(), 0);
|
||||
|
||||
// Create a mock client and add it to the local CreateSock queue
|
||||
ConnectClient();
|
||||
// Accept the connection
|
||||
BOOST_REQUIRE(server.AcceptConnectionFromListeningSocket(addr_connection));
|
||||
BOOST_CHECK_EQUAL(addr_connection.ToStringAddrPort(), "5.5.5.5:6789");
|
||||
|
||||
// Wait up to a minute to find and connect the client in the I/O loop
|
||||
int attempts{6000};
|
||||
while (server.GetConnectionsCount() < 1) {
|
||||
std::this_thread::sleep_for(10ms);
|
||||
BOOST_REQUIRE(--attempts > 0);
|
||||
}
|
||||
|
||||
// Inspect the connection
|
||||
const HTTPRemoteClient& client = server.GetFirstConnection();
|
||||
BOOST_CHECK_EQUAL(client.m_origin, "5.5.5.5:6789");
|
||||
|
||||
// Stop the I/O thread before modifying m_connected. CloseConnection() is a
|
||||
// temporary test-only API that is not thread-safe against GenerateWaitSockets(),
|
||||
// which iterates m_connected on the I/O thread.
|
||||
server.InterruptNet();
|
||||
server.JoinSocketsThreads();
|
||||
// Close connection
|
||||
BOOST_REQUIRE(server.CloseConnection(client));
|
||||
// Close all listening sockets
|
||||
server.StopListening();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user