2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief threadpool unit tests
23 * \author Mark Michelson <mmichelson@digium.com>
28 <depend>TEST_FRAMEWORK</depend>
29 <support_level>core</support_level>
34 #include "asterisk/test.h"
35 #include "asterisk/threadpool.h"
36 #include "asterisk/module.h"
37 #include "asterisk/lock.h"
38 #include "asterisk/astobj2.h"
39 #include "asterisk/logger.h"
41 struct test_listener_data {
52 static void *test_alloc(struct ast_threadpool_listener *listener)
54 struct test_listener_data *tld = ast_calloc(1, sizeof(*tld));
58 ast_mutex_init(&tld->lock);
59 ast_cond_init(&tld->cond, NULL);
63 static void test_state_changed(struct ast_threadpool *pool,
64 struct ast_threadpool_listener *listener,
68 struct test_listener_data *tld = listener->private_data;
69 SCOPED_MUTEX(lock, &tld->lock);
70 ast_log(LOG_NOTICE, "State changed: num_active: %d, num_idle: %d\n", active_threads, idle_threads);
71 tld->num_active = active_threads;
72 tld->num_idle = idle_threads;
73 ast_cond_signal(&tld->cond);
76 static void test_task_pushed(struct ast_threadpool *pool,
77 struct ast_threadpool_listener *listener,
80 struct test_listener_data *tld = listener->private_data;
81 SCOPED_MUTEX(lock, &tld->lock);
84 tld->was_empty = was_empty;
85 ast_cond_signal(&tld->cond);
88 static void test_emptied(struct ast_threadpool *pool,
89 struct ast_threadpool_listener *listener)
91 struct test_listener_data *tld = listener->private_data;
92 SCOPED_MUTEX(lock, &tld->lock);
93 tld->empty_notice = 1;
94 ast_cond_signal(&tld->cond);
97 static void test_destroy(void *private_data)
99 struct test_listener_data *tld = private_data;
100 ast_debug(1, "Poop\n");
101 ast_cond_destroy(&tld->cond);
102 ast_mutex_destroy(&tld->lock);
106 static const struct ast_threadpool_listener_callbacks test_callbacks = {
108 .state_changed = test_state_changed,
109 .task_pushed = test_task_pushed,
110 .emptied = test_emptied,
111 .destroy = test_destroy,
114 struct simple_task_data {
120 static struct simple_task_data *simple_task_data_alloc(void)
122 struct simple_task_data *std = ast_calloc(1, sizeof(*std));
127 ast_mutex_init(&std->lock);
128 ast_cond_init(&std->cond, NULL);
132 static int simple_task(void *data)
134 struct simple_task_data *std = data;
135 SCOPED_MUTEX(lock, &std->lock);
136 std->task_executed = 1;
137 ast_cond_signal(&std->cond);
141 #define WAIT_WHILE(tld, condition) \
143 ast_mutex_lock(&tld->lock);\
144 while ((condition)) {\
145 ast_cond_wait(&tld->cond, &tld->lock);\
147 ast_mutex_unlock(&tld->lock);\
150 static void wait_for_task_pushed(struct ast_threadpool_listener *listener)
152 struct test_listener_data *tld = listener->private_data;
153 struct timeval start = ast_tvnow();
154 struct timespec end = {
155 .tv_sec = start.tv_sec + 5,
156 .tv_nsec = start.tv_usec * 1000
158 SCOPED_MUTEX(lock, &tld->lock);
160 while (!tld->task_pushed) {
161 ast_cond_timedwait(&tld->cond, lock, &end);
165 static enum ast_test_result_state wait_for_completion(struct simple_task_data *std)
167 struct timeval start = ast_tvnow();
168 struct timespec end = {
169 .tv_sec = start.tv_sec + 5,
170 .tv_nsec = start.tv_usec * 1000
172 enum ast_test_result_state res = AST_TEST_PASS;
173 SCOPED_MUTEX(lock, &std->lock);
175 while (!std->task_executed) {
176 ast_cond_timedwait(&std->cond, lock, &end);
179 if (!std->task_executed) {
185 static enum ast_test_result_state wait_for_empty_notice(struct test_listener_data *tld)
187 struct timeval start = ast_tvnow();
188 struct timespec end = {
189 .tv_sec = start.tv_sec + 5,
190 .tv_nsec = start.tv_usec * 1000
192 enum ast_test_result_state res = AST_TEST_PASS;
193 SCOPED_MUTEX(lock, &tld->lock);
195 while (!tld->empty_notice) {
196 ast_cond_timedwait(&tld->cond, lock, &end);
199 if (!tld->empty_notice) {
206 static enum ast_test_result_state listener_check(
207 struct ast_test *test,
208 struct ast_threadpool_listener *listener,
216 struct test_listener_data *tld = listener->private_data;
217 enum ast_test_result_state res = AST_TEST_PASS;
219 if (tld->task_pushed != task_pushed) {
220 ast_test_status_update(test, "Expected task %sto be pushed, but it was%s\n",
221 task_pushed ? "" : "not ", tld->task_pushed ? "" : " not");
224 if (tld->was_empty != was_empty) {
225 ast_test_status_update(test, "Expected %sto be empty, but it was%s\n",
226 was_empty ? "" : "not ", tld->was_empty ? "" : " not");
229 if (tld->num_tasks!= num_tasks) {
230 ast_test_status_update(test, "Expected %d tasks to be pushed, but got %d\n",
231 num_tasks, tld->num_tasks);
234 if (tld->num_active != num_active) {
235 ast_test_status_update(test, "Expected %d active threads, but got %d\n",
236 num_active, tld->num_active);
239 if (tld->num_idle != num_idle) {
240 ast_test_status_update(test, "Expected %d idle threads, but got %d\n",
241 num_idle, tld->num_idle);
244 if (tld->empty_notice != empty_notice) {
245 ast_test_status_update(test, "Expected %s empty notice, but got %s\n",
246 was_empty ? "an" : "no", tld->task_pushed ? "one" : "none");
253 AST_TEST_DEFINE(threadpool_push)
255 struct ast_threadpool *pool = NULL;
256 struct ast_threadpool_listener *listener = NULL;
257 struct simple_task_data *std = NULL;
258 enum ast_test_result_state res = AST_TEST_FAIL;
262 info->name = "threadpool_push";
263 info->category = "/main/threadpool/";
264 info->summary = "Test task";
266 "Basic threadpool test";
267 return AST_TEST_NOT_RUN;
272 listener = ast_threadpool_listener_alloc(&test_callbacks);
274 return AST_TEST_FAIL;
277 pool = ast_threadpool_create(listener, 0);
282 std = simple_task_data_alloc();
287 ast_threadpool_push(pool, simple_task, std);
289 wait_for_task_pushed(listener);
291 res = listener_check(test, listener, 1, 1, 1, 0, 0, 0);
295 ast_threadpool_shutdown(pool);
297 ao2_cleanup(listener);
302 AST_TEST_DEFINE(threadpool_thread_creation)
304 struct ast_threadpool *pool = NULL;
305 struct ast_threadpool_listener *listener = NULL;
306 enum ast_test_result_state res = AST_TEST_FAIL;
307 struct test_listener_data *tld;
311 info->name = "threadpool_thread_creation";
312 info->category = "/main/threadpool/";
313 info->summary = "Test threadpool thread creation";
315 "Ensure that threads can be added to a threadpool";
316 return AST_TEST_NOT_RUN;
321 listener = ast_threadpool_listener_alloc(&test_callbacks);
323 return AST_TEST_FAIL;
325 tld = listener->private_data;
327 pool = ast_threadpool_create(listener, 0);
332 /* Now let's create a thread. It should start active, then go
335 ast_threadpool_set_size(pool, 1);
337 WAIT_WHILE(tld, tld->num_idle == 0);
339 res = listener_check(test, listener, 0, 0, 0, 0, 1, 0);
343 ast_threadpool_shutdown(pool);
345 ao2_cleanup(listener);
349 AST_TEST_DEFINE(threadpool_thread_destruction)
351 struct ast_threadpool *pool = NULL;
352 struct ast_threadpool_listener *listener = NULL;
353 enum ast_test_result_state res = AST_TEST_FAIL;
354 struct test_listener_data *tld;
358 info->name = "threadpool_thread_destruction";
359 info->category = "/main/threadpool/";
360 info->summary = "Test threadpool thread destruction";
362 "Ensure that threads are properly destroyed in a threadpool";
363 return AST_TEST_NOT_RUN;
368 listener = ast_threadpool_listener_alloc(&test_callbacks);
370 return AST_TEST_FAIL;
372 tld = listener->private_data;
374 pool = ast_threadpool_create(listener, 0);
379 ast_threadpool_set_size(pool, 3);
381 WAIT_WHILE(tld, tld->num_idle < 3);
383 res = listener_check(test, listener, 0, 0, 0, 0, 3, 0);
384 if (res == AST_TEST_FAIL) {
388 ast_threadpool_set_size(pool, 2);
390 WAIT_WHILE(tld, tld->num_idle > 2);
392 res = listener_check(test, listener, 0, 0, 0, 0, 2, 0);
396 ast_threadpool_shutdown(pool);
398 ao2_cleanup(listener);
402 AST_TEST_DEFINE(threadpool_one_task_one_thread)
404 struct ast_threadpool *pool = NULL;
405 struct ast_threadpool_listener *listener = NULL;
406 struct simple_task_data *std = NULL;
407 enum ast_test_result_state res = AST_TEST_FAIL;
408 struct test_listener_data *tld;
412 info->name = "threadpool_one_task_one_thread";
413 info->category = "/main/threadpool/";
414 info->summary = "Test a single task with a single thread";
416 "Push a task into an empty threadpool, then add a thread to the pool.";
417 return AST_TEST_NOT_RUN;
422 listener = ast_threadpool_listener_alloc(&test_callbacks);
424 return AST_TEST_FAIL;
426 tld = listener->private_data;
428 pool = ast_threadpool_create(listener, 0);
433 std = simple_task_data_alloc();
438 ast_threadpool_push(pool, simple_task, std);
440 ast_threadpool_set_size(pool, 1);
442 /* Threads added to the pool are active when they start,
443 * so the newly-created thread should immediately execute
446 res = wait_for_completion(std);
447 if (res == AST_TEST_FAIL) {
451 res = wait_for_empty_notice(tld);
452 if (res == AST_TEST_FAIL) {
456 /* After completing the task, the thread should go idle */
457 WAIT_WHILE(tld, tld->num_idle == 0);
459 res = listener_check(test, listener, 1, 1, 1, 0, 1, 1);
463 ast_threadpool_shutdown(pool);
465 ao2_cleanup(listener);
471 AST_TEST_DEFINE(threadpool_one_thread_one_task)
473 struct ast_threadpool *pool = NULL;
474 struct ast_threadpool_listener *listener = NULL;
475 struct simple_task_data *std = NULL;
476 enum ast_test_result_state res = AST_TEST_FAIL;
477 struct test_listener_data *tld;
481 info->name = "threadpool_one_thread_one_task";
482 info->category = "/main/threadpool/";
483 info->summary = "Test a single thread with a single task";
485 "Add a thread to the pool and then push a task to it.";
486 return AST_TEST_NOT_RUN;
491 listener = ast_threadpool_listener_alloc(&test_callbacks);
493 return AST_TEST_FAIL;
495 tld = listener->private_data;
497 pool = ast_threadpool_create(listener, 0);
502 std = simple_task_data_alloc();
507 ast_threadpool_set_size(pool, 1);
509 WAIT_WHILE(tld, tld->num_idle == 0);
511 ast_threadpool_push(pool, simple_task, std);
513 res = wait_for_completion(std);
514 if (res == AST_TEST_FAIL) {
518 res = wait_for_empty_notice(tld);
519 if (res == AST_TEST_FAIL) {
523 /* After completing the task, the thread should go idle */
524 WAIT_WHILE(tld, tld->num_idle == 0);
526 res = listener_check(test, listener, 1, 1, 1, 0, 1, 1);
530 ast_threadpool_shutdown(pool);
532 ao2_cleanup(listener);
538 AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks)
540 struct ast_threadpool *pool = NULL;
541 struct ast_threadpool_listener *listener = NULL;
542 struct simple_task_data *std1 = NULL;
543 struct simple_task_data *std2 = NULL;
544 struct simple_task_data *std3 = NULL;
545 enum ast_test_result_state res = AST_TEST_FAIL;
546 struct test_listener_data *tld;
550 info->name = "threadpool_one_thread_multiple_tasks";
551 info->category = "/main/threadpool/";
552 info->summary = "Test a single thread with multiple tasks";
554 "Add a thread to the pool and then push three tasks to it.";
555 return AST_TEST_NOT_RUN;
560 listener = ast_threadpool_listener_alloc(&test_callbacks);
562 return AST_TEST_FAIL;
564 tld = listener->private_data;
566 pool = ast_threadpool_create(listener, 0);
571 std1 = simple_task_data_alloc();
572 std2 = simple_task_data_alloc();
573 std3 = simple_task_data_alloc();
574 if (!std1 || !std2 || !std3) {
578 ast_threadpool_set_size(pool, 1);
580 WAIT_WHILE(tld, tld->num_idle == 0);
582 ast_threadpool_push(pool, simple_task, std1);
583 ast_threadpool_push(pool, simple_task, std2);
584 ast_threadpool_push(pool, simple_task, std3);
586 res = wait_for_completion(std1);
587 if (res == AST_TEST_FAIL) {
590 res = wait_for_completion(std2);
591 if (res == AST_TEST_FAIL) {
594 res = wait_for_completion(std3);
595 if (res == AST_TEST_FAIL) {
599 res = wait_for_empty_notice(tld);
600 if (res == AST_TEST_FAIL) {
604 WAIT_WHILE(tld, tld->num_idle == 0);
606 res = listener_check(test, listener, 1, 0, 3, 0, 1, 1);
610 ast_threadpool_shutdown(pool);
612 ao2_cleanup(listener);
620 static int unload_module(void)
622 ast_test_unregister(threadpool_push);
623 ast_test_unregister(threadpool_thread_creation);
624 ast_test_unregister(threadpool_thread_destruction);
625 ast_test_unregister(threadpool_one_task_one_thread);
626 ast_test_unregister(threadpool_one_thread_one_task);
627 ast_test_unregister(threadpool_one_thread_multiple_tasks);
631 static int load_module(void)
633 ast_test_register(threadpool_push);
634 ast_test_register(threadpool_thread_creation);
635 ast_test_register(threadpool_thread_destruction);
636 ast_test_register(threadpool_one_task_one_thread);
637 ast_test_register(threadpool_one_thread_one_task);
638 ast_test_register(threadpool_one_thread_multiple_tasks);
639 return AST_MODULE_LOAD_SUCCESS;
642 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "threadpool test module");