summaryrefslogtreecommitdiff
path: root/tests/test_threadpool.c
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2012-12-07 00:30:35 +0000
committerMark Michelson <mmichelson@digium.com>2012-12-07 00:30:35 +0000
commit4590bfd93d7124fbd042f17260738e315e0abb6e (patch)
tree1579fa64689d9d5da754444e4ba41a9bc6c3f4a4 /tests/test_threadpool.c
parent62981d203221541829b7d6165e2127ca9a29efd4 (diff)
Add new threadpool test and fix some taskprocessor bugs.
The new thread creation test fails because Asterisk locks up while trying to lock a taskprocessor. While trying to debug that, I found a race condition during taskprocessor creation where a default taskprocessor listener could try to operate on a partially started taskprocessor. This was fixed by adding a new callback to taskprocessor listeners. Then while testing that change, I found some bugs in the taskprocessor tests where I was not properly unlocking when done with a lock. Scoped locks have spoiled me a bit. I still have not figured out why the threadpool thread creation test is locking up. git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@377368 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests/test_threadpool.c')
-rw-r--r--tests/test_threadpool.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/test_threadpool.c b/tests/test_threadpool.c
index eed4dc20f..fbbe670bb 100644
--- a/tests/test_threadpool.c
+++ b/tests/test_threadpool.c
@@ -36,6 +36,7 @@
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/astobj2.h"
+#include "asterisk/logger.h"
struct test_listener_data {
int num_active;
@@ -66,6 +67,7 @@ static void test_state_changed(struct ast_threadpool *pool,
{
struct test_listener_data *tld = listener->private_data;
SCOPED_MUTEX(lock, &tld->lock);
+ ast_log(LOG_NOTICE, "State changed: num_active: %d, num_idle: %d\n", active_threads, idle_threads);
tld->num_active = active_threads;
tld->num_idle = idle_threads;
ast_cond_signal(&tld->cond);
@@ -95,6 +97,7 @@ static void test_emptied(struct ast_threadpool *pool,
static void test_destroy(void *private_data)
{
struct test_listener_data *tld = private_data;
+ ast_debug(1, "Poop\n");
ast_cond_destroy(&tld->cond);
ast_mutex_destroy(&tld->lock);
ast_free(tld);
@@ -135,6 +138,15 @@ static int simple_task(void *data)
return 0;
}
+#define WAIT_WHILE(tld, condition) \
+{\
+ ast_mutex_lock(&tld->lock);\
+ while ((condition)) {\
+ ast_cond_wait(&tld->cond, &tld->lock);\
+ }\
+ ast_mutex_unlock(&tld->lock);\
+}\
+
static void wait_for_task_pushed(struct ast_threadpool_listener *listener)
{
struct test_listener_data *tld = listener->private_data;
@@ -246,15 +258,64 @@ end:
return res;
}
+AST_TEST_DEFINE(threadpool_thread_creation)
+{
+ struct ast_threadpool *pool = NULL;
+ struct ast_threadpool_listener *listener = NULL;
+ enum ast_test_result_state res = AST_TEST_FAIL;
+ struct test_listener_data *tld;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "threadpool_thread_creation";
+ info->category = "/main/threadpool_thread_creation/";
+ info->summary = "Test threadpool thread creation";
+ info->description =
+ "Ensure that threads can be added to a threadpool";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks);
+ if (!listener) {
+ return AST_TEST_FAIL;
+ }
+ tld = listener->private_data;
+
+ pool = ast_threadpool_create(listener, 0);
+ if (!pool) {
+ goto end;
+ }
+
+ /* Now let's create a thread. It should start active, then go
+ * idle immediately
+ */
+ ast_threadpool_set_size(pool, 1);
+
+ WAIT_WHILE(tld, tld->num_idle == 0);
+
+ res = listener_check(test, listener, 0, 0, 0, 0, 1, 0);
+
+end:
+ if (pool) {
+ ast_threadpool_shutdown(pool);
+ }
+ ao2_cleanup(listener);
+ return res;
+}
+
static int unload_module(void)
{
ast_test_unregister(threadpool_push);
+ ast_test_unregister(threadpool_thread_creation);
return 0;
}
static int load_module(void)
{
ast_test_register(threadpool_push);
+ ast_test_register(threadpool_thread_creation);
return AST_MODULE_LOAD_SUCCESS;
}