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 = 1;
50 ast_cond_signal(&task_data->cond);
54 AST_TEST_DEFINE(default_taskprocessor)
56 struct ast_taskprocessor *tps;
57 struct task_data task_data;
58 enum ast_test_result_state res = AST_TEST_PASS;
62 info->name = "default_taskprocessor";
63 info->category = "/main/taskprocessor/";
64 info->summary = "Test of default taskproccesor";
66 "Ensures that queued tasks are executed.";
67 return AST_TEST_NOT_RUN;
72 tps = ast_taskprocessor_get("test", TPS_REF_DEFAULT);
75 ast_test_status_update(test, "Unable to create test taskprocessor\n");
79 ast_cond_init(&task_data.cond, NULL);
80 ast_mutex_init(&task_data.lock);
81 task_data.task_complete = 0;
83 ast_taskprocessor_push(tps, task, &task_data);
84 ast_mutex_lock(&task_data.lock);
85 while (!task_data.task_complete) {
86 ast_cond_wait(&task_data.cond, &task_data.lock);
89 if (!task_data.task_complete) {
90 ast_test_status_update(test, "Queued task did not execute!\n");
96 tps = ast_taskprocessor_unreference(tps);
97 ast_mutex_destroy(&task_data.lock);
98 ast_cond_destroy(&task_data.cond);
102 struct test_listener_pvt {
107 static void *test_alloc(struct ast_taskprocessor_listener *listener)
109 struct test_listener_pvt *pvt;
111 pvt = ast_calloc(1, sizeof(*pvt));
115 static void test_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty)
117 struct test_listener_pvt *pvt = listener->private_data;
121 static void test_emptied(struct ast_taskprocessor_listener *listener)
123 struct test_listener_pvt *pvt = listener->private_data;
127 static void test_destroy(void *private_data)
129 struct test_listener_pvt *pvt = private_data;
133 static const struct ast_taskprocessor_listener_callbacks test_callbacks = {
135 .task_pushed = test_task_pushed,
136 .emptied = test_emptied,
137 .destroy = test_destroy,
140 static int listener_test_task(void *ignore)
145 AST_TEST_DEFINE(taskprocessor_listener)
147 struct ast_taskprocessor *tps;
148 struct ast_taskprocessor_listener *listener;
149 struct test_listener_pvt *pvt;
150 enum ast_test_result_state res = AST_TEST_PASS;
154 info->name = "taskprocessor_listener";
155 info->category = "/main/taskprocessor/";
156 info->summary = "Test of taskproccesor listeners";
158 "Ensures that listener callbacks are called when expected.";
159 return AST_TEST_NOT_RUN;
164 listener = ast_taskprocessor_listener_alloc(&test_callbacks);
166 ast_test_status_update(test, "Unable to allocate test taskprocessor listener\n");
167 return AST_TEST_FAIL;
170 tps = ast_taskprocessor_create_with_listener("test_listener", listener);
172 ast_test_status_update(test, "Unable to allocate test taskprocessor\n");
177 ast_taskprocessor_push(tps, listener_test_task, NULL);
178 ast_taskprocessor_push(tps, listener_test_task, NULL);
180 ast_taskprocessor_execute(tps);
181 ast_taskprocessor_execute(tps);
183 pvt = listener->private_data;
184 if (pvt->num_pushed != 2) {
185 ast_test_status_update(test, "Unexpected number of tasks pushed. Expected %d but got %d\n",
191 if (pvt->num_emptied != 1) {
192 ast_test_status_update(test, "Unexpected number of empties. Expected %d but got %d\n",
193 1, pvt->num_emptied);
199 ao2_ref(listener, -1);
200 ast_taskprocessor_unreference(tps);
204 static int unload_module(void)
206 ast_test_unregister(default_taskprocessor);
207 ast_test_unregister(taskprocessor_listener);
211 static int load_module(void)
213 ast_test_register(default_taskprocessor);
214 ast_test_register(taskprocessor_listener);
215 return AST_MODULE_LOAD_SUCCESS;
218 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "taskprocessor test module");