Add a single-thread multi-task test.
[asterisk/asterisk.git] / tests / test_threadpool.c
index 6d67283..86ced4d 100644 (file)
@@ -162,6 +162,47 @@ static void wait_for_task_pushed(struct ast_threadpool_listener *listener)
        }
 }
 
        }
 }
 
+static enum ast_test_result_state wait_for_completion(struct simple_task_data *std)
+{
+       struct timeval start = ast_tvnow();
+       struct timespec end = {
+               .tv_sec = start.tv_sec + 5,
+               .tv_nsec = start.tv_usec * 1000
+       };
+       enum ast_test_result_state res = AST_TEST_PASS;
+       SCOPED_MUTEX(lock, &std->lock);
+
+       while (!std->task_executed) {
+               ast_cond_timedwait(&std->cond, lock, &end);
+       }
+
+       if (!std->task_executed) {
+               res = AST_TEST_FAIL;
+       }
+       return res;
+}
+
+static enum ast_test_result_state wait_for_empty_notice(struct test_listener_data *tld)
+{
+       struct timeval start = ast_tvnow();
+       struct timespec end = {
+               .tv_sec = start.tv_sec + 5,
+               .tv_nsec = start.tv_usec * 1000
+       };
+       enum ast_test_result_state res = AST_TEST_PASS;
+       SCOPED_MUTEX(lock, &tld->lock);
+
+       while (!tld->empty_notice) {
+               ast_cond_timedwait(&tld->cond, lock, &end);
+       }
+
+       if (!tld->empty_notice) {
+               res = AST_TEST_FAIL;
+       }
+
+       return res;
+}
+
 static enum ast_test_result_state listener_check(
                struct ast_test *test,
                struct ast_threadpool_listener *listener,
 static enum ast_test_result_state listener_check(
                struct ast_test *test,
                struct ast_threadpool_listener *listener,
@@ -182,7 +223,7 @@ static enum ast_test_result_state listener_check(
        }
        if (tld->was_empty != was_empty) {
                ast_test_status_update(test, "Expected %sto be empty, but it was%s\n",
        }
        if (tld->was_empty != was_empty) {
                ast_test_status_update(test, "Expected %sto be empty, but it was%s\n",
-                               was_empty ? "" : "not ", tld->task_pushed ? "" : " not");
+                               was_empty ? "" : "not ", tld->was_empty ? "" : " not");
                res = AST_TEST_FAIL;
        }
        if (tld->num_tasks!= num_tasks) {
                res = AST_TEST_FAIL;
        }
        if (tld->num_tasks!= num_tasks) {
@@ -305,10 +346,285 @@ end:
        return res;
 }
 
        return res;
 }
 
+AST_TEST_DEFINE(threadpool_thread_destruction)
+{
+       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_destruction";
+               info->category = "/main/threadpool/";
+               info->summary = "Test threadpool thread destruction";
+               info->description =
+                       "Ensure that threads are properly destroyed in 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;
+       }
+
+       ast_threadpool_set_size(pool, 3);
+
+       WAIT_WHILE(tld, tld->num_idle < 3);
+
+       res = listener_check(test, listener, 0, 0, 0, 0, 3, 0);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       ast_threadpool_set_size(pool, 2);
+
+       WAIT_WHILE(tld, tld->num_idle > 2);
+
+       res = listener_check(test, listener, 0, 0, 0, 0, 2, 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;
+       struct ast_threadpool_listener *listener = NULL;
+       struct simple_task_data *std = NULL;
+       enum ast_test_result_state res = AST_TEST_FAIL;
+       struct test_listener_data *tld;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "threadpool_one_task_one_thread";
+               info->category = "/main/threadpool/";
+               info->summary = "Test a single task with a single thread";
+               info->description =
+                       "Push a task into an empty threadpool, then add a thread to the pool.";
+               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;
+       }
+
+       std = simple_task_data_alloc();
+       if (!std) {
+               goto end;
+       }
+
+       ast_threadpool_push(pool, simple_task, std);
+
+       ast_threadpool_set_size(pool, 1);
+
+       /* Threads added to the pool are active when they start,
+        * so the newly-created thread should immediately execute
+        * the waiting task.
+        */
+       res = wait_for_completion(std);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       res = wait_for_empty_notice(tld);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+       
+       /* After completing the task, the thread should go idle */
+       WAIT_WHILE(tld, tld->num_idle == 0);
+
+       res = listener_check(test, listener, 1, 1, 1, 0, 1, 1);
+
+end:
+       if (pool) {
+               ast_threadpool_shutdown(pool);
+       }
+       ao2_cleanup(listener);
+       ast_free(std);
+       return res;
+
+}
+
+AST_TEST_DEFINE(threadpool_one_thread_one_task)
+{
+       struct ast_threadpool *pool = NULL;
+       struct ast_threadpool_listener *listener = NULL;
+       struct simple_task_data *std = NULL;
+       enum ast_test_result_state res = AST_TEST_FAIL;
+       struct test_listener_data *tld;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "threadpool_one_thread_one_task";
+               info->category = "/main/threadpool/";
+               info->summary = "Test a single thread with a single task";
+               info->description =
+                       "Add a thread to the pool and then push a task to it.";
+               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;
+       }
+
+       std = simple_task_data_alloc();
+       if (!std) {
+               goto end;
+       }
+
+       ast_threadpool_set_size(pool, 1);
+
+       WAIT_WHILE(tld, tld->num_idle == 0);
+
+       ast_threadpool_push(pool, simple_task, std);
+
+       res = wait_for_completion(std);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       res = wait_for_empty_notice(tld);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       /* After completing the task, the thread should go idle */
+       WAIT_WHILE(tld, tld->num_idle == 0);
+
+       res = listener_check(test, listener, 1, 1, 1, 0, 1, 1);
+
+end:
+       if (pool) {
+               ast_threadpool_shutdown(pool);
+       }
+       ao2_cleanup(listener);
+       ast_free(std);
+       return res;
+
+}
+
+AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks)
+{
+       struct ast_threadpool *pool = NULL;
+       struct ast_threadpool_listener *listener = NULL;
+       struct simple_task_data *std1 = NULL;
+       struct simple_task_data *std2 = NULL;
+       struct simple_task_data *std3 = NULL;
+       enum ast_test_result_state res = AST_TEST_FAIL;
+       struct test_listener_data *tld;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "threadpool_one_thread_multiple_tasks";
+               info->category = "/main/threadpool/";
+               info->summary = "Test a single thread with multiple tasks";
+               info->description =
+                       "Add a thread to the pool and then push three tasks to it.";
+               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;
+       }
+
+       std1 = simple_task_data_alloc();
+       std2 = simple_task_data_alloc();
+       std3 = simple_task_data_alloc();
+       if (!std1 || !std2 || !std3) {
+               goto end;
+       }
+
+       ast_threadpool_set_size(pool, 1);
+
+       WAIT_WHILE(tld, tld->num_idle == 0);
+
+       ast_threadpool_push(pool, simple_task, std1);
+       ast_threadpool_push(pool, simple_task, std2);
+       ast_threadpool_push(pool, simple_task, std3);
+
+       res = wait_for_completion(std1);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+       res = wait_for_completion(std2);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+       res = wait_for_completion(std3);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       res = wait_for_empty_notice(tld);
+       if (res == AST_TEST_FAIL) {
+               goto end;
+       }
+
+       WAIT_WHILE(tld, tld->num_idle == 0);
+
+       res = listener_check(test, listener, 1, 0, 3, 0, 1, 1);
+
+end:
+       if (pool) {
+               ast_threadpool_shutdown(pool);
+       }
+       ao2_cleanup(listener);
+       ast_free(std1);
+       ast_free(std2);
+       ast_free(std3);
+       return res;
+
+}
+
 static int unload_module(void)
 {
        ast_test_unregister(threadpool_push);
        ast_test_unregister(threadpool_thread_creation);
 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_one_task_one_thread);
+       ast_test_unregister(threadpool_one_thread_one_task);
+       ast_test_unregister(threadpool_one_thread_multiple_tasks);
        return 0;
 }
 
        return 0;
 }
 
@@ -316,6 +632,10 @@ static int load_module(void)
 {
        ast_test_register(threadpool_push);
        ast_test_register(threadpool_thread_creation);
 {
        ast_test_register(threadpool_push);
        ast_test_register(threadpool_thread_creation);
+       ast_test_register(threadpool_thread_destruction);
+       ast_test_register(threadpool_one_task_one_thread);
+       ast_test_register(threadpool_one_thread_one_task);
+       ast_test_register(threadpool_one_thread_multiple_tasks);
        return AST_MODULE_LOAD_SUCCESS;
 }
 
        return AST_MODULE_LOAD_SUCCESS;
 }