test_shutdown_stress.cpp 2.24 KB
Newer Older
1
/*
2 3 4
    Copyright (c) 2010-2011 250bpm s.r.o.
    Copyright (c) 2011 iMatix Corporation
    Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
10 11 12 13 14 15
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU Lesser General Public License for more details.
17

18
    You should have received a copy of the GNU Lesser General Public License
19 20 21 22 23 24
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../include/zmq.h"
#include <pthread.h>
#include <stddef.h>
25
#include <stdio.h>
26

27 28 29
#undef NDEBUG
#include <assert.h>

30
#define THREAD_COUNT 100
31

32
extern "C"
33
{
34 35 36
    static void *worker (void *s)
    {
        int rc;
37

38
        rc = zmq_connect (s, "tcp://127.0.0.1:5560");
39
        assert (rc == 0);
40

41 42 43
        //  Start closing the socket while the connecting process is underway.
        rc = zmq_close (s);
        assert (rc == 0);
44

45 46
        return NULL;
    }
47 48
}

49
int main (void)
50 51 52 53 54 55 56 57 58
{
    void *ctx;
    void *s1;
    void *s2;
    int i;
    int j;
    int rc;
    pthread_t threads [THREAD_COUNT];

59 60
    fprintf (stderr, "test_shutdown_stress running...\n");

61 62 63 64 65 66
    for (j = 0; j != 10; j++) {

        //  Check the shutdown with many parallel I/O threads.
        ctx = zmq_init (7);
        assert (ctx);

67
        s1 = zmq_socket (ctx, ZMQ_PUB);
68 69
        assert (s1);

70
        rc = zmq_bind (s1, "tcp://127.0.0.1:5560");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        assert (rc == 0);

        for (i = 0; i != THREAD_COUNT; i++) {
            s2 = zmq_socket (ctx, ZMQ_SUB);
            assert (s2);
            rc = pthread_create (&threads [i], NULL, worker, s2);
            assert (rc == 0);
        }

        for (i = 0; i != THREAD_COUNT; i++) {
            rc = pthread_join (threads [i], NULL);
            assert (rc == 0);
        }

        rc = zmq_close (s1);
        assert (rc == 0);

        rc = zmq_term (ctx);
        assert (rc == 0);
    }

    return 0;
}