From 839e07f2529012df5c343c1ebfe5218ddc40acb1 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Tue, 10 May 2022 20:50:33 +0100
Subject: [PATCH] Allow waitConnections timeout

---
 include/ftl/protocol/self.hpp | 2 +-
 src/self.cpp                  | 4 ++--
 src/universe.cpp              | 6 +++---
 src/universe.hpp              | 2 +-
 test/net_integration.cpp      | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/ftl/protocol/self.hpp b/include/ftl/protocol/self.hpp
index fcb697d..1eab3c9 100644
--- a/include/ftl/protocol/self.hpp
+++ b/include/ftl/protocol/self.hpp
@@ -63,7 +63,7 @@ class Self {
 	 * Will block until all currently registered connnections have completed.
 	 * You should not use this, but rather use onConnect.
 	 */
-	int waitConnections();
+	int waitConnections(int seconds = 1);
 	
 	/** get peer pointer by peer UUID, returns nullptr if not found */
 	std::shared_ptr<ftl::protocol::Node> getNode(const ftl::UUID &pid) const;
diff --git a/src/self.cpp b/src/self.cpp
index d7e5e8f..672a5d8 100644
--- a/src/self.cpp
+++ b/src/self.cpp
@@ -66,8 +66,8 @@ size_t Self::numberOfNodes() const {
     return universe_->numberOfPeers();
 }
 
-int Self::waitConnections() {
-    return universe_->waitConnections();
+int Self::waitConnections(int seconds) {
+    return universe_->waitConnections(seconds);
 }
 
 std::shared_ptr<ftl::protocol::Node> Self::getNode(const ftl::UUID &pid) const {
diff --git a/src/universe.cpp b/src/universe.cpp
index 3234336..ddf2574 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -276,12 +276,12 @@ void Universe::unbind(const std::string &name) {
 	disp_.unbind(name);
 }
 
-int Universe::waitConnections() {
+int Universe::waitConnections(int seconds) {
 	SHARED_LOCK(net_mutex_, lk);
 	auto peers = peers_;
 	lk.unlock();
-	return std::count_if(peers.begin(), peers.end(), [](const auto &p) {
-		return p && p->waitConnection();
+	return std::count_if(peers.begin(), peers.end(), [seconds](const auto &p) {
+		return p && p->waitConnection(seconds);
 	});
 }
 
diff --git a/src/universe.hpp b/src/universe.hpp
index 1930d21..f486b89 100644
--- a/src/universe.hpp
+++ b/src/universe.hpp
@@ -99,7 +99,7 @@ public:
 	 * Will block until all currently registered connnections have completed.
 	 * You should not use this, but rather use onConnect.
 	 */
-	int waitConnections();
+	int waitConnections(int seconds = 1);
 	
 	/** get peer pointer by peer UUID, returns nullptr if not found */
 	PeerPtr getPeer(const ftl::UUID &pid) const;
diff --git a/test/net_integration.cpp b/test/net_integration.cpp
index 1ba5701..867d85f 100644
--- a/test/net_integration.cpp
+++ b/test/net_integration.cpp
@@ -48,7 +48,7 @@ TEST_CASE("Listen and Connect", "[net]") {
 		
 		REQUIRE( p->waitConnection(5) );
 		
-		REQUIRE( self->waitConnections() == 1 );
+		REQUIRE( self->waitConnections(5) == 1 );
 		REQUIRE( ftl::getSelf()->numberOfNodes() == 1);
 	}
 
-- 
GitLab