test_fork.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <assert.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <sys/wait.h>
  30. SETUP_TEARDOWN_TESTCONTEXT
  31. char connect_address[MAX_SOCKET_STRING];
  32. #define NUM_MESSAGES 5
  33. void test_fork ()
  34. {
  35. #if !defined(ZMQ_HAVE_WINDOWS)
  36. // Create and bind pull socket to receive messages
  37. void *pull = test_context_socket (ZMQ_PULL);
  38. bind_loopback_ipv4 (pull, connect_address, sizeof connect_address);
  39. int pid = fork ();
  40. if (pid == 0) {
  41. // use regular assertions in the child process
  42. // Child process
  43. // Immediately close parent sockets and context
  44. zmq_close (pull);
  45. zmq_ctx_term (get_test_context ());
  46. // Create new context, socket, connect and send some messages
  47. void *child_ctx = zmq_ctx_new ();
  48. assert (child_ctx);
  49. void *push = zmq_socket (child_ctx, ZMQ_PUSH);
  50. assert (push);
  51. int rc = zmq_connect (push, connect_address);
  52. assert (rc == 0);
  53. int count;
  54. for (count = 0; count < NUM_MESSAGES; count++)
  55. zmq_send (push, "Hello", 5, 0);
  56. zmq_close (push);
  57. zmq_ctx_destroy (child_ctx);
  58. exit (0);
  59. } else {
  60. // Parent process
  61. int count;
  62. for (count = 0; count < NUM_MESSAGES; count++) {
  63. recv_string_expect_success (pull, "Hello", 0);
  64. }
  65. int child_status;
  66. while (true) {
  67. int rc = waitpid (pid, &child_status, 0);
  68. if (rc == -1 && errno == EINTR)
  69. continue;
  70. TEST_ASSERT_GREATER_THAN (0, rc);
  71. // Verify the status code of the child was zero
  72. TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status));
  73. break;
  74. }
  75. test_context_socket_close (pull);
  76. }
  77. #endif
  78. }
  79. int main (void)
  80. {
  81. setup_test_environment ();
  82. UNITY_BEGIN ();
  83. RUN_TEST (test_fork);
  84. return UNITY_END ();
  85. }