diff --git a/src/peer.cpp b/src/peer.cpp index 465cc7d49207bbeb9a2a8b8dae3d6b62532cf223..066b2a89c82cc05b1608ab868411a4d31da3357a 100644 --- a/src/peer.cpp +++ b/src/peer.cpp @@ -54,7 +54,7 @@ bool Peer::isConnected() const { } bool Peer::isValid() const { - return sock_ && ((status_ == NodeStatus::kConnected) || (status_ == NodeStatus::kConnecting)); + return sock_ && sock_->fd() != INVALID_SOCKET && ((status_ == NodeStatus::kConnected) || (status_ == NodeStatus::kConnecting)); } void Peer::_set_socket_options() { diff --git a/src/universe.cpp b/src/universe.cpp index 7858a1a3d5f2c901a3aee1fd3a937d1b38871fa4..ec5583b5489397991c3ef63ba7e219dc4fe99536 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -291,21 +291,24 @@ socket_t Universe::_setDescriptors() { //Set file descriptor for the listening sockets. for (auto &l : listeners_) { if (l) { - FD_SET(l->fd(), &impl_->sfdread_); - FD_SET(l->fd(), &impl_->sfderror_); + auto sock = l->fd(); + if (sock != INVALID_SOCKET) { + FD_SET(sock, &impl_->sfdread_); + FD_SET(sock, &impl_->sfderror_); + } n = std::max<socket_t>(n, l->fd()); } } //Set the file descriptors for each client for (const auto &s : peers_) { - // NOTE: s->isValid() should return true only and only if a valid OS - // socket exists. - if (s && s->isValid()) { - n = std::max<socket_t>(n, s->_socket()); - FD_SET(s->_socket(), &impl_->sfdread_); - FD_SET(s->_socket(), &impl_->sfderror_); + auto sock = s->_socket(); + n = std::max<socket_t>(n, sock); + if (sock != INVALID_SOCKET) { + FD_SET(sock, &impl_->sfdread_); + FD_SET(s->_socket(), &impl_->sfderror_); + } } } diff --git a/test/net_performance.cpp b/test/net_performance.cpp index 1472746f3936f939a86c18ab46c84580ef8708c5..e5056f86e45d75d82f08832a19519f1ac5984522 100644 --- a/test/net_performance.cpp +++ b/test/net_performance.cpp @@ -64,16 +64,12 @@ static float peer_send(ftl::net::Peer* p, const std::vector<DTYPE>& data, int cn return (throughput_send + throughput_recv)/2.0f; } -std::unique_ptr<Universe> net_server; -std::unique_ptr<Universe> net_client; -std::atomic_bool init_ = false; - -static void init() { - if (init_.exchange(true)) { return; } +ftl::URI uri(""); - net_server = std::make_unique<Universe>(); +TEST_CASE("throughput", "[net]") { + auto net_server = std::make_unique<Universe>(); net_server->setLocalID(ftl::UUID()); - net_client = std::make_unique<Universe>(); + auto net_client = std::make_unique<Universe>(); net_client->setLocalID(ftl::UUID()); net_server->bind("test_server", [](){ LOG(INFO) << "test_server"; }); @@ -82,23 +78,12 @@ static void init() { data_test.clear(); data_test.reserve(BSIZE); for (int i = 0; i < BSIZE; i++) { data_test.push_back(i ^ (i - 1)); } -} - -ftl::URI uri(""); -TEST_CASE("throughput", "[net]") { - - SECTION("create server and client Universe instances") { - init(); - } - - SECTION("create TCP server") { - std::string host = "localhost"; - int port = 0; // pick random port - net_server->listen(ftl::URI("tcp://" + host + ":" + std::to_string(port))); - int listening_port = net_server->getListeningURIs()[0].getPort(); - uri = ftl::URI("tcp://localhost:" + std::to_string(listening_port)); - } + std::string host = "localhost"; + int port = 0; // pick random port + net_server->listen(ftl::URI("tcp://" + host + ":" + std::to_string(port))); + int listening_port = net_server->getListeningURIs()[0].getPort(); + uri = ftl::URI("tcp://localhost:" + std::to_string(listening_port)); SECTION("TCP throughput") { LOG(INFO) << "connecting to " << uri.to_string(); @@ -107,6 +92,6 @@ TEST_CASE("throughput", "[net]") { while(!p->isConnected()) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } auto r = peer_send(p.get(), data_test, COUNT); - //REQUIRE(r > 8000); + REQUIRE(r > 1000); } } \ No newline at end of file