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 taskprocessor 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/taskprocessor.h"
36 #include "asterisk/module.h"
37 #include "asterisk/astobj2.h"
45 static int task(void *data)
47 struct task_data *task_data = data;
48 SCOPED_MUTEX(lock, &task_data->lock);
49 ++task_data->task_complete;
50 ast_cond_signal(&task_data->cond);
54 AST_TEST_DEFINE(default_taskprocessor)
56 struct ast_taskprocessor *tps;
57 struct task_data task_data;
60 enum ast_test_result_state res = AST_TEST_PASS;
65 info->name = "default_taskprocessor";
66 info->category = "/main/taskprocessor/";
67 info->summary = "Test of default taskproccesor";
69 "Ensures that queued tasks are executed.";
70 return AST_TEST_NOT_RUN;
75 tps = ast_taskprocessor_get("test", TPS_REF_DEFAULT);
78 ast_test_status_update(test, "Unable to create test taskprocessor\n");
84 ts.tv_sec = start.tv_sec + 30;
85 ts.tv_nsec = start.tv_usec * 1000;
87 ast_cond_init(&task_data.cond, NULL);
88 ast_mutex_init(&task_data.lock);
89 task_data.task_complete = 0;
91 ast_taskprocessor_push(tps, task, &task_data);
92 ast_mutex_lock(&task_data.lock);
93 while (!task_data.task_complete) {
94 timedwait_res = ast_cond_timedwait(&task_data.cond, &task_data.lock, &ts);
95 if (timedwait_res == ETIMEDOUT) {
100 if (!task_data.task_complete) {
101 ast_test_status_update(test, "Queued task did not execute!\n");
107 tps = ast_taskprocessor_unreference(tps);
108 ast_mutex_destroy(&task_data.lock);
109 ast_cond_destroy(&task_data.cond);
113 struct test_listener_pvt {
120 static void *test_alloc(struct ast_taskprocessor_listener *listener)
122 struct test_listener_pvt *pvt;
124 pvt = ast_calloc(1, sizeof(*pvt));
128 static void test_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty)
130 struct test_listener_pvt *pvt = listener->private_data;
133 ++pvt->num_was_empty;
137 static void test_emptied(struct ast_taskprocessor_listener *listener)
139 struct test_listener_pvt *pvt = listener->private_data;
143 static void test_shutdown(struct ast_taskprocessor_listener *listener)
145 struct test_listener_pvt *pvt = listener->private_data;
149 static void test_destroy(void *private_data)
151 struct test_listener_pvt *pvt = private_data;
155 static const struct ast_taskprocessor_listener_callbacks test_callbacks = {
157 .task_pushed = test_task_pushed,
158 .emptied = test_emptied,
159 .shutdown = test_shutdown,
160 .destroy = test_destroy,
163 static int listener_test_task(void *ignore)
168 static int check_stats(struct ast_test *test, const struct test_listener_pvt *pvt, int num_pushed, int num_emptied, int num_was_empty)
170 if (pvt->num_pushed != num_pushed) {
171 ast_test_status_update(test, "Unexpected number of tasks pushed. Expected %d but got %d\n",
172 num_pushed, pvt->num_pushed);
176 if (pvt->num_emptied != num_emptied) {
177 ast_test_status_update(test, "Unexpected number of empties. Expected %d but got %d\n",
178 num_emptied, pvt->num_emptied);
182 if (pvt->num_was_empty != num_was_empty) {
183 ast_test_status_update(test, "Unexpected number of empties. Expected %d but got %d\n",
184 num_was_empty, pvt->num_emptied);
191 AST_TEST_DEFINE(taskprocessor_listener)
193 struct ast_taskprocessor *tps;
194 struct ast_taskprocessor_listener *listener;
195 struct test_listener_pvt *pvt;
196 enum ast_test_result_state res = AST_TEST_PASS;
200 info->name = "taskprocessor_listener";
201 info->category = "/main/taskprocessor/";
202 info->summary = "Test of taskproccesor listeners";
204 "Ensures that listener callbacks are called when expected.";
205 return AST_TEST_NOT_RUN;
210 listener = ast_taskprocessor_listener_alloc(&test_callbacks);
212 ast_test_status_update(test, "Unable to allocate test taskprocessor listener\n");
213 return AST_TEST_FAIL;
216 tps = ast_taskprocessor_create_with_listener("test_listener", listener);
218 ast_test_status_update(test, "Unable to allocate test taskprocessor\n");
223 pvt = listener->private_data;
225 ast_taskprocessor_push(tps, listener_test_task, NULL);
227 if (check_stats(test, pvt, 1, 0, 1) < 0) {
232 ast_taskprocessor_push(tps, listener_test_task, NULL);
234 if (check_stats(test, pvt, 2, 0, 1) < 0) {
239 ast_taskprocessor_execute(tps);
241 if (check_stats(test, pvt, 2, 0, 1) < 0) {
246 ast_taskprocessor_execute(tps);
248 if (check_stats(test, pvt, 2, 1, 1) < 0) {
253 tps = ast_taskprocessor_unreference(tps);
255 if (!pvt->shutdown) {
261 ao2_ref(listener, -1);
262 /* This is safe even if tps is NULL */
263 ast_taskprocessor_unreference(tps);
267 static int unload_module(void)
269 ast_test_unregister(default_taskprocessor);
270 ast_test_unregister(taskprocessor_listener);
274 static int load_module(void)
276 ast_test_register(default_taskprocessor);
277 ast_test_register(taskprocessor_listener);
278 return AST_MODULE_LOAD_SUCCESS;
281 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "taskprocessor test module");