tests/test_fork.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <assert.h>
7 
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/wait.h>
11 
13 
15 
16 #define NUM_MESSAGES 5
17 
18 void test_fork ()
19 {
20 #if !defined(ZMQ_HAVE_WINDOWS)
21  // Create and bind pull socket to receive messages
22  void *pull = test_context_socket (ZMQ_PULL);
24 
25  int pid = fork ();
26  if (pid == 0) {
27  // use regular assertions in the child process
28 
29  // Child process
30  // Immediately close parent sockets and context
31  zmq_close (pull);
33 
34  // Create new context, socket, connect and send some messages
35  void *child_ctx = zmq_ctx_new ();
36  assert (child_ctx);
37  void *push = zmq_socket (child_ctx, ZMQ_PUSH);
38  assert (push);
39  int rc = zmq_connect (push, connect_address);
40  assert (rc == 0);
41  int count;
42  for (count = 0; count < NUM_MESSAGES; count++)
43  zmq_send (push, "Hello", 5, 0);
44 
45  zmq_close (push);
46  zmq_ctx_destroy (child_ctx);
47  exit (0);
48  } else {
49  // Parent process
50  int count;
51  for (count = 0; count < NUM_MESSAGES; count++) {
52  recv_string_expect_success (pull, "Hello", 0);
53  }
54  int child_status;
55  while (true) {
56  int rc = waitpid (pid, &child_status, 0);
57  if (rc == -1 && errno == EINTR)
58  continue;
60  // Verify the status code of the child was zero
61  TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status));
62  break;
63  }
65  }
66 #endif
67 }
68 
69 int main (void)
70 {
72 
73  UNITY_BEGIN ();
75  return UNITY_END ();
76 }
UNITY_END
return UNITY_END()
EINTR
#define EINTR
Definition: errno.hpp:7
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
zmq_ctx_new
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:109
TEST_ASSERT_GREATER_THAN
#define TEST_ASSERT_GREATER_THAN(threshold, actual)
Definition: unity.h:152
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
errno
int errno
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
zmq_ctx_destroy
ZMQ_EXPORT int zmq_ctx_destroy(void *context_)
Definition: zmq.cpp:212
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
testutil.hpp
NUM_MESSAGES
#define NUM_MESSAGES
Definition: tests/test_fork.cpp:16
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
test_fork
void test_fork()
Definition: tests/test_fork.cpp:18
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
connect_address
SETUP_TEARDOWN_TESTCONTEXT char connect_address[MAX_SOCKET_STRING]
Definition: tests/test_fork.cpp:14
push
static void push(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5890
zmq_ctx_term
ZMQ_EXPORT int zmq_ctx_term(void *context_)
Definition: zmq.cpp:128
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
assert.h
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
count
GLint GLsizei count
Definition: glcorearb.h:2830
main
int main(void)
Definition: tests/test_fork.cpp:69


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59