test_shutdown_stress.cpp 1.97 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
15

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

20
#include "testutil.hpp"
21

22
#define THREAD_COUNT 100
23

24
extern "C"
25
{
26
    static void worker (void *s)
27 28
    {
        int rc;
29

30
        rc = zmq_connect (s, "tcp://127.0.0.1:5560");
31
        assert (rc == 0);
32

33 34 35 36
        //  Start closing the socket while the connecting process is underway.
        rc = zmq_close (s);
        assert (rc == 0);
    }
37 38
}

39
int main (void)
40
{
41
    setup_test_environment();
42 43 44 45 46
    void *s1;
    void *s2;
    int i;
    int j;
    int rc;
47
    void* threads [THREAD_COUNT];
48 49 50 51

    for (j = 0; j != 10; j++) {

        //  Check the shutdown with many parallel I/O threads.
52
        void *ctx = zmq_ctx_new ();
53
        assert (ctx);
54
        zmq_ctx_set (ctx, ZMQ_IO_THREADS, 7);
55

56
        s1 = zmq_socket (ctx, ZMQ_PUB);
57 58
        assert (s1);

59
        rc = zmq_bind (s1, "tcp://127.0.0.1:5560");
60 61 62 63 64
        assert (rc == 0);

        for (i = 0; i != THREAD_COUNT; i++) {
            s2 = zmq_socket (ctx, ZMQ_SUB);
            assert (s2);
65
            threads [i] = zmq_threadstart(&worker, s2);
66 67 68
        }

        for (i = 0; i != THREAD_COUNT; i++) {
69
            zmq_threadclose(threads [i]);
70 71 72 73 74
        }

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

75
        rc = zmq_ctx_term (ctx);
76 77 78 79 80
        assert (rc == 0);
    }

    return 0;
}