From ea86f98cade7cdb24c2ac208a134849adacd35ad Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 7 May 2019 08:48:19 +0300
Subject: [PATCH] Make net tests more reliable

---
 net/cpp/include/ftl/net/universe.hpp |  2 +-
 net/cpp/src/peer.cpp                 |  3 ++-
 net/cpp/src/universe.cpp             |  6 +++---
 net/cpp/test/net_integration.cpp     | 15 +++++++++------
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp
index b29dc10ac..728421c47 100644
--- a/net/cpp/include/ftl/net/universe.hpp
+++ b/net/cpp/include/ftl/net/universe.hpp
@@ -56,7 +56,7 @@ class Universe {
 	 *
 	 * @param addr URI giving protocol, interface and port
 	 */
-	bool connect(const std::string &addr);
+	Peer *connect(const std::string &addr);
 	
 	int numberOfPeers() const { return peers_.size(); }
 	
diff --git a/net/cpp/src/peer.cpp b/net/cpp/src/peer.cpp
index 25a1721cb..2032b5acf 100644
--- a/net/cpp/src/peer.cpp
+++ b/net/cpp/src/peer.cpp
@@ -178,7 +178,8 @@ Peer::Peer(const char *pUri, Dispatcher *d) : uri_(pUri) {
 	scheme_ = uri.getProtocol();
 	if (uri.getProtocol() == URI::SCHEME_TCP) {
 		sock_ = tcpConnect(uri);
-		status_ = kConnecting;
+		if (sock_ != INVALID_SOCKET) status_ = kConnecting;
+		else status_ = kReconnecting;
 	} else if (uri.getProtocol() == URI::SCHEME_WS) {
 		LOG(INFO) << "Websocket connect " << uri.getPath();
 		sock_ = tcpConnect(uri);
diff --git a/net/cpp/src/universe.cpp b/net/cpp/src/universe.cpp
index f6d7e2acb..421f5eebe 100644
--- a/net/cpp/src/universe.cpp
+++ b/net/cpp/src/universe.cpp
@@ -66,9 +66,9 @@ bool Universe::listen(const string &addr) {
 	return l->isListening();
 }
 
-bool Universe::connect(const string &addr) {
+Peer *Universe::connect(const string &addr) {
 	auto p = new Peer(addr.c_str(), &disp_);
-	if (!p) return false;
+	if (!p) return nullptr;
 	
 	if (p->status() != Peer::kInvalid) {
 		unique_lock<mutex> lk(net_mutex_);
@@ -81,7 +81,7 @@ bool Universe::connect(const string &addr) {
 		peer_ids_[p.id()] = &p;
 	});
 	
-	return p->status() == Peer::kConnecting;
+	return p;
 }
 
 int Universe::_setDescriptors() {
diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp
index 34f01215a..f6e4de548 100644
--- a/net/cpp/test/net_integration.cpp
+++ b/net/cpp/test/net_integration.cpp
@@ -17,28 +17,31 @@ TEST_CASE("Universe::connect()", "[net]") {
 	Universe b;
 	
 	a.listen("tcp://localhost:7077");
-	sleep_for(milliseconds(100));
+	//sleep_for(milliseconds(100));
 
 	SECTION("valid tcp connection using ipv4") {
-		REQUIRE( b.connect("tcp://127.0.0.1:7077") );
+		auto p = b.connect("tcp://127.0.0.1:7077");
+		REQUIRE( p );
 		
-		sleep_for(milliseconds(200));
+		while (!p->isConnected()) sleep_for(milliseconds(20));
 		
 		REQUIRE( a.numberOfPeers() == 1 );
 		REQUIRE( b.numberOfPeers() == 1 );
 	}
 
 	SECTION("valid tcp connection using hostname") {
-		REQUIRE( b.connect("tcp://localhost:7077") );
+		auto p = b.connect("tcp://localhost:7077");
+		REQUIRE( p );
 		
-		sleep_for(milliseconds(200));
+		while (!p->isConnected()) sleep_for(milliseconds(20));
 		
 		REQUIRE( a.numberOfPeers() == 1 );
 		REQUIRE( b.numberOfPeers() == 1 );
 	}
 
 	SECTION("invalid protocol") {
-		REQUIRE( !b.connect("http://127.0.0.1:7077") );
+		auto p = b.connect("http://127.0.0.1:7077");
+		REQUIRE( !p->isValid() );
 		
 		sleep_for(milliseconds(100));
 		
-- 
GitLab