Debian Codegraph @

Xpjproject/pjnath/src/pjnath-test/concur_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21 #if INCLUDE_CONCUR_TEST
22
23 #define THIS_FILE "concur_test.c"
24
25 /****************************************************************************/
26 #define WORKER_THREAD_CNT 4
27 #define SERVER_THREAD_CNT 4
28 #define MAX_SOCK_CLIENTS (PJ_IOQUEUE_MAX_HANDLES/2)
29
30 struct stun_test_session
31 {
32 pj_stun_config stun_cfg;
33
34 pj_lock_t *lock;
35
36 pj_thread_t *worker_threads[WORKER_THREAD_CNT];
37
38 pj_sock_t server_sock;
39 int server_port;
40 pj_thread_t *server_threads[SERVER_THREAD_CNT];
41 pj_event_t *server_event;
42
43 pj_bool_t thread_quit_flag;
44
45 /* Test parameters: */
46 struct {
47 int client_got_response;
48
49 pj_bool_t server_wait_for_event;
50 pj_bool_t server_drop_request;
51 int client_sleep_after_start;
52 int client_sleep_before_destroy;
53 } param;
54 };
55
56 static int server_thread_proc(void *p)
57 {
58 struct stun_test_session *test_sess = (struct stun_test_session*)p;
59 pj_pool_t *pool;
60 pj_status_t status;
61
62 PJ_LOG(4,(THIS_FILE, "Server thread running"));
63
64 pool = pj_pool_create(test_sess->stun_cfg.pf, "server", 512, 512, NULL);
65
66 while (!test_sess->thread_quit_flag) {
67 pj_time_val timeout = {0, 10};
68 pj_fd_set_t rdset;
69 int n;
70
71 /* Serve client */
72 PJ_FD_ZERO(&rdset);
73 PJ_FD_SET(test_sess->server_sock, &rdset);
74 n = pj_sock_select((int)test_sess->server_sock+1, &rdset,
75 NULL, NULL, &timeout);
76 if (n==1 && PJ_FD_ISSET(test_sess->server_sock, &rdset)) {
77 pj_uint8_t pkt[512];
78 pj_ssize_t pkt_len;
79 pj_size_t res_len;
80 pj_sockaddr client_addr;
81 int addr_len;
82
83 pj_stun_msg *stun_req, *stun_res;
84
85 pj_pool_reset(pool);
86
87 /* Got query */
88 pkt_len = sizeof(pkt);
89 addr_len = sizeof(client_addr);
90 status = pj_sock_recvfrom(test_sess->server_sock, pkt, &pkt_len,
91 0, &client_addr, &addr_len);
92 if (status != PJ_SUCCESS) {
93 continue;
94 }
95
96 status = pj_stun_msg_decode(pool, pkt, pkt_len,
97 PJ_STUN_IS_DATAGRAM,
98 &stun_req, NULL, NULL);
99 if (status != PJ_SUCCESS) {
100 PJ_PERROR(1,(THIS_FILE, status, "STUN request decode error"));
101 continue;
102 }
103
104 status = pj_stun_msg_create_response(pool, stun_req,
105 PJ_STUN_SC_BAD_REQUEST, NULL,
106 &stun_res);
107 if (status != PJ_SUCCESS) {
108 PJ_PERROR(1,(THIS_FILE, status, "STUN create response error"));
109 continue;
110 }
111
112 status = pj_stun_msg_encode(stun_res, pkt, sizeof(pkt), 0,
113 NULL, &res_len);
114 if (status != PJ_SUCCESS) {
115 PJ_PERROR(1,(THIS_FILE, status, "STUN encode error"));
116 continue;
117 }
118
119 /* Ignore request */
120 if (test_sess->param.server_drop_request)
121 continue;
122
123 /* Wait for signal to continue */
124 if (test_sess->param.server_wait_for_event)
125 pj_event_wait(test_sess->server_event);
126
127 pkt_len = res_len;
128 pj_sock_sendto(test_sess->server_sock, pkt, &pkt_len, 0,
129 &client_addr, pj_sockaddr_get_len(&client_addr));
130 }
131 }
132
133 pj_pool_release(pool);
134
135 PJ_LOG(4,(THIS_FILE, "Server thread quitting"));
136 return 0;
137 }
138
139 static int worker_thread_proc(void *p)
140 {
141 struct stun_test_session *test_sess = (struct stun_test_session*)p;
142
143 PJ_LOG(4,(THIS_FILE, "Worker thread running"));
144
145 while (!test_sess->thread_quit_flag) {
146 pj_time_val timeout = {0, 10};
147 pj_timer_heap_poll(test_sess->stun_cfg.timer_heap, NULL);
148 pj_ioqueue_poll(test_sess->stun_cfg.ioqueue, &timeout);
149 }
150
151 PJ_LOG(4,(THIS_FILE, "Worker thread quitting"));
152 return 0;
153 }
154
155 static pj_bool_t stun_sock_on_status(pj_stun_sock *stun_sock,
156 pj_stun_sock_op op,
157 pj_status_t status)
158 {
159 struct stun_test_session *test_sess = (struct stun_test_session*)pj_stun_sock_get_user_data(stun_sock);
160
161 PJ_UNUSED_ARG(op);
162 PJ_UNUSED_ARG(status);
163
164 test_sess->param.client_got_response++;
165 return PJ_TRUE;
166 }
167
168 static int stun_destroy_test_session(struct stun_test_session *test_sess)
169 {
170
171 unsigned i;
172 pj_stun_sock_cb stun_cb;
173 pj_status_t status;
174 pj_stun_sock *stun_sock[MAX_SOCK_CLIENTS];
175
176 pj_bzero(&stun_cb, sizeof(stun_cb));
177 stun_cb.on_status = &stun_sock_on_status;
178
179 pj_event_reset(test_sess->server_event);
180
181 /* Create all clients first */
182 for (i=0; i<MAX_SOCK_CLIENTS; ++i) {
183 char name[10];
184 pj_ansi_snprintf(name, sizeof(name), "stun%02d", i);
185 status = pj_stun_sock_create(&test_sess->stun_cfg, name, pj_AF_INET(),
186 &stun_cb, NULL, test_sess,
187 &stun_sock[i]);
188 if (status != PJ_SUCCESS) {
189 PJ_PERROR(1,(THIS_FILE, status, "Error creating stun socket"));
190 return -10;
191 }
192 }
193
194 /* Start resolution */
195 for (i=0; i<MAX_SOCK_CLIENTS; ++i) {
196 pj_str_t server_ip = pj_str("127.0.0.1");
197 status = pj_stun_sock_start(stun_sock[i], &server_ip,
198 (pj_uint16_t)test_sess->server_port, NULL);
199 if (status != PJ_SUCCESS) {
200 PJ_PERROR(1,(THIS_FILE, status, "Error starting stun socket"));
201 return -20;
202 }
203 }
204
205 /* settle down */
206 pj_thread_sleep(test_sess->param.client_sleep_after_start);
207
208 /* Resume server threads */
209 pj_event_set(test_sess->server_event);
210
211 pj_thread_sleep(test_sess->param.client_sleep_before_destroy);
212
213 /* Destroy clients */
214 for (i=0; i<MAX_SOCK_CLIENTS; ++i) {
215 status = pj_stun_sock_destroy(stun_sock[i]);
216 if (status != PJ_SUCCESS) {
217 PJ_PERROR(1,(THIS_FILE, status, "Error destroying stun socket"));
218 }
219 }
220
221 /* Give some time to ioqueue to free sockets */
222 pj_thread_sleep(PJ_IOQUEUE_KEY_FREE_DELAY);
223
224 return 0;
225 }
226
227 static int stun_destroy_test(void)
228 {
229 #define ERR(errval) { rc=errval; goto on_return; }
230 enum { LOOP = 10 };
231 struct stun_test_session test_sess;
232 pj_sockaddr bind_addr;
233 int addr_len;
234 pj_caching_pool cp;
235 pj_pool_t *pool;
236 unsigned i;
237 int rc = 0;
238
239 PJ_LOG(3,(THIS_FILE, " STUN destroy concurrency test"));
240
241 pj_bzero(&test_sess, sizeof(test_sess));
242
243 pj_caching_pool_init(&cp, NULL, 0);
244 PJ_TEST_NOT_NULL((pool=pj_pool_create(&cp.factory, "testsess",
245 512, 512, NULL)), NULL, ERR(-10));
246
247 pj_stun_config_init(&test_sess.stun_cfg, &cp.factory, 0, NULL, NULL);
248
249 PJ_TEST_SUCCESS(pj_timer_heap_create(pool, 1023,
250 &test_sess.stun_cfg.timer_heap),
251 NULL, ERR(-20));
252
253 PJ_TEST_SUCCESS(pj_lock_create_recursive_mutex(pool, NULL,
254 &test_sess.lock),
255 NULL, ERR(-30));
256
257 pj_timer_heap_set_lock(test_sess.stun_cfg.timer_heap, test_sess.lock,
258 PJ_TRUE);
259
260 PJ_TEST_SUCCESS(pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES,
261 &test_sess.stun_cfg.ioqueue),
262 NULL, ERR(-40));
263
264 PJ_TEST_SUCCESS(pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
265 &test_sess.server_sock),
266 NULL, ERR(-50));
267 pj_sockaddr_init(pj_AF_INET(), &bind_addr, NULL, 0);
268 PJ_TEST_SUCCESS(pj_sock_bind(test_sess.server_sock, &bind_addr,
269 pj_sockaddr_get_len(&bind_addr)),
270 NULL, ERR(-60));
271
272 /* Set socket to nonblocking to avoid stuck in recv/recvfrom() on concurrent events */
273 app_set_sock_nb(test_sess.server_sock);
274
275 addr_len = sizeof(bind_addr);
276 PJ_TEST_SUCCESS(pj_sock_getsockname(test_sess.server_sock, &bind_addr,
277 &addr_len),
278 NULL, ERR(-70));
279
280 test_sess.server_port = pj_sockaddr_get_port(&bind_addr);
281
282 PJ_TEST_SUCCESS(pj_event_create(pool, NULL, PJ_TRUE, PJ_FALSE,
283 &test_sess.server_event),
284 NULL, ERR(-80));
285
286 for (i=0; i<SERVER_THREAD_CNT; ++i) {
287 PJ_TEST_SUCCESS(pj_thread_create(pool, NULL,
288 &server_thread_proc, &test_sess,
289 0, 0, &test_sess.server_threads[i]),
290 NULL, ERR(-90));
291 }
292
293 for (i=0; i<WORKER_THREAD_CNT; ++i) {
294 PJ_TEST_SUCCESS(pj_thread_create(pool, NULL,
295 &worker_thread_proc, &test_sess,
296 0, 0, &test_sess.worker_threads[i]),
297 NULL, ERR(-100));
298 }
299
300 /* Test 1: Main thread calls destroy while callback is processing response */
301 PJ_LOG(3,(THIS_FILE, " Destroy in main thread while callback is running"));
302 for (i=0; i<LOOP; ++i) {
303 int sleep = pj_rand() % 5;
304
305 PJ_LOG(3,(THIS_FILE, " Try %-3d of %d", i+1, LOOP));
306
307 /* Test 1: destroy at the same time when receiving response */
308 pj_bzero(&test_sess.param, sizeof(test_sess.param));
309 test_sess.param.client_sleep_after_start = 20;
310 test_sess.param.client_sleep_before_destroy = sleep;
311 test_sess.param.server_wait_for_event = PJ_TRUE;
312 stun_destroy_test_session(&test_sess);
313 PJ_LOG(3,(THIS_FILE,
314 " stun test a: sleep delay:%d: clients with response: %d",
315 sleep, test_sess.param.client_got_response));
316
317 /* Test 2: destroy at the same time with STUN retransmit timer */
318 test_sess.param.server_drop_request = PJ_TRUE;
319 test_sess.param.client_sleep_after_start = 0;
320 test_sess.param.client_sleep_before_destroy = PJ_STUN_RTO_VALUE;
321 test_sess.param.server_wait_for_event = PJ_FALSE;
322 stun_destroy_test_session(&test_sess);
323 PJ_LOG(3,(THIS_FILE, " stun test b: retransmit concurrency"));
324
325 /* Test 3: destroy at the same time with receiving response
326 * AND STUN retransmit timer */
327 test_sess.param.client_got_response = 0;
328 test_sess.param.server_drop_request = PJ_FALSE;
329 test_sess.param.client_sleep_after_start = PJ_STUN_RTO_VALUE;
330 test_sess.param.client_sleep_before_destroy = 0;
331 test_sess.param.server_wait_for_event = PJ_TRUE;
332 stun_destroy_test_session(&test_sess);
333 PJ_LOG(3,(THIS_FILE,
334 " stun test c: clients with response: %d",
335 test_sess.param.client_got_response));
336
337 pj_thread_sleep(10);
338
339 ice_one_conc_test(&test_sess.stun_cfg, PJ_FALSE);
340
341 pj_thread_sleep(10);
342 }
343
344 rc = 0;
345
346 on_return:
347 test_sess.thread_quit_flag = PJ_TRUE;
348
349 for (i=0; i<SERVER_THREAD_CNT; ++i) {
350 if (test_sess.server_threads[i])
351 pj_thread_join(test_sess.server_threads[i]);
352 }
353
354 for (i=0; i<WORKER_THREAD_CNT; ++i) {
355 if (test_sess.worker_threads[i])
356 pj_thread_join(test_sess.worker_threads[i]);
357 }
358
359 if (test_sess.server_event)
360 pj_event_destroy(test_sess.server_event);
361 if (test_sess.server_sock != PJ_INVALID_SOCKET)
362 pj_sock_close(test_sess.server_sock);
363 if (test_sess.stun_cfg.ioqueue)
364 pj_ioqueue_destroy(test_sess.stun_cfg.ioqueue);
365 if (test_sess.stun_cfg.timer_heap)
366 pj_timer_heap_destroy(test_sess.stun_cfg.timer_heap);
367
368 if (pool)
369 pj_pool_release(pool);
370 pj_caching_pool_destroy(&cp);
371
372 PJ_LOG(3,(THIS_FILE, " Done. rc=%d", rc));
373 return rc;
374 #undef ERR
375 }
376
377
378 int concur_test(void)
379 {
380 stun_destroy_test();
381
382 return 0;
383 }
384
385 #endif /* INCLUDE_CONCUR_TEST */