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 #include "../src/utils/atomic.h"
33 #include "../src/utils/atomic.c"
34
35 /* Stress test the IPC transport. */
36
37 #ifndef NN_HAVE_WSL
38
39 #define THREAD_COUNT 10
40 #define TEST_LOOPS 10
41 #define SOCKET_ADDRESS "ipc://test-stress.ipc"
42
server(NN_UNUSED void * arg)43 static void server(NN_UNUSED void *arg)
44 {
45 int bytes;
46 int count;
47 int sock = nn_socket(AF_SP, NN_PULL);
48 int res[TEST_LOOPS];
49 nn_assert(sock >= 0);
50 nn_assert(nn_bind(sock, SOCKET_ADDRESS) >= 0);
51 count = THREAD_COUNT * TEST_LOOPS;
52 memset(res, 0, sizeof (res));
53 while (count > 0)
54 {
55 char *buf = NULL;
56 int tid;
57 int num;
58 bytes = nn_recv(sock, &buf, NN_MSG, 0);
59 nn_assert(bytes >= 0);
60 nn_assert(bytes >= 2);
61 nn_assert(buf[0] >= 'A' && buf[0] <= 'Z');
62 nn_assert(buf[1] >= 'a' && buf[0] <= 'z');
63 tid = buf[0]-'A';
64 num = buf[1]-'a';
65 nn_assert(tid < THREAD_COUNT);
66 nn_assert(res[tid] == num);
67 res[tid]=num+1;
68 nn_freemsg(buf);
69 count--;
70 }
71 nn_close(sock);
72 }
73
client(void * arg)74 static void client(void *arg)
75 {
76 intptr_t id = (intptr_t)arg;
77 int bytes;
78 char msg[3];
79 size_t sz_msg;
80 int i;
81
82 msg[0] = 'A' + id%26;
83 msg[1] = 'a';
84 msg[2] = '\0';
85 /* '\0' too */
86 sz_msg = strlen (msg) + 1;
87
88 for (i = 0; i < TEST_LOOPS; i++) {
89 int cli_sock = nn_socket (AF_SP, NN_PUSH);
90 msg[1] = 'a' + i%26;
91 nn_assert (cli_sock >= 0);
92 nn_assert (nn_connect (cli_sock, SOCKET_ADDRESS) >= 0);
93 /* Give time to allow for connect to establish. */
94 nn_sleep (50);
95 bytes = nn_send (cli_sock, msg, sz_msg, 0);
96 /* This would better be handled via semaphore or condvar. */
97 nn_sleep (100);
98 nn_assert ((size_t)bytes == sz_msg);
99 nn_close (cli_sock);
100 }
101 }
102
main()103 int main()
104 {
105 int i;
106 struct nn_thread srv_thread;
107 struct nn_thread cli_threads[THREAD_COUNT];
108 /* Stress the shutdown algorithm. */
109 nn_thread_init(&srv_thread, server, NULL);
110
111 for (i = 0; i != THREAD_COUNT; ++i)
112 nn_thread_init(&cli_threads[i], client, (void *)(intptr_t)i);
113 for (i = 0; i != THREAD_COUNT; ++i)
114 nn_thread_term(&cli_threads[i]);
115
116 return 0;
117 }
118
119 #else
120
main()121 int main()
122 {
123 return (0);
124 }
125 #endif
126