test_msg_ffn.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. void ffn (void *data_, void *hint_)
  29. {
  30. // Signal that ffn has been called by writing "freed" to hint
  31. (void) data_; // Suppress 'unused' warnings at compile time
  32. memcpy (hint_, (void *) "freed", 5);
  33. }
  34. void test_msg_init_ffn ()
  35. {
  36. // Create the infrastructure
  37. char my_endpoint[MAX_SOCKET_STRING];
  38. void *router = test_context_socket (ZMQ_ROUTER);
  39. bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
  40. void *dealer = test_context_socket (ZMQ_DEALER);
  41. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
  42. // Test that creating and closing a message triggers ffn
  43. zmq_msg_t msg;
  44. char hint[5];
  45. char data[255];
  46. memset (data, 0, 255);
  47. memcpy (data, (void *) "data", 4);
  48. memcpy (hint, (void *) "hint", 4);
  49. TEST_ASSERT_SUCCESS_ERRNO (
  50. zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
  51. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  52. msleep (SETTLE_TIME);
  53. TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
  54. memcpy (hint, (void *) "hint", 4);
  55. // Making and closing a copy triggers ffn
  56. zmq_msg_t msg2;
  57. zmq_msg_init (&msg2);
  58. TEST_ASSERT_SUCCESS_ERRNO (
  59. zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
  60. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
  61. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
  62. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  63. msleep (SETTLE_TIME);
  64. TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
  65. memcpy (hint, (void *) "hint", 4);
  66. // Test that sending a message triggers ffn
  67. TEST_ASSERT_SUCCESS_ERRNO (
  68. zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
  69. zmq_msg_send (&msg, dealer, 0);
  70. char buf[255];
  71. TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
  72. TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
  73. TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
  74. msleep (SETTLE_TIME);
  75. TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
  76. memcpy (hint, (void *) "hint", 4);
  77. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  78. // Sending a copy of a message triggers ffn
  79. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg2));
  80. TEST_ASSERT_SUCCESS_ERRNO (
  81. zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
  82. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
  83. zmq_msg_send (&msg, dealer, 0);
  84. TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
  85. TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
  86. TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
  87. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
  88. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  89. msleep (SETTLE_TIME);
  90. TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
  91. // Deallocate the infrastructure.
  92. test_context_socket_close (router);
  93. test_context_socket_close (dealer);
  94. }
  95. int main (void)
  96. {
  97. setup_test_environment ();
  98. UNITY_BEGIN ();
  99. RUN_TEST (test_msg_init_ffn);
  100. return UNITY_END ();
  101. }