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