1 /*
2 Copyright (c) 2012 Martin Sustrik All rights reserved.
3 Copyright 2017 Garrett D'Amore <garrett@damore.org>
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/pair.h"
26 #include "../src/pubsub.h"
27 #include "../src/pipeline.h"
28 #include "../src/ipc.h"
29
30 #include "testutil.h"
31 #include "../src/utils/thread.c"
32
33 #ifndef NN_HAVE_WSL
34
35 /* Stress test the IPC transport. */
36
37 #define THREAD_COUNT 100
38 #define TEST2_THREAD_COUNT 10
39 #define MESSAGES_PER_THREAD 10
40 #define TEST_LOOPS 10
41 #define SOCKET_ADDRESS "ipc://test-shutdown.ipc"
42
43 volatile int active;
44
routine(NN_UNUSED void * arg)45 static void routine (NN_UNUSED void *arg)
46 {
47 int s;
48
49 s = nn_socket (AF_SP, NN_SUB);
50 if (s < 0 && nn_errno () == EMFILE)
51 return;
52 errno_assert (s >= 0);
53 test_connect (s, SOCKET_ADDRESS);
54 test_close (s);
55 }
56
routine2(NN_UNUSED void * arg)57 static void routine2 (NN_UNUSED void *arg)
58 {
59 int s;
60 int i;
61
62 s = test_socket (AF_SP, NN_PULL);
63
64 for (i = 0; i < 10; ++i) {
65 test_connect (s, SOCKET_ADDRESS);
66 }
67
68 for (i = 0; i < MESSAGES_PER_THREAD; ++i) {
69 test_recv (s, "hello");
70 }
71
72 test_close (s);
73 active --;
74 }
75
76 #endif /* NN_HAVE_WSL */
77
main()78 int main ()
79 {
80
81 #ifndef NN_HAVE_WSL
82 int sb;
83 int i;
84 int j;
85 struct nn_thread threads [THREAD_COUNT];
86
87 /* Stress the shutdown algorithm. */
88
89 #if defined(SIGPIPE) && defined(SIG_IGN)
90 signal (SIGPIPE, SIG_IGN);
91 #endif
92
93 sb = test_socket (AF_SP, NN_PUB);
94 test_bind (sb, SOCKET_ADDRESS);
95
96 for (j = 0; j != TEST_LOOPS; ++j) {
97 for (i = 0; i != THREAD_COUNT; ++i)
98 nn_thread_init (&threads [i], routine, NULL);
99 for (i = 0; i != THREAD_COUNT; ++i)
100 nn_thread_term (&threads [i]);
101 }
102
103 test_close (sb);
104
105 /* Test race condition of sending message while socket shutting down */
106
107 sb = test_socket (AF_SP, NN_PUSH);
108 test_bind (sb, SOCKET_ADDRESS);
109
110 for (j = 0; j != TEST_LOOPS; ++j) {
111 for (i = 0; i != TEST2_THREAD_COUNT; ++i)
112 nn_thread_init (&threads [i], routine2, NULL);
113 active = TEST2_THREAD_COUNT;
114
115 while (active) {
116 (void) nn_send (sb, "hello", 5, NN_DONTWAIT);
117 nn_sleep (0);
118 }
119
120 for (i = 0; i != TEST2_THREAD_COUNT; ++i)
121 nn_thread_term (&threads [i]);
122 }
123
124 test_close (sb);
125 #endif /* NN_HAVE_WSL */
126
127 return 0;
128 }
129
130