binary_plugin_router_test.cpp
Go to the documentation of this file.
1 #include "../../src/plugin_router/binary_plugin_router.h"
2 
3 #include <fruit/fruit.h>
4 #include <gtest/gtest.h>
5 
6 #include <algorithm>
7 #include <boost/array.hpp>
8 #include <boost/asio.hpp>
9 #include <functional>
10 #include <iostream>
11 #include <iterator>
12 #include <memory>
13 #include <unordered_map>
14 
15 #include "../../src/socket/socket.h"
16 #include "std_msgs/UInt8MultiArray.h"
17 
18 using boost::asio::ip::udp;
19 using fruit::Annotated;
20 using fruit::Component;
21 using fruit::createComponent;
22 using fruit::Injector;
23 
24 class MockBinarySocket : public Socket {
25  private:
26  std::shared_ptr<std::function<void(std::vector<uint8_t>)>> callback_;
27 
28  public:
29  // デフォルトコンストラクタは削除
30  MockBinarySocket() = delete;
32  ASSISTED(udp::endpoint) target_socket,
33  ASSISTED(std::shared_ptr<std::function<void(std::vector<uint8_t>)>>)
34  callback))
35  : callback_(callback) {}
36 
37  virtual void Start() override {
38  std::vector<uint8_t> vec{0, 1};
39  (*callback_)(vec);
40 
41  vec.push_back(2);
42  (*callback_)(vec);
43 
44  vec.push_back(3);
45  (*callback_)(vec);
46  }
47 
48  virtual void SendData(std::vector<uint8_t> data) override {
49  for (int i = 0; i < data.size(); i++) {
50  ASSERT_EQ(data[i], i);
51  }
52  }
53 };
54 
55 // Mockを入れるもの
56 Component<SocketFactory> getMockBinarySourceComponent() {
57  return createComponent().bind<Socket, MockBinarySocket>();
58 }
59 
60 Component<Annotated<BinaryAnnotation, PluginRouterFactory>>
62  return createComponent()
63  .replace(getUdpSocketComponent)
66 }
67 
68 // XmlRpcValueが不正なケース
69 TEST(TestSuite, binary_plugin_try_start_with_invalid_xml) {
70  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
71  config->Parse("{}");
72 
73  // objectを作成し、受信スレッドを開始
74  Injector<Annotated<BinaryAnnotation, PluginRouterFactory>> injector(
76  PluginRouterFactory pluginRouterFactory =
77  injector.get<Annotated<BinaryAnnotation, PluginRouterFactory>>();
78  // データは送信しないのでportは何でも良い
79  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
80  auto result = source->TryStart();
81  // TryStartに失敗して、config間違いのエラーメッセージを受け取る
82  ASSERT_FALSE(result.is_success);
83  ASSERT_STREQ(result.error_message, "invalid config parameters");
84 }
85 
86 // pluginが見つからないケース
87 TEST(TestSuite, binary_plugin_try_start_not_found_plugin) {
88  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
89  config->Parse(
90  "[{\"plugin_name\":\"binary_loopback::BinaryLoopback\",\"param\":"
91  "\"Parameter\"},{"
92  "\"plugin_name\":\"not_found_plugin::NotFoundPlugin\"}]");
93  ROS_INFO("This test produces a ROS_ERROR message because of a loading error in the plugin, but the behavior is as expected.");
94 
95  // objectを作成し、受信スレッドを開始
96  Injector<Annotated<BinaryAnnotation, PluginRouterFactory>> injector(
98  PluginRouterFactory pluginRouterFactory =
99  injector.get<Annotated<BinaryAnnotation, PluginRouterFactory>>();
100  // データは送信しないのでportは何でも良い
101  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
102  auto result = source->TryStart();
103  // TryStartに失敗して、pluginがない旨のメッセージを受け取る
104  ASSERT_FALSE(result.is_success);
105  std::string error_message = result.error_message;
106  ASSERT_EQ(error_message.rfind("Failed to load"), 0);
107 }
108 
109 // Loopback Pluginを使うケース
110 TEST(TestSuite, binary_plugin_try_start_with_loopback_plugin) {
111  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
112  config->Parse(
113  "[{\"plugin_name\":\"binary_loopback::BinaryLoopback\",\"param\":"
114  "\"Parameter\"}]");
115 
116  // objectを作成し、受信スレッドを開始
117  Injector<Annotated<BinaryAnnotation, PluginRouterFactory>> injector(
119  PluginRouterFactory pluginRouterFactory =
120  injector.get<Annotated<BinaryAnnotation, PluginRouterFactory>>();
121  // データは送信しないのでportは何でも良い
122  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
123  auto result = source->TryStart();
124  // TryStartに成功する
125  // データの送信と評価はMockBinarySocket内でやっている
126  ASSERT_TRUE(result.is_success);
127 }
MockBinarySocket::Start
virtual void Start() override
Definition: binary_plugin_router_test.cpp:37
MockBinarySocket::INJECT
INJECT(MockBinarySocket(ASSISTED(udp::endpoint) target_socket, callback))
Definition: binary_plugin_router_test.cpp:31
getMockBinaryPluginRouterComponent
Component< Annotated< BinaryAnnotation, PluginRouterFactory > > getMockBinaryPluginRouterComponent()
Definition: binary_plugin_router_test.cpp:61
getUdpSocketComponent
Component< SocketFactory > getUdpSocketComponent()
Definition: udp_socket.cpp:87
MockBinarySocket
Definition: binary_plugin_router_test.cpp:24
Socket
Definition: socket.h:8
MockBinarySocket::callback_
std::shared_ptr< std::function< void(std::vector< uint8_t >)> > callback_
Definition: binary_plugin_router_test.cpp:26
MockBinarySocket::MockBinarySocket
MockBinarySocket()=delete
getMockBinarySourceComponent
Component< SocketFactory > getMockBinarySourceComponent()
Definition: binary_plugin_router_test.cpp:56
TEST
TEST(TestSuite, binary_plugin_try_start_with_invalid_xml)
Definition: binary_plugin_router_test.cpp:69
PluginRouterFactory
std::function< std::unique_ptr< PluginRouter >(std::shared_ptr< rapidjson::Document >, udp::endpoint)> PluginRouterFactory
Definition: plugin_router.h:29
MockBinarySocket::SendData
virtual void SendData(std::vector< uint8_t > data) override
Definition: binary_plugin_router_test.cpp:48
getBinaryPluginRouterComponent
Component< fruit::Annotated< BinaryAnnotation, PluginRouterFactory > > getBinaryPluginRouterComponent()
Definition: binary_plugin_router.cpp:87
ROS_INFO
#define ROS_INFO(...)


skyway
Author(s): Toshiya Nakakura
autogenerated on Thu Oct 26 2023 02:42:21