From dd57f7b64528d674633dae3a8e6113979d5d0966 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Fri, 6 May 2022 20:55:13 +0100
Subject: [PATCH] Fix some integration tests

---
 .vscode/launch.json      | 35 +++++++++++++++++++++++++++++++++++
 src/protocol.cpp         |  1 -
 src/universe.cpp         | 16 +++++++++-------
 test/CMakeLists.txt      |  2 +-
 test/net_integration.cpp | 14 ++++++++------
 5 files changed, 53 insertions(+), 15 deletions(-)
 create mode 100644 .vscode/launch.json

diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..71b0e0b
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,35 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "g++ - Build and debug active file",
+            "type": "cppdbg",
+            "request": "launch",
+            "program": "${command:cmake.launchTargetPath}",
+            "args": [],
+            "stopAtEntry": false,
+            "cwd": "${workspaceFolder}/build",
+            "environment": [],
+            "externalConsole": false,
+            "MIMode": "gdb",
+            "setupCommands": [
+                {
+                    "description": "Enable pretty-printing for gdb",
+                    "text": "-enable-pretty-printing",
+                    "ignoreFailures": true
+                }
+            ],
+            "preLaunchTask": "C/C++: g++ build active file",
+            "miDebuggerPath": "/usr/bin/gdb",
+            "sourceFileMap": {
+                "${workspaceFolder}": {	
+                    "editorPath": "${workspaceFolder}",
+                    "useForBreakpoints": "true"
+                }
+            }            
+        }
+    ]
+}
\ No newline at end of file
diff --git a/src/protocol.cpp b/src/protocol.cpp
index 26e7436..d30058e 100644
--- a/src/protocol.cpp
+++ b/src/protocol.cpp
@@ -6,7 +6,6 @@ static std::shared_ptr<ftl::net::Universe> universe;
 
 ctpl::thread_pool ftl::pool(std::thread::hardware_concurrency()*2);
 
-/** Reset network and streams. Used by tests. */
 void ftl::protocol::reset() {
     universe.reset();
 }
diff --git a/src/universe.cpp b/src/universe.cpp
index c91b3b3..f8e2fa1 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -165,7 +165,7 @@ void Universe::shutdown() {
 	UNIQUE_LOCK(net_mutex_, lk);
 
 	for (auto &s : peers_) {
-		s->rawClose();
+		if (s) s->rawClose();
 	}
 	
 	peers_.clear();
@@ -310,11 +310,11 @@ void Universe::_installBindings() {
 void Universe::_cleanupPeers() {
 	auto i = peers_.begin();
 	while (i != peers_.end()) {
-		if (!(*i)->isValid() ||
-			(*i)->status() == NodeStatus::kReconnecting ||
-			(*i)->status() == NodeStatus::kDisconnected) {
-			
-			const auto &p = *i;
+		auto &p = *i;
+		if (p && (!p->isValid() ||
+			p->status() == NodeStatus::kReconnecting ||
+			p->status() == NodeStatus::kDisconnected)) {
+
 			LOG(INFO) << "Removing disconnected peer: " << p->id().to_string();
 			_notifyDisconnect(p.get());
 
@@ -328,13 +328,15 @@ void Universe::_cleanupPeers() {
 				}
 			}
 
-			i = peers_.erase(i);
+			//i = peers_.erase(i);
 
 			if (p->status() == NodeStatus::kReconnecting) {
 				reconnects_.push_back({reconnect_attempts_, 1.0f, p});
 			} else {
 				garbage_.push_back(p);
 			}
+
+			p.reset();
 		} else {
 			i++;
 		}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3a52a82..f4c8cf9 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -30,7 +30,7 @@ add_test(HandleUnitTest handle_unit)
 
 ### URI ########################################################################
 add_executable(net_integration
-	$<TARGET_OBJECTS:CatchTest>
+	$<TARGET_OBJECTS:CatchTestFTL>
 	./net_integration.cpp)
 target_include_directories(net_integration PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include")
 target_link_libraries(net_integration beyond-protocol
diff --git a/test/net_integration.cpp b/test/net_integration.cpp
index c33a79c..6095d62 100644
--- a/test/net_integration.cpp
+++ b/test/net_integration.cpp
@@ -29,9 +29,9 @@ TEST_CASE("Listen and Connect", "[net]") {
 	auto self = ftl::createDummySelf();
 	
 	self->listen(ftl::URI("tcp://localhost:0")); 
-	auto uri = "tcp://localhost:" + std::to_string(self->getListeningURIs().front().getPort());
 
 	SECTION("valid tcp connection using ipv4") {
+		auto uri = "tcp://127.0.0.1:" + std::to_string(self->getListeningURIs().front().getPort());
 		LOG(INFO) << uri;
 		auto p = ftl::createNode(uri);
 		REQUIRE( p );
@@ -39,19 +39,21 @@ TEST_CASE("Listen and Connect", "[net]") {
 		p->waitConnection();
 		
 		REQUIRE( self->numberOfNodes() == 1 );
+		REQUIRE( ftl::getSelf()->numberOfNodes() == 1);
 	}
 
-	/*SECTION("valid tcp connection using hostname") {
-		auto p = b.connect(uri);
+	SECTION("valid tcp connection using hostname") {
+		auto uri = "tcp://localhost:" + std::to_string(self->getListeningURIs().front().getPort());
+		auto p = ftl::createNode(uri);
 		REQUIRE( p );
 		
 		p->waitConnection();
 		
-		REQUIRE( a.numberOfPeers() == 1 );
-		REQUIRE( b.numberOfPeers() == 1 );
+		REQUIRE( self->numberOfNodes() == 1 );
+		REQUIRE( ftl::getSelf()->numberOfNodes() == 1);
 	}
 
-	SECTION("invalid protocol") {
+	/*SECTION("invalid protocol") {
 		bool throws = false;
 		try {
 			auto p = b.connect("http://localhost:1234");
-- 
GitLab