1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3     Copyright (c) 2015-2016 Jack R. Dunaway. All rights reserved.
4 
5     Permission is hereby granted, free of charge, to any person obtaining a copy
6     of this software and associated documentation files (the "Software"),
7     to deal in the Software without restriction, including without limitation
8     the rights to use, copy, modify, merge, publish, distribute, sublicense,
9     and/or sell copies of the Software, and to permit persons to whom
10     the Software is furnished to do so, subject to the following conditions:
11 
12     The above copyright notice and this permission notice shall be included
13     in all copies or substantial portions of the Software.
14 
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21     IN THE SOFTWARE.
22 */
23 
24 #include "../src/nn.h"
25 #include "../src/pubsub.h"
26 
27 #include "testutil.h"
28 #include "../src/utils/thread.c"
29 
30 static char socket_address [128];
31 
32 /*  Test condition of closing sockets that are blocking in another thread. */
33 
34 #define TEST_LOOPS 10
35 #define TEST_THREADS 10
36 
routine(NN_UNUSED void * arg)37 static void routine (NN_UNUSED void *arg)
38 {
39     int s;
40     int rc;
41     char msg[1];
42 
43     nn_assert (arg);
44 
45     s = *(int *)arg;
46 
47     while (1) {
48         rc = nn_recv (s, &msg, sizeof(msg), 0);
49         if (rc == 0) {
50             continue;
51         }
52 
53         nn_assert (rc == -1);
54 
55         /*  A timeout is OK since PUB/SUB is lossy. */
56         if (nn_errno () == ETIMEDOUT) {
57             continue;
58         }
59         break;
60     }
61     /*  Socket is expected to be closed by caller.  */
62     errno_assert (nn_errno () == EBADF);
63 }
64 
main(int argc,const char * argv[])65 int main (int argc, const char *argv[])
66 {
67     int i;
68     int j;
69     int s;
70     int sb;
71     int rcvtimeo = 10;
72     int sndtimeo = 0;
73     int sockets [TEST_THREADS];
74     struct nn_thread threads [TEST_THREADS];
75 
76     test_addr_from (socket_address, "ws", "127.0.0.1",
77         get_test_port (argc, argv));
78 
79     for (i = 0; i != TEST_LOOPS; ++i) {
80 
81         sb = test_socket (AF_SP, NN_PUB);
82         test_bind (sb, socket_address);
83         test_setsockopt (sb, NN_SOL_SOCKET, NN_SNDTIMEO,
84             &sndtimeo, sizeof (sndtimeo));
85 
86         for (j = 0; j < TEST_THREADS; j++){
87             s = test_socket (AF_SP, NN_SUB);
88             test_setsockopt (s, NN_SOL_SOCKET, NN_RCVTIMEO,
89                 &rcvtimeo, sizeof (rcvtimeo));
90             test_setsockopt (s, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
91             test_connect (s, socket_address);
92             sockets [j] = s;
93             nn_thread_init (&threads [j], routine, &sockets [j]);
94         }
95 
96         /*  Allow all threads a bit of time to connect. */
97         nn_sleep (100);
98 
99         test_send (sb, "");
100 
101         for (j = 0; j < TEST_THREADS; j++) {
102             test_close (sockets [j]);
103             nn_thread_term (&threads [j]);
104         }
105 
106         test_close (sb);
107     }
108 
109     return 0;
110 }
111