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 listener_check(
166 struct ast_test *test,
167 struct ast_threadpool_listener *listener,
175 struct test_listener_data *tld = listener->private_data;
176 enum ast_test_result_state res = AST_TEST_PASS;
178 if (tld->task_pushed != task_pushed) {
179 ast_test_status_update(test, "Expected task %sto be pushed, but it was%s\n",
180 task_pushed ? "" : "not ", tld->task_pushed ? "" : " not");
183 if (tld->was_empty != was_empty) {
184 ast_test_status_update(test, "Expected %sto be empty, but it was%s\n",
185 was_empty ? "" : "not ", tld->task_pushed ? "" : " not");
188 if (tld->num_tasks!= num_tasks) {
189 ast_test_status_update(test, "Expected %d tasks to be pushed, but got %d\n",
190 num_tasks, tld->num_tasks);
193 if (tld->num_active != num_active) {
194 ast_test_status_update(test, "Expected %d active threads, but got %d\n",
195 num_active, tld->num_active);
198 if (tld->num_idle != num_idle) {
199 ast_test_status_update(test, "Expected %d idle threads, but got %d\n",
200 num_idle, tld->num_idle);
203 if (tld->empty_notice != empty_notice) {
204 ast_test_status_update(test, "Expected %s empty notice, but got %s\n",
205 was_empty ? "an" : "no", tld->task_pushed ? "one" : "none");
212 AST_TEST_DEFINE(threadpool_push)
214 struct ast_threadpool *pool = NULL;
215 struct ast_threadpool_listener *listener = NULL;
216 struct simple_task_data *std = NULL;
217 enum ast_test_result_state res = AST_TEST_FAIL;
221 info->name = "threadpool_push";
222 info->category = "/main/threadpool/";
223 info->summary = "Test task";
225 "Basic threadpool test";
226 return AST_TEST_NOT_RUN;
231 listener = ast_threadpool_listener_alloc(&test_callbacks);
233 return AST_TEST_FAIL;
236 pool = ast_threadpool_create(listener, 0);
241 std = simple_task_data_alloc();
246 ast_threadpool_push(pool, simple_task, std);
248 wait_for_task_pushed(listener);
250 res = listener_check(test, listener, 1, 1, 1, 0, 0, 0);
254 ast_threadpool_shutdown(pool);
256 ao2_cleanup(listener);
261 AST_TEST_DEFINE(threadpool_thread_creation)
263 struct ast_threadpool *pool = NULL;
264 struct ast_threadpool_listener *listener = NULL;
265 enum ast_test_result_state res = AST_TEST_FAIL;
266 struct test_listener_data *tld;
270 info->name = "threadpool_thread_creation";
271 info->category = "/main/threadpool/";
272 info->summary = "Test threadpool thread creation";
274 "Ensure that threads can be added to a threadpool";
275 return AST_TEST_NOT_RUN;
280 listener = ast_threadpool_listener_alloc(&test_callbacks);
282 return AST_TEST_FAIL;
284 tld = listener->private_data;
286 pool = ast_threadpool_create(listener, 0);
291 /* Now let's create a thread. It should start active, then go
294 ast_threadpool_set_size(pool, 1);
296 WAIT_WHILE(tld, tld->num_idle == 0);
298 res = listener_check(test, listener, 0, 0, 0, 0, 1, 0);
302 ast_threadpool_shutdown(pool);
304 ao2_cleanup(listener);
308 static int unload_module(void)
310 ast_test_unregister(threadpool_push);
311 ast_test_unregister(threadpool_thread_creation);
315 static int load_module(void)
317 ast_test_register(threadpool_push);
318 ast_test_register(threadpool_thread_creation);
319 return AST_MODULE_LOAD_SUCCESS;
322 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "threadpool test module");