test_proxy.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. SETUP_TEARDOWN_TESTCONTEXT
  29. #define CONTENT_SIZE 13
  30. #define CONTENT_SIZE_MAX 32
  31. #define ROUTING_ID_SIZE 10
  32. #define ROUTING_ID_SIZE_MAX 32
  33. #define QT_WORKERS 5
  34. #define QT_CLIENTS 3
  35. #define is_verbose 0
  36. struct thread_data
  37. {
  38. int id;
  39. };
  40. typedef struct
  41. {
  42. uint64_t msg_in;
  43. uint64_t bytes_in;
  44. uint64_t msg_out;
  45. uint64_t bytes_out;
  46. } zmq_socket_stats_t;
  47. typedef struct
  48. {
  49. zmq_socket_stats_t frontend;
  50. zmq_socket_stats_t backend;
  51. } zmq_proxy_stats_t;
  52. void *g_clients_pkts_out = NULL;
  53. void *g_workers_pkts_out = NULL;
  54. // Asynchronous client-to-server (DEALER to ROUTER) - pure libzmq
  55. //
  56. // While this example runs in a single process, that is to make
  57. // it easier to start and stop the example. Each task may have its own
  58. // context and conceptually acts as a separate process. To have this
  59. // behaviour, it is necessary to replace the inproc transport of the
  60. // control socket by a tcp transport.
  61. // This is our client task
  62. // It connects to the server, and then sends a request once per second
  63. // It collects responses as they arrive, and it prints them out. We will
  64. // run several client tasks in parallel, each with a different random ID.
  65. static void client_task (void *db_)
  66. {
  67. const thread_data *const databag = static_cast<const thread_data *> (db_);
  68. // Endpoint socket gets random port to avoid test failing when port in use
  69. void *endpoint = zmq_socket (get_test_context (), ZMQ_PAIR);
  70. TEST_ASSERT_NOT_NULL (endpoint);
  71. int linger = 0;
  72. TEST_ASSERT_SUCCESS_ERRNO (
  73. zmq_setsockopt (endpoint, ZMQ_LINGER, &linger, sizeof (linger)));
  74. char endpoint_source[256];
  75. sprintf (endpoint_source, "inproc://endpoint%d", databag->id);
  76. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (endpoint, endpoint_source));
  77. char *my_endpoint = s_recv (endpoint);
  78. TEST_ASSERT_NOT_NULL (my_endpoint);
  79. void *client = zmq_socket (get_test_context (), ZMQ_DEALER);
  80. TEST_ASSERT_NOT_NULL (client);
  81. // Control socket receives terminate command from main over inproc
  82. void *control = zmq_socket (get_test_context (), ZMQ_SUB);
  83. TEST_ASSERT_NOT_NULL (control);
  84. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0));
  85. TEST_ASSERT_SUCCESS_ERRNO (
  86. zmq_setsockopt (control, ZMQ_LINGER, &linger, sizeof (linger)));
  87. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
  88. char content[CONTENT_SIZE_MAX] = {};
  89. // Set random routing id to make tracing easier
  90. char routing_id[ROUTING_ID_SIZE] = {};
  91. sprintf (routing_id, "%04X-%04X", rand () % 0xFFFF, rand () % 0xFFFF);
  92. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  93. client, ZMQ_ROUTING_ID, routing_id,
  94. ROUTING_ID_SIZE)); // includes '\0' as an helper for printf
  95. linger = 0;
  96. TEST_ASSERT_SUCCESS_ERRNO (
  97. zmq_setsockopt (client, ZMQ_LINGER, &linger, sizeof (linger)));
  98. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
  99. zmq_pollitem_t items[] = {{client, 0, ZMQ_POLLIN, 0},
  100. {control, 0, ZMQ_POLLIN, 0}};
  101. int request_nbr = 0;
  102. bool run = true;
  103. bool keep_sending = true;
  104. while (run) {
  105. // Tick once per 200 ms, pulling in arriving messages
  106. int centitick;
  107. for (centitick = 0; centitick < 20; centitick++) {
  108. zmq_poll (items, 2, 10);
  109. if (items[0].revents & ZMQ_POLLIN) {
  110. int rcvmore;
  111. size_t sz = sizeof (rcvmore);
  112. int rc = TEST_ASSERT_SUCCESS_ERRNO (
  113. zmq_recv (client, content, CONTENT_SIZE_MAX, 0));
  114. TEST_ASSERT_EQUAL_INT (CONTENT_SIZE, rc);
  115. if (is_verbose)
  116. printf (
  117. "client receive - routing_id = %s content = %s\n",
  118. routing_id, content);
  119. // Check that message is still the same
  120. TEST_ASSERT_EQUAL_STRING_LEN ("request #", content, 9);
  121. TEST_ASSERT_SUCCESS_ERRNO (
  122. zmq_getsockopt (client, ZMQ_RCVMORE, &rcvmore, &sz));
  123. TEST_ASSERT_FALSE (rcvmore);
  124. }
  125. if (items[1].revents & ZMQ_POLLIN) {
  126. int rc = zmq_recv (control, content, CONTENT_SIZE_MAX, 0);
  127. if (rc > 0) {
  128. content[rc] = 0; // NULL-terminate the command string
  129. if (is_verbose)
  130. printf (
  131. "client receive - routing_id = %s command = %s\n",
  132. routing_id, content);
  133. if (memcmp (content, "TERMINATE", 9) == 0) {
  134. run = false;
  135. break;
  136. }
  137. if (memcmp (content, "STOP", 4) == 0) {
  138. keep_sending = false;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. if (keep_sending) {
  145. sprintf (content, "request #%03d", ++request_nbr); // CONTENT_SIZE
  146. if (is_verbose)
  147. printf ("client send - routing_id = %s request #%03d\n",
  148. routing_id, request_nbr);
  149. zmq_atomic_counter_inc (g_clients_pkts_out);
  150. TEST_ASSERT_EQUAL_INT (CONTENT_SIZE,
  151. zmq_send (client, content, CONTENT_SIZE, 0));
  152. }
  153. }
  154. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (client));
  155. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
  156. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (endpoint));
  157. free (my_endpoint);
  158. }
  159. // This is our server task.
  160. // It uses the multithreaded server model to deal requests out to a pool
  161. // of workers and route replies back to clients. One worker can handle
  162. // one request at a time but one client can talk to multiple workers at
  163. // once.
  164. static void server_worker (void * /*unused_*/);
  165. void server_task (void * /*unused_*/)
  166. {
  167. // Frontend socket talks to clients over TCP
  168. char my_endpoint[MAX_SOCKET_STRING];
  169. void *frontend = zmq_socket (get_test_context (), ZMQ_ROUTER);
  170. TEST_ASSERT_NOT_NULL (frontend);
  171. int linger = 0;
  172. TEST_ASSERT_SUCCESS_ERRNO (
  173. zmq_setsockopt (frontend, ZMQ_LINGER, &linger, sizeof (linger)));
  174. bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
  175. // Backend socket talks to workers over inproc
  176. void *backend = zmq_socket (get_test_context (), ZMQ_DEALER);
  177. TEST_ASSERT_NOT_NULL (backend);
  178. TEST_ASSERT_SUCCESS_ERRNO (
  179. zmq_setsockopt (backend, ZMQ_LINGER, &linger, sizeof (linger)));
  180. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "inproc://backend"));
  181. // Control socket receives terminate command from main over inproc
  182. void *control = zmq_socket (get_test_context (), ZMQ_REP);
  183. TEST_ASSERT_NOT_NULL (control);
  184. TEST_ASSERT_SUCCESS_ERRNO (
  185. zmq_setsockopt (control, ZMQ_LINGER, &linger, sizeof (linger)));
  186. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control_proxy"));
  187. // Launch pool of worker threads, precise number is not critical
  188. int thread_nbr;
  189. void *threads[5];
  190. for (thread_nbr = 0; thread_nbr < QT_WORKERS; thread_nbr++)
  191. threads[thread_nbr] = zmq_threadstart (&server_worker, NULL);
  192. // Endpoint socket sends random port to avoid test failing when port in use
  193. void *endpoint_receivers[QT_CLIENTS];
  194. char endpoint_source[256];
  195. for (int i = 0; i < QT_CLIENTS; ++i) {
  196. endpoint_receivers[i] = zmq_socket (get_test_context (), ZMQ_PAIR);
  197. TEST_ASSERT_NOT_NULL (endpoint_receivers[i]);
  198. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  199. endpoint_receivers[i], ZMQ_LINGER, &linger, sizeof (linger)));
  200. sprintf (endpoint_source, "inproc://endpoint%d", i);
  201. TEST_ASSERT_SUCCESS_ERRNO (
  202. zmq_bind (endpoint_receivers[i], endpoint_source));
  203. }
  204. for (int i = 0; i < QT_CLIENTS; ++i) {
  205. send_string_expect_success (endpoint_receivers[i], my_endpoint, 0);
  206. }
  207. // Connect backend to frontend via a proxy
  208. TEST_ASSERT_SUCCESS_ERRNO (
  209. zmq_proxy_steerable (frontend, backend, NULL, control));
  210. for (thread_nbr = 0; thread_nbr < QT_WORKERS; thread_nbr++)
  211. zmq_threadclose (threads[thread_nbr]);
  212. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
  213. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
  214. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
  215. for (int i = 0; i < QT_CLIENTS; ++i) {
  216. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (endpoint_receivers[i]));
  217. }
  218. }
  219. // Each worker task works on one request at a time and sends a random number
  220. // of replies back, with random delays between replies:
  221. // The comments in the first column, if suppressed, makes it a poller version
  222. static void server_worker (void * /*unused_*/)
  223. {
  224. void *worker = zmq_socket (get_test_context (), ZMQ_DEALER);
  225. TEST_ASSERT_NOT_NULL (worker);
  226. int linger = 0;
  227. TEST_ASSERT_SUCCESS_ERRNO (
  228. zmq_setsockopt (worker, ZMQ_LINGER, &linger, sizeof (linger)));
  229. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (worker, "inproc://backend"));
  230. // Control socket receives terminate command from main over inproc
  231. void *control = zmq_socket (get_test_context (), ZMQ_SUB);
  232. TEST_ASSERT_NOT_NULL (control);
  233. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0));
  234. TEST_ASSERT_SUCCESS_ERRNO (
  235. zmq_setsockopt (control, ZMQ_LINGER, &linger, sizeof (linger)));
  236. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
  237. char content[CONTENT_SIZE_MAX] =
  238. {}; // bigger than what we need to check that
  239. char routing_id[ROUTING_ID_SIZE_MAX] =
  240. {}; // the size received is the size sent
  241. bool run = true;
  242. bool keep_sending = true;
  243. while (run) {
  244. int rc = zmq_recv (control, content, CONTENT_SIZE_MAX,
  245. ZMQ_DONTWAIT); // usually, rc == -1 (no message)
  246. if (rc > 0) {
  247. content[rc] = 0; // NULL-terminate the command string
  248. if (is_verbose)
  249. printf ("server_worker receives command = %s\n", content);
  250. if (memcmp (content, "TERMINATE", 9) == 0)
  251. run = false;
  252. if (memcmp (content, "STOP", 4) == 0)
  253. keep_sending = false;
  254. }
  255. // The DEALER socket gives us the reply envelope and message
  256. // if we don't poll, we have to use ZMQ_DONTWAIT, if we poll, we can block-receive with 0
  257. rc = zmq_recv (worker, routing_id, ROUTING_ID_SIZE_MAX, ZMQ_DONTWAIT);
  258. if (rc == ROUTING_ID_SIZE) {
  259. rc = zmq_recv (worker, content, CONTENT_SIZE_MAX, 0);
  260. TEST_ASSERT_EQUAL_INT (CONTENT_SIZE, rc);
  261. if (is_verbose)
  262. printf ("server receive - routing_id = %s content = %s\n",
  263. routing_id, content);
  264. // Send 0..4 replies back
  265. if (keep_sending) {
  266. int reply, replies = rand () % 5;
  267. for (reply = 0; reply < replies; reply++) {
  268. // Sleep for some fraction of a second
  269. msleep (rand () % 10 + 1);
  270. // Send message from server to client
  271. if (is_verbose)
  272. printf ("server send - routing_id = %s reply\n",
  273. routing_id);
  274. zmq_atomic_counter_inc (g_workers_pkts_out);
  275. rc = zmq_send (worker, routing_id, ROUTING_ID_SIZE,
  276. ZMQ_SNDMORE);
  277. TEST_ASSERT_EQUAL_INT (ROUTING_ID_SIZE, rc);
  278. rc = zmq_send (worker, content, CONTENT_SIZE, 0);
  279. TEST_ASSERT_EQUAL_INT (CONTENT_SIZE, rc);
  280. }
  281. }
  282. }
  283. }
  284. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (worker));
  285. TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
  286. }
  287. uint64_t recv_stat (void *sock_, bool last_)
  288. {
  289. uint64_t res;
  290. zmq_msg_t stats_msg;
  291. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&stats_msg));
  292. TEST_ASSERT_EQUAL_INT (sizeof (uint64_t),
  293. zmq_recvmsg (sock_, &stats_msg, 0));
  294. memcpy (&res, zmq_msg_data (&stats_msg), zmq_msg_size (&stats_msg));
  295. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&stats_msg));
  296. int more;
  297. size_t moresz = sizeof more;
  298. TEST_ASSERT_SUCCESS_ERRNO (
  299. zmq_getsockopt (sock_, ZMQ_RCVMORE, &more, &moresz));
  300. TEST_ASSERT_TRUE ((last_ && !more) || (!last_ && more));
  301. return res;
  302. }
  303. // Utility function to interrogate the proxy:
  304. void check_proxy_stats (void *control_proxy_)
  305. {
  306. zmq_proxy_stats_t total_stats;
  307. send_string_expect_success (control_proxy_, "STATISTICS", 0);
  308. // first frame of the reply contains FRONTEND stats:
  309. total_stats.frontend.msg_in = recv_stat (control_proxy_, false);
  310. total_stats.frontend.bytes_in = recv_stat (control_proxy_, false);
  311. total_stats.frontend.msg_out = recv_stat (control_proxy_, false);
  312. total_stats.frontend.bytes_out = recv_stat (control_proxy_, false);
  313. // second frame of the reply contains BACKEND stats:
  314. total_stats.backend.msg_in = recv_stat (control_proxy_, false);
  315. total_stats.backend.bytes_in = recv_stat (control_proxy_, false);
  316. total_stats.backend.msg_out = recv_stat (control_proxy_, false);
  317. total_stats.backend.bytes_out = recv_stat (control_proxy_, true);
  318. // check stats
  319. if (is_verbose) {
  320. printf (
  321. "frontend: pkts_in=%lu bytes_in=%lu pkts_out=%lu bytes_out=%lu\n",
  322. static_cast<unsigned long int> (total_stats.frontend.msg_in),
  323. static_cast<unsigned long int> (total_stats.frontend.bytes_in),
  324. static_cast<unsigned long int> (total_stats.frontend.msg_out),
  325. static_cast<unsigned long int> (total_stats.frontend.bytes_out));
  326. printf (
  327. "backend: pkts_in=%lu bytes_in=%lu pkts_out=%lu bytes_out=%lu\n",
  328. static_cast<unsigned long int> (total_stats.backend.msg_in),
  329. static_cast<unsigned long int> (total_stats.backend.bytes_in),
  330. static_cast<unsigned long int> (total_stats.backend.msg_out),
  331. static_cast<unsigned long int> (total_stats.backend.bytes_out));
  332. printf ("clients sent out %d requests\n",
  333. zmq_atomic_counter_value (g_clients_pkts_out));
  334. printf ("workers sent out %d replies\n",
  335. zmq_atomic_counter_value (g_workers_pkts_out));
  336. }
  337. TEST_ASSERT_EQUAL_UINT (
  338. (unsigned) zmq_atomic_counter_value (g_clients_pkts_out),
  339. total_stats.frontend.msg_in);
  340. TEST_ASSERT_EQUAL_UINT (
  341. (unsigned) zmq_atomic_counter_value (g_workers_pkts_out),
  342. total_stats.frontend.msg_out);
  343. TEST_ASSERT_EQUAL_UINT (
  344. (unsigned) zmq_atomic_counter_value (g_workers_pkts_out),
  345. total_stats.backend.msg_in);
  346. TEST_ASSERT_EQUAL_UINT (
  347. (unsigned) zmq_atomic_counter_value (g_clients_pkts_out),
  348. total_stats.backend.msg_out);
  349. }
  350. // The main thread simply starts several clients and a server, and then
  351. // waits for the server to finish.
  352. void test_proxy ()
  353. {
  354. g_clients_pkts_out = zmq_atomic_counter_new ();
  355. g_workers_pkts_out = zmq_atomic_counter_new ();
  356. // Control socket receives terminate command from main over inproc
  357. void *control = test_context_socket (ZMQ_PUB);
  358. int linger = 0;
  359. TEST_ASSERT_SUCCESS_ERRNO (
  360. zmq_setsockopt (control, ZMQ_LINGER, &linger, sizeof (linger)));
  361. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
  362. // Control socket receives terminate command from main over inproc
  363. void *control_proxy = test_context_socket (ZMQ_REQ);
  364. TEST_ASSERT_SUCCESS_ERRNO (
  365. zmq_setsockopt (control_proxy, ZMQ_LINGER, &linger, sizeof (linger)));
  366. TEST_ASSERT_SUCCESS_ERRNO (
  367. zmq_bind (control_proxy, "inproc://control_proxy"));
  368. void *threads[QT_CLIENTS + 1];
  369. struct thread_data databags[QT_CLIENTS + 1];
  370. for (int i = 0; i < QT_CLIENTS; i++) {
  371. databags[i].id = i;
  372. threads[i] = zmq_threadstart (&client_task, &databags[i]);
  373. }
  374. threads[QT_CLIENTS] = zmq_threadstart (&server_task, NULL);
  375. msleep (500); // Run for 500 ms then quit
  376. if (is_verbose)
  377. printf ("stopping all clients and server workers\n");
  378. send_string_expect_success (control, "STOP", 0);
  379. msleep (500); // Wait for all clients and workers to STOP
  380. if (is_verbose)
  381. printf ("retrieving stats from the proxy\n");
  382. check_proxy_stats (control_proxy);
  383. if (is_verbose)
  384. printf ("shutting down all clients and server workers\n");
  385. send_string_expect_success (control, "TERMINATE", 0);
  386. if (is_verbose)
  387. printf ("shutting down the proxy\n");
  388. send_string_expect_success (control_proxy, "TERMINATE", 0);
  389. test_context_socket_close (control);
  390. test_context_socket_close (control_proxy);
  391. for (int i = 0; i < QT_CLIENTS + 1; i++)
  392. zmq_threadclose (threads[i]);
  393. }
  394. int main (void)
  395. {
  396. setup_test_environment ();
  397. UNITY_BEGIN ();
  398. RUN_TEST (test_proxy);
  399. return UNITY_END ();
  400. }