test_ctx_options.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include <limits>
4 #include "testutil.hpp"
5 #include "testutil_unity.hpp"
6 
8 
9 #define WAIT_FOR_BACKGROUND_THREAD_INSPECTION (0)
10 
11 #ifdef ZMQ_HAVE_LINUX
12 #include <sys/time.h>
13 #include <sys/resource.h>
14 #include <unistd.h> // for sleep()
15 #include <sched.h>
16 
17 #define TEST_POLICY \
18  (SCHED_OTHER) // NOTE: SCHED_OTHER is the default Linux scheduler
19 
21 {
22  // NOTE1: if setrlimit() fails with EPERM, this means that current user has not enough permissions.
23  // NOTE2: even for privileged users (e.g., root) getrlimit() would usually return 0 as nice limit; the only way to
24  // discover if the user is able to increase the nice value is to actually try to change the rlimit:
25  struct rlimit rlim;
26  rlim.rlim_cur = 40;
27  rlim.rlim_max = 40;
28  if (setrlimit (RLIMIT_NICE, &rlim) == 0) {
29  // rlim_cur == 40 means that this process is allowed to set a nice value of -20
31  printf ("This process has enough permissions to raise ZMQ "
32  "background thread priority!\n");
33  return true;
34  }
35 
37  printf ("This process has NOT enough permissions to raise ZMQ "
38  "background thread priority.\n");
39  return false;
40 }
41 
42 #else
43 
44 #define TEST_POLICY (0)
45 
47 {
48  return false;
49 }
50 
51 #endif
52 
53 
55 {
56  // verify that setting negative values (e.g., default values) fail:
63 
64 
65  // test scheduling policy:
66 
67  // set context options that alter the background thread CPU scheduling/affinity settings;
68  // as of ZMQ 4.2.3 this has an effect only on POSIX systems (nothing happens on Windows, but still it should return success):
73 
74  // test priority:
75 
76  // in theory SCHED_OTHER supports only the static priority 0 but quoting the docs
77  // http://man7.org/linux/man-pages/man7/sched.7.html
78  // "The thread to run is chosen from the static priority 0 list based on
79  // a dynamic priority that is determined only inside this list. The
80  // dynamic priority is based on the nice value [...]
81  // The nice value can be modified using nice(2), setpriority(2), or sched_setattr(2)."
82  // ZMQ will internally use nice(2) to set the nice value when using SCHED_OTHER.
83  // However changing the nice value of a process requires appropriate permissions...
84  // check that the current effective user is able to do that:
88  1 /* any positive value different than the default will be ok */));
89  }
90 
91 
92  // test affinity:
93 
94  // this should result in background threads being placed only on the
95  // first CPU available on this system; try experimenting with other values
96  // (e.g., 5 to use CPU index 5) and use "top -H" or "taskset -pc" to see the result
97 
98  int cpus_add[] = {0, 1};
99  for (unsigned int idx = 0; idx < sizeof (cpus_add) / sizeof (cpus_add[0]);
100  idx++) {
103  }
104 
105  // you can also remove CPUs from list of affinities:
106  int cpus_remove[] = {1};
107  for (unsigned int idx = 0;
108  idx < sizeof (cpus_remove) / sizeof (cpus_remove[0]); idx++) {
111  cpus_remove[idx]));
112  }
113 
114 
115  // test INTEGER thread name prefix:
116 
121 
122 #ifdef ZMQ_BUILD_DRAFT_API
123  // test STRING thread name prefix:
124 
125  const char prefix[] = "MyPrefix9012345"; // max len is 16 chars
126 
129  sizeof (prefix) / sizeof (char)));
130 
131  char buf[16];
132  size_t buflen = sizeof (buf) / sizeof (char);
135 #endif
136 }
137 
139 {
140 #ifdef ZMQ_ZERO_COPY_RECV
141  int zero_copy;
142  // Default value is 1.
144  TEST_ASSERT_EQUAL_INT (1, zero_copy);
145 
146  // Test we can set it to 0.
150  TEST_ASSERT_EQUAL_INT (0, zero_copy);
151 
152  // Create a TCP socket pair using the context and test that messages can be
153  // received. Note that inproc sockets cannot be used for this test.
154  void *pull = zmq_socket (get_test_context (), ZMQ_PULL);
155  char endpoint[MAX_SOCKET_STRING];
156  bind_loopback_ipv4 (pull, endpoint, sizeof endpoint);
157 
160 
161  const char *small_str = "abcd";
162  const char *large_str =
163  "01234567890123456789012345678901234567890123456789";
164 
165  send_string_expect_success (push, small_str, 0);
166  send_string_expect_success (push, large_str, 0);
167 
168  recv_string_expect_success (pull, small_str, 0);
169  recv_string_expect_success (pull, large_str, 0);
170 
171  // Clean up.
178 #endif
179 }
180 
182 {
185 }
186 
188 {
189 #if defined(ZMQ_USE_SELECT)
191 #elif defined(ZMQ_USE_POLL) || defined(ZMQ_USE_EPOLL) \
192  || defined(ZMQ_USE_DEVPOLL) || defined(ZMQ_USE_KQUEUE)
194 #endif
195 }
196 
198 {
201 }
202 
204 {
206 }
207 
209 {
210 #if defined(ZMQ_MSG_T_SIZE)
213 #endif
214 }
215 
217 {
221 }
222 
224 {
227 
228  void *router = test_context_socket (ZMQ_ROUTER);
229  int value;
230  size_t optsize = sizeof (int);
232  zmq_getsockopt (router, ZMQ_IPV6, &value, &optsize));
235  zmq_getsockopt (router, ZMQ_LINGER, &value, &optsize));
237  test_context_socket_close (router);
238 
239 #if WAIT_FOR_BACKGROUND_THREAD_INSPECTION
240  // this is useful when you want to use an external tool (like top or taskset) to view
241  // properties of the background threads
242  printf ("Sleeping for 100sec. You can now use 'top -H -p $(pgrep -f "
243  "test_ctx_options)' and 'taskset -pc <ZMQ background thread PID>' "
244  "to view ZMQ background thread properties.\n");
245  sleep (100);
246 #endif
247 
252  router = test_context_socket (ZMQ_ROUTER);
254  zmq_getsockopt (router, ZMQ_LINGER, &value, &optsize));
256  test_context_socket_close (router);
257 }
258 
260 {
265 }
266 
267 int main (void)
268 {
270 
271  UNITY_BEGIN ();
282  return UNITY_END ();
283 }
ZMQ_MAX_SOCKETS_DFLT
#define ZMQ_MAX_SOCKETS_DFLT
Definition: zmq.h:194
zmq_ctx_set
ZMQ_EXPORT int zmq_ctx_set(void *context_, int option_, int optval_)
Definition: zmq.cpp:156
TEST_ASSERT_EQUAL_STRING
#define TEST_ASSERT_EQUAL_STRING(expected, actual)
Definition: unity.h:235
test_ctx_option_blocky
void test_ctx_option_blocky()
Definition: test_ctx_options.cpp:223
ZMQ_MAX_SOCKETS
#define ZMQ_MAX_SOCKETS
Definition: zmq.h:182
ZMQ_IO_THREADS_DFLT
#define ZMQ_IO_THREADS_DFLT
Definition: zmq.h:193
UNITY_END
return UNITY_END()
ZMQ_THREAD_AFFINITY_CPU_REMOVE
#define ZMQ_THREAD_AFFINITY_CPU_REMOVE
Definition: zmq.h:189
test_ctx_option_max_sockets
void test_ctx_option_max_sockets()
Definition: test_ctx_options.cpp:181
EINVAL
#define EINVAL
Definition: errno.hpp:25
TEST_POLICY
#define TEST_POLICY
Definition: test_ctx_options.cpp:44
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
test_ctx_option_io_threads
void test_ctx_option_io_threads()
Definition: test_ctx_options.cpp:197
ZMQ_SOCKET_LIMIT
#define ZMQ_SOCKET_LIMIT
Definition: zmq.h:183
test_ctx_option_socket_limit
void test_ctx_option_socket_limit()
Definition: test_ctx_options.cpp:187
test_ctx_option_ipv6
void test_ctx_option_ipv6()
Definition: test_ctx_options.cpp:203
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
test_ctx_zero_copy
void test_ctx_zero_copy()
Definition: test_ctx_options.cpp:138
errno
int errno
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
zmq_ctx_get_ext
int zmq_ctx_get_ext(void *ctx_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:183
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
idx
static uint32_t idx(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5925
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
ZMQ_THREAD_SCHED_POLICY
#define ZMQ_THREAD_SCHED_POLICY
Definition: zmq.h:185
ZMQ_ZERO_COPY_RECV
#define ZMQ_ZERO_COPY_RECV
Definition: zmq_draft.h:71
ZMQ_THREAD_SCHED_POLICY_DFLT
#define ZMQ_THREAD_SCHED_POLICY_DFLT
Definition: zmq.h:196
testutil_unity.hpp
is_allowed_to_raise_priority
bool is_allowed_to_raise_priority()
Definition: test_ctx_options.cpp:46
prefix
static const char prefix[]
Definition: test_pair_ipc.cpp:26
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
zmq_msg_t
Definition: zmq.h:218
zmq_ctx_set_ext
int zmq_ctx_set_ext(void *ctx_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:161
test_ctx_option_ipv6_set
void test_ctx_option_ipv6_set()
Definition: test_ctx_options.cpp:216
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
ZMQ_THREAD_PRIORITY
#define ZMQ_THREAD_PRIORITY
Definition: zmq.h:184
FD_SETSIZE
#define FD_SETSIZE
Definition: deprecated-msvc/vs2015_xp/platform.hpp:10
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
ZMQ_THREAD_AFFINITY_CPU_ADD
#define ZMQ_THREAD_AFFINITY_CPU_ADD
Definition: zmq.h:188
ZMQ_LINGER
#define ZMQ_LINGER
Definition: zmq.h:288
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
zmq_ctx_get
ZMQ_EXPORT int zmq_ctx_get(void *context_, int option_)
Definition: zmq.cpp:174
WAIT_FOR_BACKGROUND_THREAD_INSPECTION
#define WAIT_FOR_BACKGROUND_THREAD_INSPECTION
Definition: test_ctx_options.cpp:9
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
ZMQ_IPV6
#define ZMQ_IPV6
Definition: zmq.h:307
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
ZMQ_IO_THREADS
#define ZMQ_IO_THREADS
Definition: zmq.h:181
push
static void push(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5890
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
ZMQ_THREAD_NAME_PREFIX
#define ZMQ_THREAD_NAME_PREFIX
Definition: zmq.h:190
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
ZMQ_MSG_T_SIZE
#define ZMQ_MSG_T_SIZE
Definition: zmq.h:187
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
test_ctx_option_msg_t_size
void test_ctx_option_msg_t_size()
Definition: test_ctx_options.cpp:208
test_ctx_option_invalid
void test_ctx_option_invalid()
Definition: test_ctx_options.cpp:259
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
main
int main(void)
Definition: test_ctx_options.cpp:267
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261
ZMQ_THREAD_PRIORITY_DFLT
#define ZMQ_THREAD_PRIORITY_DFLT
Definition: zmq.h:195
ZMQ_BLOCKY
#define ZMQ_BLOCKY
Definition: zmq.h:331
test_ctx_thread_opts
void test_ctx_thread_opts()
Definition: test_ctx_options.cpp:54


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