changeset 310:7e5001552326

Tests: add test for server-reconnect, #559
author David Demelier <markand@malikania.fr>
date Thu, 20 Oct 2016 19:21:07 +0200
parents 2f72a42ba595
children fa184c88b2c3
files libirccd/irccd/cmd-server-reconnect.cpp libirccd/irccd/cmd-server-reconnect.hpp tests/CMakeLists.txt tests/cmd-server-reconnect/CMakeLists.txt tests/cmd-server-reconnect/main.cpp
diffstat 5 files changed, 133 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/libirccd/irccd/cmd-server-reconnect.cpp	Wed Oct 19 17:46:38 2016 +0200
+++ b/libirccd/irccd/cmd-server-reconnect.cpp	Thu Oct 20 19:21:07 2016 +0200
@@ -20,37 +20,28 @@
 #include "irccd.hpp"
 #include "server.hpp"
 #include "service-server.hpp"
+#include "transport.hpp"
 
 namespace irccd {
 
 namespace command {
 
 ServerReconnectCommand::ServerReconnectCommand()
-    : Command("server-reconnect", "Server", "Force reconnection of one or more servers")
+    : Command("server-reconnect")
 {
 }
 
-std::vector<Command::Arg> ServerReconnectCommand::args() const
-{
-    return {{ "server", false }};
-}
-
-nlohmann::json ServerReconnectCommand::request(Irccdctl &, const CommandRequest &args) const
+void ServerReconnectCommand::exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args)
 {
-    return args.length() == 0 ? nlohmann::json::object() : nlohmann::json::object({ { "server", args.arg(0) } });
-}
+    auto server = args.find("server");
 
-nlohmann::json ServerReconnectCommand::exec(Irccd &irccd, const nlohmann::json &request) const
-{
-    auto server = request.find("server");
-
-    if (server != request.end() && server->is_string())
+    if (server != args.end() && server->is_string())
         irccd.servers().require(*server)->reconnect();
     else
         for (auto &server : irccd.servers().servers())
             server->reconnect();
 
-    return nullptr;
+    client.success("server-reconnect");
 }
 
 } // !command
--- a/libirccd/irccd/cmd-server-reconnect.hpp	Wed Oct 19 17:46:38 2016 +0200
+++ b/libirccd/irccd/cmd-server-reconnect.hpp	Thu Oct 20 19:21:07 2016 +0200
@@ -41,19 +41,9 @@
     IRCCD_EXPORT ServerReconnectCommand();
 
     /**
-     * \copydoc Command::args
-     */
-    IRCCD_EXPORT std::vector<Arg> args() const override;
-
-    /**
-     * \copydoc Command::request
-     */
-    IRCCD_EXPORT nlohmann::json request(Irccdctl &irccdctl, const CommandRequest &args) const override;
-
-    /**
      * \copydoc Command::exec
      */
-    IRCCD_EXPORT nlohmann::json exec(Irccd &irccd, const nlohmann::json &request) const override;
+    IRCCD_EXPORT void exec(Irccd &irccd, TransportClient &client, const nlohmann::json &args) override;
 };
 
 } // !command
--- a/tests/CMakeLists.txt	Wed Oct 19 17:46:38 2016 +0200
+++ b/tests/CMakeLists.txt	Thu Oct 20 19:21:07 2016 +0200
@@ -34,6 +34,7 @@
     add_subdirectory(cmd-server-nick)
     add_subdirectory(cmd-server-notice)
     add_subdirectory(cmd-server-part)
+    add_subdirectory(cmd-server-reconnect)
     add_subdirectory(cmd-server-topic)
 
 #    # Misc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-reconnect/CMakeLists.txt	Thu Oct 20 19:21:07 2016 +0200
@@ -0,0 +1,23 @@
+#
+# CMakeLists.txt -- CMake build system for irccd
+#
+# Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+irccd_define_test(
+    NAME cmd-server-reconnect
+    SOURCES main.cpp
+    LIBRARIES libirccd libirccdctl
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cmd-server-reconnect/main.cpp	Thu Oct 20 19:21:07 2016 +0200
@@ -0,0 +1,102 @@
+/*
+ * main.cpp -- test server-reconnect remote command
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cmd-server-reconnect.hpp>
+#include <command-tester.hpp>
+#include <server-tester.hpp>
+#include <service-server.hpp>
+
+using namespace irccd;
+using namespace irccd::command;
+
+namespace {
+
+bool s1;
+bool s2;
+
+} // !namespace
+
+class ServerReconnectTest : public ServerTester {
+private:
+    bool &m_ref;
+
+public:
+    inline ServerReconnectTest(std::string name, bool &ref) noexcept
+        : ServerTester(name)
+        , m_ref(ref)
+    {
+        m_ref = false;
+    }
+
+    void reconnect() noexcept override
+    {
+        m_ref = true;
+    }
+};
+
+class ServerReconnectCommandTest : public CommandTester {
+public:
+    ServerReconnectCommandTest()
+        : CommandTester(std::make_unique<ServerReconnectCommand>())
+    {
+        m_irccd.servers().add(std::make_unique<ServerReconnectTest>("s1", s1));
+        m_irccd.servers().add(std::make_unique<ServerReconnectTest>("s2", s2));
+    }
+};
+
+TEST_F(ServerReconnectCommandTest, basic)
+{
+    try {
+        m_irccdctl.client().request({
+            { "command", "server-reconnect" },
+            { "server", "s1" }
+        });
+
+        poll([&] () {
+            return s1;
+        });
+
+        ASSERT_TRUE(s1);
+        ASSERT_FALSE(s2);
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+TEST_F(ServerReconnectCommandTest, all)
+{
+    try {
+        m_irccdctl.client().request({{ "command", "server-reconnect" }});
+
+        poll([&] () {
+            return s1 && s2;
+        });
+
+        ASSERT_TRUE(s1);
+        ASSERT_TRUE(s2);
+    } catch (const std::exception &ex) {
+        FAIL() << ex.what();
+    }
+}
+
+int main(int argc, char **argv)
+{
+    testing::InitGoogleTest(&argc, argv);
+
+    return RUN_ALL_TESTS();
+}