libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_ctx_destroy.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 static void receiver (void *socket)
33 {
34  char buffer[16];
35  int rc = zmq_recv (socket, &buffer, sizeof (buffer), 0);
36  assert(rc == -1);
37 }
38 
40 {
41  int rc;
42 
43  // Set up our context and sockets
44  void *ctx = zmq_ctx_new ();
45  assert (ctx);
46 
47  void *socket = zmq_socket (ctx, ZMQ_PULL);
48  assert (socket);
49 
50  // Close the socket
51  rc = zmq_close (socket);
52  assert (rc == 0);
53 
54  // Destroy the context
55  rc = zmq_ctx_destroy (ctx);
56  assert (rc == 0);
57 }
58 
60 {
61  int rc;
62 
63  // Set up our context and sockets
64  void *ctx = zmq_ctx_new ();
65  assert (ctx);
66 
67  void *socket = zmq_socket (ctx, ZMQ_PULL);
68  assert (socket);
69 
70  // Spawn a thread to receive on socket
71  void *receiver_thread = zmq_threadstart (&receiver, socket);
72 
73  // Wait for thread to start up and block
75 
76  // Shutdown context, if we used destroy here we would deadlock.
77  rc = zmq_ctx_shutdown (ctx);
78  assert (rc == 0);
79 
80  // Wait for thread to finish
81  zmq_threadclose (receiver_thread);
82 
83  // Close the socket.
84  rc = zmq_close (socket);
85  assert (rc == 0);
86 
87  // Destory the context, will now not hang as we have closed the socket.
88  rc = zmq_ctx_destroy (ctx);
89  assert (rc == 0);
90 }
91 
92 int main (void)
93 {
95 
98 
99  return 0;
100 }
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
void test_ctx_shutdown()
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
void test_ctx_destroy()
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT void zmq_threadclose(void *thread)
Definition: zmq_utils.cpp:85
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
static void receiver(void *socket)
ZMQ_EXPORT int zmq_ctx_shutdown(void *context)
Definition: zmq.cpp:191
ZMQ_EXPORT int zmq_ctx_destroy(void *context)
Definition: zmq.cpp:236
int main(void)
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func, void *arg)
Definition: zmq_utils.cpp:78
#define ZMQ_PULL
Definition: zmq.h:253