libzmq master
The Intelligent Transport Layer

test_shutdown_stress.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2010-2011 250bpm s.r.o.
00003     Copyright (c) 2011 iMatix Corporation
00004     Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "../include/zmq.h"
00023 #include <assert.h>
00024 #include <pthread.h>
00025 #include <stddef.h>
00026 #include <stdio.h>
00027 
00028 #define THREAD_COUNT 100
00029 
00030 extern "C"
00031 {
00032     static void *worker (void *s)
00033     {
00034         int rc;
00035 
00036         rc = zmq_connect (s, "tcp://127.0.0.1:5560");
00037         assert (rc == 0);
00038 
00039         //  Start closing the socket while the connecting process is underway.
00040         rc = zmq_close (s);
00041         assert (rc == 0);
00042 
00043         return NULL;
00044     }
00045 }
00046 
00047 int main (int argc, char *argv [])
00048 {
00049     void *ctx;
00050     void *s1;
00051     void *s2;
00052     int i;
00053     int j;
00054     int rc;
00055     pthread_t threads [THREAD_COUNT];
00056 
00057     fprintf (stderr, "test_shutdown_stress running...\n");
00058 
00059     for (j = 0; j != 10; j++) {
00060 
00061         //  Check the shutdown with many parallel I/O threads.
00062         ctx = zmq_init (7);
00063         assert (ctx);
00064 
00065         s1 = zmq_socket (ctx, ZMQ_PUB);
00066         assert (s1);
00067 
00068         rc = zmq_bind (s1, "tcp://127.0.0.1:5560");
00069         assert (rc == 0);
00070 
00071         for (i = 0; i != THREAD_COUNT; i++) {
00072             s2 = zmq_socket (ctx, ZMQ_SUB);
00073             assert (s2);
00074             rc = pthread_create (&threads [i], NULL, worker, s2);
00075             assert (rc == 0);
00076         }
00077 
00078         for (i = 0; i != THREAD_COUNT; i++) {
00079             rc = pthread_join (threads [i], NULL);
00080             assert (rc == 0);
00081         }
00082 
00083         rc = zmq_close (s1);
00084         assert (rc == 0);
00085 
00086         rc = zmq_term (ctx);
00087         assert (rc == 0);
00088     }
00089 
00090     return 0;
00091 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines