mirror of
https://github.com/strongswan/strongswan.git
synced 2025-10-06 00:00:47 -04:00
- job_queue tested
This commit is contained in:
parent
3ad9390a61
commit
3911f0755b
@ -21,6 +21,9 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <freeswan.h>
|
||||
#include <pluto/constants.h>
|
||||
#include <pluto/defs.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "tester.h"
|
||||
@ -36,10 +39,12 @@ int main()
|
||||
|
||||
tester->test_all(tester);
|
||||
|
||||
if (tester->destroy(tester) != SUCCESS)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
tester->destroy(tester);
|
||||
|
||||
#ifdef LEAK_DETECTIVE
|
||||
/* Leaks are reported in log file */
|
||||
report_leaks();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ static status_t remove_first(linked_list_t *linked_list, void **item)
|
||||
|
||||
this->count--;
|
||||
|
||||
return element->destroy(element);
|
||||
return (element->destroy(element));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -32,8 +32,8 @@
|
||||
typedef struct tester_s tester_t;
|
||||
|
||||
struct tester_s {
|
||||
status_t (*test_all) (tester_t *this);
|
||||
status_t (*destroy) (tester_t *this);
|
||||
status_t (*test_all) (tester_t *tester);
|
||||
status_t (*destroy) (tester_t *tester);
|
||||
};
|
||||
|
||||
tester_t *tester_create(FILE *output);
|
||||
|
Loading…
x
Reference in New Issue
Block a user