Add mainloop testcase
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 9 Jan 2020 04:28:15 +0000 (13:28 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 14 Jan 2020 01:57:30 +0000 (10:57 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/rmi/impl/ondemand/tests/mainloop.cpp [new file with mode: 0644]

diff --git a/src/vist/rmi/impl/ondemand/tests/mainloop.cpp b/src/vist/rmi/impl/ondemand/tests/mainloop.cpp
new file mode 100644 (file)
index 0000000..b0c7d31
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ *  Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <vist/rmi/impl/ondemand/socket.hpp>
+#include <vist/rmi/impl/ondemand/mainloop.hpp>
+
+#include <string>
+#include <thread>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+using namespace vist::rmi;
+using namespace vist::rmi::impl::ondemand;
+
+TEST(MainloopTests, single)
+{
+       std::string sockPath = "@sock";
+       Socket socket(sockPath);
+       Mainloop mainloop;
+
+       int input = std::numeric_limits<int>::max();
+       bool input2 = true;
+
+       int output = 0;
+       bool output2 = false;
+
+       auto onAccept = [&]() {
+               Socket accepted = socket.accept();
+
+               // Recv input from client.
+               accepted.recv(&output);
+               EXPECT_EQ(input, output);
+
+               // Send input2 to client.
+               accepted.send(&input2);
+
+               mainloop.removeHandler(socket.getFd());
+               mainloop.stop();
+       };
+
+       mainloop.addHandler(socket.getFd(), std::move(onAccept));
+       auto server = std::thread([&]() { mainloop.run(); });
+
+       // Send input to server.
+       Socket connected = Socket::connect(sockPath);
+       connected.send(&input);
+
+       // Recv input2 from server.
+       connected.recv(&output2);
+       EXPECT_EQ(input2, output2);
+
+       if (server.joinable())
+               server.join();
+}
+
+TEST(MainloopTests, multiflexing)
+{
+       std::string sockPath = "@sock";
+       Socket socket(sockPath);
+       Mainloop mainloop;
+
+       int input = std::numeric_limits<int>::max();
+       bool input2 = true;
+
+       int output = 0;
+       bool output2 = false;
+
+       auto onAccept = [&]() {
+               Socket accepted = socket.accept();
+
+               // Recv input from client.
+               accepted.recv(&output);
+               EXPECT_EQ(input, output);
+
+               // Send input2 to client.
+               accepted.send(&input2);
+       };
+
+       /// Set timeout to stop
+       mainloop.addHandler(socket.getFd(), std::move(onAccept));
+       auto server = std::thread([&]() { mainloop.run(1000); });
+
+       auto task = [&]() {
+               // Send input to server.
+               Socket connected = Socket::connect(sockPath);
+               connected.send(&input);
+
+               // Recv input2 from server.
+               connected.recv(&output2);
+               EXPECT_EQ(input2, output2);
+       };
+
+       std::vector<std::thread> clients;
+       clients.emplace_back(std::thread(task));
+       clients.emplace_back(std::thread(task));
+       clients.emplace_back(std::thread(task));
+
+       if (server.joinable())
+               server.join();
+
+       for (auto& client : clients)
+               if (client.joinable())
+                       client.join();
+}