From bfe9ec0ab57477e8bdd90ffb7be6255f3a00277e Mon Sep 17 00:00:00 2001
From: Sasa Stanko <sasa.stanko@memgraph.io>
Date: Tue, 8 Aug 2017 11:34:50 +0200
Subject: [PATCH] Fix bug in StopServer function

Summary: Add test for system start and shutdown

Reviewers: mferencevic, zuza

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D642
---
 experimental/distributed/src/communication.cpp    |  1 -
 experimental/distributed/src/communication.hpp    |  3 +--
 experimental/distributed/tests/connector_unit.cpp | 15 +++++++++++++++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/experimental/distributed/src/communication.cpp b/experimental/distributed/src/communication.cpp
index feb9b2886..b858e82ba 100644
--- a/experimental/distributed/src/communication.cpp
+++ b/experimental/distributed/src/communication.cpp
@@ -33,7 +33,6 @@ std::pair<EventStream*, std::shared_ptr<Channel>> Reactor::Open(const std::strin
   std::unique_lock<std::mutex> lock(*mutex_);
   // TODO: Improve the check that the channel name does not exist in the
   // system.
-  assert(connectors_.count(connector_name) == 0);
   if (connectors_.count(connector_name) != 0) {
     throw std::runtime_error("Connector with name " + connector_name
         + "already exists");
diff --git a/experimental/distributed/src/communication.hpp b/experimental/distributed/src/communication.hpp
index 059de9f97..b148f950c 100644
--- a/experimental/distributed/src/communication.hpp
+++ b/experimental/distributed/src/communication.hpp
@@ -546,9 +546,8 @@ class Network {
   void StopServer() {
     if (server_ != nullptr) {
       server_->Shutdown();
-      server_ = nullptr;
+      thread_.join();
     }
-    thread_.join();
   }
 
  private:
diff --git a/experimental/distributed/tests/connector_unit.cpp b/experimental/distributed/tests/connector_unit.cpp
index 18e81e2fa..120fe451a 100644
--- a/experimental/distributed/tests/connector_unit.cpp
+++ b/experimental/distributed/tests/connector_unit.cpp
@@ -10,6 +10,21 @@
 
 #include "communication.hpp"
 
+TEST(SystemTest, ReturnWithoutThrowing) {
+  struct Master : public Reactor {
+    Master(System *system, std::string name) : Reactor(system, name) {}
+    virtual void Run() {
+      CloseConnector("main");
+    }
+  };
+
+  System system;
+  ASSERT_NO_THROW(system.StartServices());
+  ASSERT_NO_THROW(system.Spawn<Master>("master"));
+  ASSERT_NO_THROW(system.AwaitShutdown());
+}
+
+
 TEST(ChannelCreationTest, ThrowOnReusingChannelName) {
   struct Master : public Reactor {
     Master(System *system, std::string name) : Reactor(system, name) {}