summaryrefslogtreecommitdiff
path: root/tests/ws_async_shutdown.c
blob: bf2a3ec9149c1effd14788cbb01653a5069a71d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
    Copyright (c) 2012 Martin Sustrik  All rights reserved.
    Copyright (c) 2015-2016 Jack R. Dunaway. All rights reserved.
    Copyright 2017 Garrett D'Amore <garrett@damore.org>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom
    the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
*/

#include "../src/nn.h"
#include "../src/pubsub.h"

#include "testutil.h"
#include "../src/utils/thread.c"

static char socket_address [128];

/*  Test condition of closing sockets that are blocking in another thread. */

#define TEST_LOOPS 5
#define TEST_THREADS 10

static void sender (void *arg)
{
    int s;
    int rc;

    s = *(int *)arg;
    for (;;) {
        rc = nn_send (s, "hello", 5, 0);
        if (rc < 0) {
            break;
        }
        nn_sleep(10);
    }
}

static void routine (void *arg)
{
    int s;
    int rc;
    char msg[10];

    nn_assert (arg);

    s = *(int *)arg;

    for (;;) {
        rc = nn_recv (s, &msg, sizeof(msg), 0);
        if (rc >= 0) {
            continue;
        }

        nn_assert (rc == -1);

        /*  A timeout is OK since PUB/SUB is lossy. */
        if (nn_errno () == ETIMEDOUT) {
            continue;
        }
        break;
    }
    /*  Socket is expected to be closed by caller.  */
    errno_assert (nn_errno () == EBADF);
}

int main (int argc, const char *argv[])
{
    int i;
    int j;
    int s;
    int sb;
    int rcvtimeo = 10;
    int sndtimeo = 0;
    int sockets [TEST_THREADS];
    struct nn_thread threads [TEST_THREADS];
    struct nn_thread send_thr;

    test_addr_from (socket_address, "ws", "127.0.0.1",
        get_test_port (argc, argv));

    sb = test_socket (AF_SP, NN_PUB);
    test_bind (sb, socket_address);
    test_setsockopt (sb, NN_SOL_SOCKET, NN_SNDTIMEO,
        &sndtimeo, sizeof (sndtimeo));
    nn_thread_init (&send_thr, sender, &sb);

    for (i = 0; i != TEST_LOOPS; ++i) {


        for (j = 0; j < TEST_THREADS; j++){
            s = test_socket (AF_SP, NN_SUB);
            test_setsockopt (s, NN_SOL_SOCKET, NN_RCVTIMEO,
                &rcvtimeo, sizeof (rcvtimeo));
            test_setsockopt (s, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
            test_connect (s, socket_address);
            sockets [j] = s;
            nn_thread_init (&threads [j], routine, &sockets [j]);
        }

        /*  Allow all threads a bit of time to connect. */
        nn_sleep (100);

        for (j = 0; j < TEST_THREADS; j++) {
            test_close (sockets [j]);
            nn_thread_term (&threads [j]);
        }
    }

    test_close (sb);
    nn_thread_term (&send_thr);

    return 0;
}