PageRenderTime 39ms CodeModel.GetById 2ms app.highlight 33ms 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
  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) or ((ZMQ_VERSION_MAJOR == 3) and (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) or ((ZMQ_VERSION_MAJOR == 3) and (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	CHECK_SET_POSITIVE(socket, int, backlog);
177#if (ZMQ_VERSION_MAJOR > 2)
178	CHECK_SET_POSITIVE(socket, int, send_buffer_size);
179	CHECK_SET_POSITIVE(socket, int, receive_buffer_size);
180	CHECK_SET_POSITIVE(socket, int, rate);
181	CHECK_SET_POSITIVE(socket, int, recovery_interval);
182	CHECK_SET_POSITIVE(socket, int, send_high_water_mark);
183	CHECK_SET_POSITIVE(socket, int, receive_high_water_mark);
184	CHECK_SET_POSITIVE(socket, int, multicast_hops);
185	CHECK_SET_POSITIVE(socket, int64_t, max_messsage_size);
186#else
187	CHECK_SET(socket, bool, multicast_loopback);
188	CHECK_SET_POSITIVE(socket, int64_t, rate);
189	CHECK_SET_POSITIVE(socket, int64_t, recovery_interval);
190	CHECK_SET_POSITIVE(socket, int64_t, recovery_interval_seconds);
191	CHECK_SET_POSITIVE(socket, int64_t, swap_size);
192	CHECK_SET(socket, uint64_t, send_buffer_size);
193	CHECK_SET(socket, uint64_t, receive_buffer_size);
194	CHECK_SET(socket, uint64_t, high_water_mark);
195#endif
196
197#if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 1))
198	CHECK_SET(socket, bool, ipv4_only);
199#endif
200
201#if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
202#if (ZMQ_VERSION_MINOR == 2)
203	CHECK_SET(socket, bool, delay_attach_on_connect);
204#else
205	CHECK_SET(socket, bool, immediate);
206#endif
207	// CHECK_SET(socket, int, tcp_keepalive);  --- special case of boolean but with -1?
208	CHECK_SET(socket, int, tcp_keepalive_idle);
209	CHECK_SET(socket, int, tcp_keepalive_count);
210	CHECK_SET(socket, int, tcp_keepalive_interval);
211	// CHECK_SET(socket, std::string, tcp_accept_filter); --- special case required to be an address
212#endif
213
214#ifdef ZMQ_EXPERIMENTAL_LABELS
215	CHECK_NOSET(socket, receive_label);
216#endif
217}
218
219BOOST_AUTO_TEST_CASE( get_socket_options )
220{
221	zmqpp::context context;
222	zmqpp::socket socket(context, zmqpp::socket_type::subscribe);
223	socket.bind("inproc://test");
224
225	CHECK_NOGET(socket, subscribe);
226	CHECK_NOGET(socket, unsubscribe);
227#if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
228	CHECK_NOGET(socket, router_mandatory);
229	CHECK_NOGET(socket, xpub_verbose);
230	CHECK_NOGET(socket, tcp_accept_filter);
231#endif
232
233	CHECK_GET(socket, bool, receive_more);
234	CHECK_GET(socket, int, file_descriptor);
235	CHECK_GET(socket, int, events);
236	CHECK_GET(socket, int, type);
237	CHECK_GET(socket, int, linger);
238	CHECK_GET(socket, int, backlog);
239	CHECK_GET(socket, int, reconnect_interval);
240	CHECK_GET(socket, int, reconnect_interval_max);
241	CHECK_GET(socket, int, receive_timeout);
242	CHECK_GET(socket, int, send_timeout);
243	CHECK_GET(socket, uint64_t, affinity);
244	CHECK_GET(socket, std::string, identity);
245
246#if (ZMQ_VERSION_MAJOR > 2)
247	CHECK_GET(socket, int, send_buffer_size);
248	CHECK_GET(socket, int, receive_buffer_size);
249	CHECK_GET(socket, int, rate);
250	CHECK_GET(socket, int, recovery_interval);
251	CHECK_GET(socket, int, send_high_water_mark);
252	CHECK_GET(socket, int, receive_high_water_mark);
253	CHECK_GET(socket, int, multicast_hops);
254	CHECK_GET(socket, int64_t, max_messsage_size);
255#else
256	CHECK_GET(socket, bool, multicast_loopback);
257	CHECK_GET(socket, int64_t, rate);
258	CHECK_GET(socket, int64_t, recovery_interval);
259	CHECK_GET(socket, int64_t, recovery_interval_seconds);
260	CHECK_GET(socket, int64_t, swap_size);
261	CHECK_GET(socket, uint64_t, send_buffer_size);
262	CHECK_GET(socket, uint64_t, receive_buffer_size);
263	CHECK_GET(socket, uint64_t, high_water_mark);
264#endif
265
266#if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 1))
267	CHECK_GET(socket, bool, ipv4_only);
268#endif
269
270#if (ZMQ_VERSION_MAJOR > 3) or ((ZMQ_VERSION_MAJOR == 3) and (ZMQ_VERSION_MINOR >= 2))
271#if (ZMQ_VERSION_MINOR == 2)
272	CHECK_GET(socket, bool, delay_attach_on_connect);
273#else
274	CHECK_GET(socket, bool, immediate);
275#endif
276	CHECK_GET(socket, std::string, last_endpoint);
277	CHECK_GET(socket, int, tcp_keepalive);
278	CHECK_GET(socket, int, tcp_keepalive_idle);
279	CHECK_GET(socket, int, tcp_keepalive_count);
280	CHECK_GET(socket, int, tcp_keepalive_interval);
281#endif
282
283#ifdef ZMQ_EXPERIMENTAL_LABELS
284	CHECK_GET(socket, bool, receive_label);
285#endif
286}
287
288BOOST_AUTO_TEST_SUITE_END()