aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/tests/job_queue_test.c
blob: 302bd6043f19b4eeed1941ae22dd118045af20a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * @file job_queue_test.c
 * 
 * @brief Tests to test the Job-Queue type job_queue_t
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

 
//#include <stdlib.h>
#include <freeswan.h>
#include <pluto/constants.h>
#include <pluto/defs.h>
#include <pthread.h>
 
#include "../tester.h"
#include "../job_queue.h"
 
 
typedef struct job_queue_test_s job_queue_test_t;

/**
 * @brief Informations for the involved test-thread used in this test
 * 
 */
struct job_queue_test_s{
	tester_t *tester;
	job_queue_t *job_queue;
	/**
	 * number of items to be inserted in the job-queue 
	 */
	int insert_item_count;	
	/**
	 * number of items to be removed by each 
	 * receiver thread from the job-queue 
	 */
	int remove_item_count;	
};

/**
 * @brief sender thread used in the the job_queue test function
 * 
 * @param testinfo informations for the specific thread.
 */
static void test_job_queue_sender(job_queue_test_t * testinfo)
{
	int i;	
	for (i = 0; i < testinfo->insert_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
 * 
 * @param testinfo informations for the specific thread.
 */
static void test_job_queue_receiver(job_queue_test_t * testinfo)
{
	int i;
	for (i = 0; i < testinfo->remove_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");		
		pfree(job->assigned_data);
		testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check");
	}
}

/*
 * description is in header file
 */
void test_job_queue(tester_t *tester)
{
	int value, i;
	pthread_t sender_thread, receiver_threads[5];
	job_queue_t *job_queue = job_queue_create();
	job_queue_test_t test_infos;

	test_infos.tester = tester;
	test_infos.job_queue = job_queue;
	test_infos.insert_item_count = 50000;
	test_infos.remove_item_count = 10000;
	
	
	for (i = 0; i < 5;i++)
	{
		pthread_create( &receiver_threads[i], NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos);
	}
	pthread_create( &sender_thread, NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos);
	
	/* Wait for all threads */
	pthread_join(sender_thread, NULL);	
	for (i = 0; i < 5;i++)
	{
		pthread_join(receiver_threads[i], NULL);
	}
	
	/* the job-queue has to be empty now! */
	tester->assert_true(tester,(job_queue->get_count(job_queue,&value) == SUCCESS), "get count call check");
	tester->assert_true(tester,(value == 0), "get count value check");
	tester->assert_true(tester,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check");
}