PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tests/test_socket_options.cpp

https://github.com/datasift/zmqpp
C++ | 288 lines | 231 code | 31 blank | 26 comment | 31 complexity | e95c1393511aa4c0de6ab5787a4156b9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*
  2. * Created on: 8 Aug 2011
  3. * Author: @benjamg
  4. */
  5. #include <boost/test/unit_test.hpp>
  6. #include <boost/lexical_cast.hpp>
  7. #include "zmqpp/context.hpp"
  8. #include "zmqpp/socket.hpp"
  9. BOOST_AUTO_TEST_SUITE( socket_options )
  10. #define STRINGIFY(x) #x
  11. #define CHECK_SET(socket, type, option) check_set<type>(socket, zmqpp::socket_option::option, STRINGIFY(option), false)
  12. #define CHECK_SET_POSITIVE(socket, type, option) check_set<type>(socket, zmqpp::socket_option::option, STRINGIFY(option), true)
  13. #define CHECK_GET(socket, type, option) check_get<type>(socket, zmqpp::socket_option::option, STRINGIFY(option))
  14. // Note the hacky abuse of the fact we don't have float options
  15. #define CHECK_NOSET(socket, option) check_set<float>(socket, zmqpp::socket_option::option, STRINGIFY(option), false)
  16. #define CHECK_NOGET(socket, option) check_get<float>(socket, zmqpp::socket_option::option, STRINGIFY(option))
  17. template<typename CheckType, typename WantedType>
  18. void try_set(zmqpp::socket& socket, zmqpp::socket_option const& option, CheckType const& value, std::string const& option_name, std::string const& value_type)
  19. {
  20. BOOST_CHECKPOINT("setting option " << option_name << " against set type '" << value_type << "'");
  21. try
  22. {
  23. socket.set(option, value);
  24. BOOST_CHECK_MESSAGE(typeid(CheckType) == typeid(WantedType), "expected exception setting option '" << option_name << "' against type '" << value_type << "'");
  25. }
  26. catch(zmqpp::zmq_internal_exception const& e)
  27. {
  28. BOOST_CHECK_MESSAGE(false, "threw internal exception " << e.zmq_error() << " '" << e.what() << "' setting option '" << option_name << "' against type '" << value_type << "'");
  29. }
  30. catch(zmqpp::exception const& e)
  31. {
  32. BOOST_CHECK_MESSAGE(typeid(CheckType) != typeid(WantedType), "threw unexpected exception '" << e.what() << "' setting option '" << option_name << "' against type '" << value_type << "'");
  33. }
  34. }
  35. template<typename CheckType, typename WantedType>
  36. void try_get(zmqpp::socket const& socket, zmqpp::socket_option const& option, std::string const& option_name, std::string const& value_type)
  37. {
  38. BOOST_CHECKPOINT("getting option " << option_name << " against set type '" << value_type << "'");
  39. try
  40. {
  41. CheckType value;
  42. socket.get(option, value);
  43. BOOST_CHECK_MESSAGE(typeid(CheckType) == typeid(WantedType), "expected exception getting option " << option_name << " against type '" << value_type << "'");
  44. }
  45. catch(zmqpp::zmq_internal_exception const& e)
  46. {
  47. BOOST_CHECK_MESSAGE(false, "threw internal exception " << e.zmq_error() << " '" << e.what() << "' getting option " << option_name << " against type '" << value_type << "'");
  48. }
  49. catch(zmqpp::exception const& e)
  50. {
  51. BOOST_CHECK_MESSAGE(typeid(CheckType) != typeid(WantedType), "threw unexpected exception '" << e.what() << "' getting option " << option_name << " against type '" << value_type << "'");
  52. }
  53. }
  54. template<typename Type>
  55. void check_set(zmqpp::socket& socket, zmqpp::socket_option const& option, std::string const& option_name, bool positive_only)
  56. {
  57. // Boolean
  58. try_set<bool, Type>(socket, option, true, option_name, "boolean (true)");
  59. try_set<bool, Type>(socket, option, false, option_name, "boolean (false)");
  60. // Integer - Masquerade of unsigned 64bit integer
  61. if (typeid(uint64_t) == typeid(Type))
  62. {
  63. // Positive integers are valid as unsigned 64bit
  64. try_set<int, Type>(socket, option, -1, option_name, "signed integer (negative)");
  65. try_set<int, int>(socket, option, 42, option_name, "signed_integer (positive / masquerade)");
  66. }
  67. else if (typeid(int64_t) == typeid(Type))
  68. {
  69. if (positive_only) { try_set<int, Type>(socket, option, -1, option_name, "signed integer (negative)"); }
  70. else { try_set<int, int>(socket, option, -1, option_name, "signed integer (negative / masquerade)"); }
  71. try_set<int, int>(socket, option, 42, option_name, "signed integer (positive / masquerade)");
  72. }
  73. // Integer - Masquerade of boolean
  74. else if (typeid(bool) == typeid(Type))
  75. {
  76. // 1 and 0 Integers are valid as boolean
  77. try_set<int, Type>(socket, option, -1, option_name, "signed integer (negative)");
  78. try_set<int, int>(socket, option, 0, option_name, "signed integer (false / masquerade)");
  79. try_set<int, int>(socket, option, 1, option_name, "signed integer (true / masquerade)");
  80. try_set<int, Type>(socket, option, 42, option_name, "signed integer (positive)");
  81. }
  82. // Integer - Others
  83. else
  84. {
  85. if (positive_only) { try_set<int, float>(socket, option, -1, option_name, "signed integer (negative / masquerade)"); }
  86. else { try_set<int, Type>(socket, option, -1, option_name, "signed integer (negative)"); }
  87. try_set<int, Type>(socket, option, 42, option_name, "signed integer (positive)");
  88. }
  89. // Unsigned 64bit integer
  90. try_set<uint64_t, Type>(socket, option, 1, option_name, "unsigned 64bit integer");
  91. #if (ZMQ_VERSION_MAJOR == 2) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR < 2))
  92. // 64bit integer
  93. try_set<int64_t, Type>(socket, option, 1, option_name, "signed 64bit integer");
  94. #endif
  95. // Strings
  96. try_set<std::string, Type>(socket, option, "test", option_name, "string");
  97. }
  98. template<typename Type>
  99. void check_get(zmqpp::socket& socket, zmqpp::socket_option const& option, std::string const& option_name)
  100. {
  101. // Boolean
  102. try_get<bool, Type>(socket, option, option_name, "boolean");
  103. // Integer - Masquerade of boolean
  104. if (typeid(bool) == typeid(Type))
  105. {
  106. try_get<int, int>(socket, option, option_name, "signed integer (masquerade)");
  107. }
  108. // Integer - Others
  109. else
  110. {
  111. try_get<int, Type>(socket, option, option_name, "signed integer");
  112. }
  113. // Unsigned 64bit integer
  114. try_get<uint64_t, Type>(socket, option, option_name, "unsigned 64bit integer");
  115. #if (ZMQ_VERSION_MAJOR == 2)
  116. // 64bit integer - Masquerade of boolean
  117. if (typeid(bool) == typeid(Type))
  118. {
  119. try_get<int64_t, int64_t>(socket, option, option_name, "signed 64bit integer (masquerade)");
  120. }
  121. // 64bit integer - Others
  122. else
  123. {
  124. try_get<int64_t, Type>(socket, option, option_name, "signed 64bit integer");
  125. }
  126. #else
  127. // 64bit integer
  128. try_get<int64_t, Type>(socket, option, option_name, "signed 64bit integer");
  129. #endif
  130. // Strings
  131. try_get<std::string, Type>(socket, option, option_name, "string");
  132. }
  133. BOOST_AUTO_TEST_CASE( set_socket_options )
  134. {
  135. zmqpp::context context;
  136. zmqpp::socket socket(context, zmqpp::socket_type::subscribe);
  137. socket.bind("inproc://test");
  138. CHECK_NOSET(socket, receive_more);
  139. CHECK_NOSET(socket, file_descriptor);
  140. CHECK_NOSET(socket, events);
  141. CHECK_NOSET(socket, type);
  142. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
  143. CHECK_NOSET(socket, last_endpoint);
  144. #endif
  145. CHECK_SET(socket, int, linger);
  146. CHECK_SET_POSITIVE(socket, int, backlog);
  147. CHECK_SET(socket, int, receive_timeout);
  148. CHECK_SET(socket, int, send_timeout);
  149. CHECK_SET(socket, uint64_t, affinity);
  150. CHECK_SET(socket, std::string, identity);
  151. CHECK_SET(socket, std::string, subscribe);
  152. CHECK_SET(socket, std::string, unsubscribe);
  153. // For some reason -1 not working here
  154. //CHECK_SET(socket, int, reconnect_interval);
  155. CHECK_SET_POSITIVE(socket, int, reconnect_interval_max);
  156. CHECK_SET_POSITIVE(socket, int, backlog);
  157. #if (ZMQ_VERSION_MAJOR > 2)
  158. CHECK_SET_POSITIVE(socket, int, send_buffer_size);
  159. CHECK_SET_POSITIVE(socket, int, receive_buffer_size);
  160. CHECK_SET_POSITIVE(socket, int, rate);
  161. CHECK_SET_POSITIVE(socket, int, recovery_interval);
  162. CHECK_SET_POSITIVE(socket, int, send_high_water_mark);
  163. CHECK_SET_POSITIVE(socket, int, receive_high_water_mark);
  164. CHECK_SET_POSITIVE(socket, int, multicast_hops);
  165. CHECK_SET_POSITIVE(socket, int64_t, max_messsage_size);
  166. #else
  167. CHECK_SET(socket, bool, multicast_loopback);
  168. CHECK_SET_POSITIVE(socket, int64_t, rate);
  169. CHECK_SET_POSITIVE(socket, int64_t, recovery_interval);
  170. CHECK_SET_POSITIVE(socket, int64_t, recovery_interval_seconds);
  171. CHECK_SET_POSITIVE(socket, int64_t, swap_size);
  172. CHECK_SET(socket, uint64_t, send_buffer_size);
  173. CHECK_SET(socket, uint64_t, receive_buffer_size);
  174. CHECK_SET(socket, uint64_t, high_water_mark);
  175. #endif
  176. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 1))
  177. CHECK_SET(socket, bool, ipv4_only);
  178. #endif
  179. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
  180. #if (ZMQ_VERSION_MINOR == 2)
  181. CHECK_SET(socket, bool, delay_attach_on_connect);
  182. #else
  183. CHECK_SET(socket, bool, immediate);
  184. #endif
  185. // CHECK_SET(socket, int, tcp_keepalive); --- special case of boolean but with -1?
  186. CHECK_SET(socket, int, tcp_keepalive_idle);
  187. CHECK_SET(socket, int, tcp_keepalive_count);
  188. CHECK_SET(socket, int, tcp_keepalive_interval);
  189. // CHECK_SET(socket, std::string, tcp_accept_filter); --- special case required to be an address
  190. #endif
  191. #ifdef ZMQ_EXPERIMENTAL_LABELS
  192. CHECK_NOSET(socket, receive_label);
  193. #endif
  194. }
  195. BOOST_AUTO_TEST_CASE( get_socket_options )
  196. {
  197. zmqpp::context context;
  198. zmqpp::socket socket(context, zmqpp::socket_type::subscribe);
  199. socket.bind("inproc://test");
  200. CHECK_NOGET(socket, subscribe);
  201. CHECK_NOGET(socket, unsubscribe);
  202. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
  203. CHECK_NOGET(socket, router_mandatory);
  204. CHECK_NOGET(socket, xpub_verbose);
  205. CHECK_NOGET(socket, tcp_accept_filter);
  206. #endif
  207. CHECK_GET(socket, bool, receive_more);
  208. CHECK_GET(socket, int, file_descriptor);
  209. CHECK_GET(socket, int, events);
  210. CHECK_GET(socket, int, type);
  211. CHECK_GET(socket, int, linger);
  212. CHECK_GET(socket, int, backlog);
  213. CHECK_GET(socket, int, reconnect_interval);
  214. CHECK_GET(socket, int, reconnect_interval_max);
  215. CHECK_GET(socket, int, receive_timeout);
  216. CHECK_GET(socket, int, send_timeout);
  217. CHECK_GET(socket, uint64_t, affinity);
  218. CHECK_GET(socket, std::string, identity);
  219. #if (ZMQ_VERSION_MAJOR > 2)
  220. CHECK_GET(socket, int, send_buffer_size);
  221. CHECK_GET(socket, int, receive_buffer_size);
  222. CHECK_GET(socket, int, rate);
  223. CHECK_GET(socket, int, recovery_interval);
  224. CHECK_GET(socket, int, send_high_water_mark);
  225. CHECK_GET(socket, int, receive_high_water_mark);
  226. CHECK_GET(socket, int, multicast_hops);
  227. CHECK_GET(socket, int64_t, max_messsage_size);
  228. #else
  229. CHECK_GET(socket, bool, multicast_loopback);
  230. CHECK_GET(socket, int64_t, rate);
  231. CHECK_GET(socket, int64_t, recovery_interval);
  232. CHECK_GET(socket, int64_t, recovery_interval_seconds);
  233. CHECK_GET(socket, int64_t, swap_size);
  234. CHECK_GET(socket, uint64_t, send_buffer_size);
  235. CHECK_GET(socket, uint64_t, receive_buffer_size);
  236. CHECK_GET(socket, uint64_t, high_water_mark);
  237. #endif
  238. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 1))
  239. CHECK_GET(socket, bool, ipv4_only);
  240. #endif
  241. #if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
  242. #if (ZMQ_VERSION_MINOR == 2)
  243. CHECK_GET(socket, bool, delay_attach_on_connect);
  244. #else
  245. CHECK_GET(socket, bool, immediate);
  246. #endif
  247. CHECK_GET(socket, std::string, last_endpoint);
  248. CHECK_GET(socket, int, tcp_keepalive);
  249. CHECK_GET(socket, int, tcp_keepalive_idle);
  250. CHECK_GET(socket, int, tcp_keepalive_count);
  251. CHECK_GET(socket, int, tcp_keepalive_interval);
  252. #endif
  253. #ifdef ZMQ_EXPERIMENTAL_LABELS
  254. CHECK_GET(socket, bool, receive_label);
  255. #endif
  256. }
  257. BOOST_AUTO_TEST_SUITE_END()