diff options
Diffstat (limited to 'Source/charon/tester.c')
-rw-r--r-- | Source/charon/tester.c | 80 |
1 files changed, 75 insertions, 5 deletions
diff --git a/Source/charon/tester.c b/Source/charon/tester.c index 1e5974656..db2676e43 100644 --- a/Source/charon/tester.c +++ b/Source/charon/tester.c @@ -26,10 +26,12 @@ #include <pluto/constants.h> #include <pluto/defs.h> #include <string.h> +#include <pthread.h> #include "tester.h" #include "linked_list.h" #include "thread_pool.h" +#include "job_queue.h" /** * @brief Private Variables and Functions of tester class @@ -45,6 +47,7 @@ struct private_tester_s { int tests_count; int failed_tests_count; int failed_asserts_count; + pthread_mutex_t mutex; /* Private functions */ /** @@ -139,6 +142,71 @@ static void test_thread_pool(private_tester_t *this) pool->destroy(pool); } +typedef struct job_queue_test_s job_queue_test_t; + +struct job_queue_test_s{ + private_tester_t *tester; + job_queue_t *job_queue; + int max_queue_item_count; +}; + +/** + * @brief sender thread used in the the job_queue test function + */ +static void test_job_queue_sender(job_queue_test_t * testinfo) +{ + + int i; + + for (i = 0; i < testinfo->max_queue_item_count; i++) + { + int *value = alloc_thing(int,"int"); + *value = i; + job_t *job = job_create(INCOMING_PACKET,value); + testinfo->job_queue->add(testinfo->job_queue,job); + } +} + +/** + * @brief receiver thread used in the the job_queue test function + */ +static void test_job_queue_receiver(job_queue_test_t * testinfo) +{ + int i; + + for (i = 0; i < testinfo->max_queue_item_count; i++) + { + job_t *job; + testinfo->tester->assert_true(testinfo->tester,(testinfo->job_queue->get(testinfo->job_queue,&job) == SUCCESS), "get job call check"); + testinfo->tester->assert_true(testinfo->tester,(job->type == INCOMING_PACKET), "job type check"); + testinfo->tester->assert_true(testinfo->tester,((*((int *) (job->assigned_data))) == i), "job value check"); + + pfree(job->assigned_data); + testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check"); + } +} + +/** + * @brief Test function test the job_queue functionality + */ +static void test_job_queue(private_tester_t *this) +{ + pthread_t sender_thread, receiver_thread; + job_queue_t *job_queue = job_queue_create(); + job_queue_test_t test_infos; + test_infos.tester = this; + test_infos.job_queue = job_queue; + test_infos.max_queue_item_count = 100; + + pthread_create( &receiver_thread, NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos); + pthread_create( &sender_thread, NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos); + + pthread_join(sender_thread, NULL); + pthread_join(receiver_thread, NULL); + + this->assert_true(this,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check"); +} + /** * @brief Testing of all registered tests * @@ -152,13 +220,10 @@ static status_t test_all(tester_t *tester) /* Add new Tests here! */ this->run_test(this,test_linked_list,"Linked List"); this->run_test(this,test_thread_pool,"Thread Pool"); + this->run_test(this,test_job_queue,"Job-Queue"); fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count); -#ifdef LEAK_DETECTIVE - /* Leaks are reported in log file */ - report_leaks(); -#endif return SUCCESS; } @@ -194,6 +259,7 @@ static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_ assert_name = "unknown"; } + pthread_mutex_lock(&(this->mutex)); if (!to_be_true) { this->failed_asserts_count++; @@ -202,14 +268,17 @@ static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_ { fprintf(this->output," Assert '%s' succeeded\n", assert_name); } + pthread_mutex_unlock(&(this->mutex)); } /** * Implements the destroy function * */ -static status_t destroy(tester_t *this) +static status_t destroy(tester_t *tester) { + private_tester_t *this = (private_tester_t*) tester; + pthread_mutex_destroy(&(this->mutex)); pfree(this); return SUCCESS; } @@ -226,6 +295,7 @@ tester_t *tester_create(FILE *output) this->failed_tests_count = 0; this->tests_count = 0; this->output = output; + pthread_mutex_init(&(this->mutex),NULL); return &(this->tester); } |