test_ctx_destroy.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 <unity.h>
7 
8 void setUp ()
9 {
10 }
11 
12 void tearDown ()
13 {
14 }
15 
16 static void receiver (void *socket_)
17 {
18  char buffer[16];
19  int rc = zmq_recv (socket_, &buffer, sizeof (buffer), 0);
20  // TODO which error is expected here? use TEST_ASSERT_FAILURE_ERRNO instead
21  TEST_ASSERT_EQUAL_INT (-1, rc);
22 }
23 
25 {
26  // Set up our context and sockets
27  void *ctx = zmq_ctx_new ();
29 
30  void *socket = zmq_socket (ctx, ZMQ_PULL);
31  TEST_ASSERT_NOT_NULL (socket);
32 
33  // Close the socket
35 
36  // Destroy the context
38 }
39 
41 {
42  // Set up our context and sockets
43  void *ctx = zmq_ctx_new ();
45 
46  void *socket = zmq_socket (ctx, ZMQ_PULL);
47  TEST_ASSERT_NOT_NULL (socket);
48 
49  // Spawn a thread to receive on socket
50  void *receiver_thread = zmq_threadstart (&receiver, socket);
51 
52  // Wait for thread to start up and block
54 
55  // Shutdown context, if we used destroy here we would deadlock.
57 
58  // Wait for thread to finish
59  zmq_threadclose (receiver_thread);
60 
61  // Close the socket.
63 
64  // Destroy the context, will now not hang as we have closed the socket.
66 }
67 
69 {
70  // Set up our context.
71  void *ctx = zmq_ctx_new ();
73 
74  // Open a socket to start context, and close it immediately again.
75  void *socket = zmq_socket (ctx, ZMQ_PULL);
76  TEST_ASSERT_NOT_NULL (socket);
78 
79  // Shutdown context.
81 
82  // Opening socket should now fail.
85 
86  // Destroy the context.
88 }
89 
91 {
92  // Set up our context.
93  void *ctx = zmq_ctx_new ();
95 
96  // Shutdown context.
98 
99  // Opening socket should now fail.
102 
103  // Destroy the context.
105 }
106 
108 {
109  int rc = zmq_ctx_term (NULL);
110  TEST_ASSERT_EQUAL_INT (-1, rc);
112 }
113 
115 {
116  int rc = zmq_term (NULL);
117  TEST_ASSERT_EQUAL_INT (-1, rc);
119 }
120 
122 {
123  int rc = zmq_ctx_shutdown (NULL);
124  TEST_ASSERT_EQUAL_INT (-1, rc);
126 }
127 
128 #ifdef ZMQ_HAVE_POLLER
130 {
132  void *ctx;
133  void *counter;
134 };
135 
136 void run_poller (void *data_)
137 {
138  const poller_test_data_t *const poller_test_data =
139  static_cast<const poller_test_data_t *> (data_);
140 
141  void *socket =
142  zmq_socket (poller_test_data->ctx, poller_test_data->socket_type);
143  TEST_ASSERT_NOT_NULL (socket);
144 
145  void *poller = zmq_poller_new ();
146  TEST_ASSERT_NOT_NULL (poller);
147 
149  zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN));
150 
151  zmq_atomic_counter_set (poller_test_data->counter, 1);
152 
155 
157 
158  // Close the socket
160 }
161 #endif
162 
164 {
165 #ifdef ZMQ_HAVE_POLLER
166  struct poller_test_data_t poller_test_data;
167 
168  poller_test_data.socket_type = socket_type_;
169 
170  // Set up our context and sockets
171  poller_test_data.ctx = zmq_ctx_new ();
172  TEST_ASSERT_NOT_NULL (poller_test_data.ctx);
173 
174  poller_test_data.counter = zmq_atomic_counter_new ();
175  TEST_ASSERT_NOT_NULL (poller_test_data.counter);
176 
177  void *thread = zmq_threadstart (run_poller, &poller_test_data);
178  TEST_ASSERT_NOT_NULL (thread);
179 
180  while (zmq_atomic_counter_value (poller_test_data.counter) == 0) {
181  msleep (10);
182  }
183 
184  // Destroy the context
185  TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_destroy (poller_test_data.ctx));
186 
187  zmq_threadclose (thread);
188 
189  zmq_atomic_counter_destroy (&poller_test_data.counter);
190 #else
191  TEST_IGNORE_MESSAGE ("libzmq without zmq_poller_* support, ignoring test");
192 #endif
193 }
194 
196 {
197 #ifdef ZMQ_BUILD_DRAFT_API
199 #else
200  TEST_IGNORE_MESSAGE ("libzmq without DRAFT support, ignoring test");
201 #endif
202 }
203 
205 {
207 }
208 
209 int main (void)
210 {
212 
213  UNITY_BEGIN ();
221 
222  RUN_TEST (
224  RUN_TEST (
226 
227  return UNITY_END ();
228 }
zmq_ctx_shutdown
ZMQ_EXPORT int zmq_ctx_shutdown(void *context_)
Definition: zmq.cpp:147
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
zmq_threadstart
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func_, void *arg_)
Definition: zmq_utils.cpp:54
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
run_poller
void run_poller(void *data_)
Definition: test_ctx_destroy.cpp:136
ZMQ_CLIENT
#define ZMQ_CLIENT
Definition: zmq_draft.h:15
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
errno
int errno
test_ctx_shutdown
void test_ctx_shutdown()
Definition: test_ctx_destroy.cpp:40
zmq_ctx_destroy
ZMQ_EXPORT int zmq_ctx_destroy(void *context_)
Definition: zmq.cpp:212
ZMQ_POLLIN
#define ZMQ_POLLIN
Definition: zmq.h:482
test_ctx_destroy
void test_ctx_destroy()
Definition: test_ctx_destroy.cpp:24
test_ctx_shutdown_only_socket_opened_after
void test_ctx_shutdown_only_socket_opened_after()
Definition: test_ctx_destroy.cpp:90
testutil_unity.hpp
test_ctx_shutdown_socket_opened_after
void test_ctx_shutdown_socket_opened_after()
Definition: test_ctx_destroy.cpp:68
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
test_zmq_ctx_shutdown_null_fails
void test_zmq_ctx_shutdown_null_fails()
Definition: test_ctx_destroy.cpp:121
zmq_threadclose
ZMQ_EXPORT void zmq_threadclose(void *thread_)
Definition: zmq_utils.cpp:62
zmq_atomic_counter_new
ZMQ_EXPORT void * zmq_atomic_counter_new(void)
Definition: zmq_utils.cpp:255
event
struct _cl_event * event
Definition: glcorearb.h:4163
testutil.hpp
zmq_poller_event_t
Definition: zmq_draft.h:114
zmq_poller_new
void * zmq_poller_new(void)
Definition: zmq.cpp:1429
ETERM
#define ETERM
Definition: zmq.h:161
zmq_poller_wait
int zmq_poller_wait(void *poller_, zmq_poller_event_t *event_, long timeout_)
Definition: zmq.cpp:1576
setUp
void setUp()
Definition: test_ctx_destroy.cpp:8
poller_test_data_t::counter
void * counter
Definition: test_ctx_destroy.cpp:133
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
test_poller_exists_with_socket_on_zmq_ctx_term_thread_safe_socket
void test_poller_exists_with_socket_on_zmq_ctx_term_thread_safe_socket()
Definition: test_ctx_destroy.cpp:195
buffer
Definition: buffer_processor.h:43
zmq_atomic_counter_set
ZMQ_EXPORT void zmq_atomic_counter_set(void *counter_, int value_)
Definition: zmq_utils.cpp:264
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
tearDown
void tearDown()
Definition: test_ctx_destroy.cpp:12
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
TEST_IGNORE_MESSAGE
#define TEST_IGNORE_MESSAGE(message)
Definition: unity.h:103
unity.h
test_poller_exists_with_socket_on_zmq_ctx_term
void test_poller_exists_with_socket_on_zmq_ctx_term(const int socket_type_)
Definition: test_ctx_destroy.cpp:163
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
zmq_poller_add
int zmq_poller_add(void *poller_, void *s_, void *user_data_, short events_)
Definition: zmq.cpp:1509
zmq_atomic_counter_value
ZMQ_EXPORT int zmq_atomic_counter_value(void *counter_)
Definition: zmq_utils.cpp:286
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
receiver
static void receiver(void *socket_)
Definition: test_ctx_destroy.cpp:16
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
zmq_atomic_counter_destroy
ZMQ_EXPORT void zmq_atomic_counter_destroy(void **counter_p_)
Definition: zmq_utils.cpp:293
test_zmq_ctx_term_null_fails
void test_zmq_ctx_term_null_fails()
Definition: test_ctx_destroy.cpp:107
poller_test_data_t::socket_type
int socket_type
Definition: test_ctx_destroy.cpp:131
zmq_ctx_term
ZMQ_EXPORT int zmq_ctx_term(void *context_)
Definition: zmq.cpp:128
zmq_term
ZMQ_EXPORT int zmq_term(void *context_)
Definition: zmq.cpp:207
main
int main(void)
Definition: test_ctx_destroy.cpp:209
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
test_zmq_term_null_fails
void test_zmq_term_null_fails()
Definition: test_ctx_destroy.cpp:114
EFAULT
#define EFAULT
Definition: errno.hpp:17
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
poller_test_data_t
Definition: test_ctx_destroy.cpp:129
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
poller_test_data_t::ctx
void * ctx
Definition: test_ctx_destroy.cpp:132
zmq_poller_destroy
int zmq_poller_destroy(void **poller_p_)
Definition: zmq.cpp:1438
test_poller_exists_with_socket_on_zmq_ctx_term_non_thread_safe_socket
void test_poller_exists_with_socket_on_zmq_ctx_term_non_thread_safe_socket()
Definition: test_ctx_destroy.cpp:204
TEST_ASSERT_NULL
#define TEST_ASSERT_NULL(pointer)
Definition: unity.h:124


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