summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2012-12-10 06:13:09 +0000
committerMark Michelson <mmichelson@digium.com>2012-12-10 06:13:09 +0000
commit64deed062a6c79124dbc3b5d09c770a7a3f20f1c (patch)
tree8e182a35753bcb81b53d973dbbd8d89e150b813d /tests
parent5dd22df050264299b42160daeaa7701c81488ceb (diff)
Add threadpool options and accompanying test.
The only test added so far is an idle thread timeout option. This will greatly aid threadpool users who wish to maintain a threadpool by allowing for idle threads to die out as necessary. Test passes. git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@377580 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests')
-rw-r--r--tests/test_threadpool.c112
1 files changed, 103 insertions, 9 deletions
diff --git a/tests/test_threadpool.c b/tests/test_threadpool.c
index 373d0c028..5de516839 100644
--- a/tests/test_threadpool.c
+++ b/tests/test_threadpool.c
@@ -257,6 +257,10 @@ AST_TEST_DEFINE(threadpool_push)
struct ast_threadpool_listener *listener = NULL;
struct simple_task_data *std = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -275,7 +279,7 @@ AST_TEST_DEFINE(threadpool_push)
return AST_TEST_FAIL;
}
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -306,6 +310,10 @@ AST_TEST_DEFINE(threadpool_thread_creation)
struct ast_threadpool_listener *listener = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -325,7 +333,7 @@ AST_TEST_DEFINE(threadpool_thread_creation)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -353,6 +361,10 @@ AST_TEST_DEFINE(threadpool_thread_destruction)
struct ast_threadpool_listener *listener = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -372,7 +384,7 @@ AST_TEST_DEFINE(threadpool_thread_destruction)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -400,6 +412,62 @@ end:
return res;
}
+AST_TEST_DEFINE(threadpool_thread_timeout)
+{
+ 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;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 5,
+ };
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "threadpool_thread_timeout";
+ info->category = "/main/threadpool/";
+ info->summary = "Test threadpool thread timeout";
+ info->description =
+ "Ensure that a thread with a five second timeout dies as expected.";
+ 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, &options);
+ if (!pool) {
+ goto end;
+ }
+
+ ast_threadpool_set_size(pool, 1);
+
+ WAIT_WHILE(tld, tld->num_idle < 1);
+
+ res = listener_check(test, listener, 0, 0, 0, 0, 1, 0);
+ if (res == AST_TEST_FAIL) {
+ goto end;
+ }
+
+ /* The thread should time out after 5 seconds */
+ WAIT_WHILE(tld, tld->num_idle > 0);
+
+ res = listener_check(test, listener, 0, 0, 0, 0, 0, 0);
+
+end:
+ if (pool) {
+ ast_threadpool_shutdown(pool);
+ }
+ ao2_cleanup(listener);
+ return res;
+}
+
AST_TEST_DEFINE(threadpool_one_task_one_thread)
{
struct ast_threadpool *pool = NULL;
@@ -407,6 +475,10 @@ AST_TEST_DEFINE(threadpool_one_task_one_thread)
struct simple_task_data *std = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -426,7 +498,7 @@ AST_TEST_DEFINE(threadpool_one_task_one_thread)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -476,6 +548,10 @@ AST_TEST_DEFINE(threadpool_one_thread_one_task)
struct simple_task_data *std = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -495,7 +571,7 @@ AST_TEST_DEFINE(threadpool_one_thread_one_task)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -545,6 +621,10 @@ AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks)
struct simple_task_data *std3 = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -564,7 +644,7 @@ AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -626,6 +706,10 @@ AST_TEST_DEFINE(threadpool_reactivation)
struct simple_task_data *std2 = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -647,7 +731,7 @@ AST_TEST_DEFINE(threadpool_reactivation)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -773,6 +857,10 @@ AST_TEST_DEFINE(threadpool_task_distribution)
struct complex_task_data *ctd2 = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -793,7 +881,7 @@ AST_TEST_DEFINE(threadpool_task_distribution)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -851,6 +939,10 @@ AST_TEST_DEFINE(threadpool_more_destruction)
struct complex_task_data *ctd2 = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct test_listener_data *tld;
+ struct ast_threadpool_options options = {
+ .version = AST_THREADPOOL_OPTIONS_VERSION,
+ .idle_timeout = 0,
+ };
switch (cmd) {
case TEST_INIT:
@@ -873,7 +965,7 @@ AST_TEST_DEFINE(threadpool_more_destruction)
}
tld = listener->private_data;
- pool = ast_threadpool_create(listener, 0);
+ pool = ast_threadpool_create(listener, 0, &options);
if (!pool) {
goto end;
}
@@ -940,6 +1032,7 @@ static int unload_module(void)
ast_test_unregister(threadpool_push);
ast_test_unregister(threadpool_thread_creation);
ast_test_unregister(threadpool_thread_destruction);
+ ast_test_unregister(threadpool_thread_timeout);
ast_test_unregister(threadpool_one_task_one_thread);
ast_test_unregister(threadpool_one_thread_one_task);
ast_test_unregister(threadpool_one_thread_multiple_tasks);
@@ -954,6 +1047,7 @@ static int load_module(void)
ast_test_register(threadpool_push);
ast_test_register(threadpool_thread_creation);
ast_test_register(threadpool_thread_destruction);
+ ast_test_register(threadpool_thread_timeout);
ast_test_register(threadpool_one_task_one_thread);
ast_test_register(threadpool_one_thread_one_task);
ast_test_register(threadpool_one_thread_multiple_tasks);