- LEAK_DETECTIVE activated
[strongswan.git] / Source / charon / tests / job_queue_test.c
1 /**
2 * @file job_queue_test.c
3 *
4 * @brief Tests to test the Job-Queue type job_queue_t
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23
24 #include <stdlib.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28 #include <pthread.h>
29 #include <unistd.h>
30
31 #include "job_queue_test.h"
32 #include "../tester.h"
33 #include "../job_queue.h"
34
35
36 typedef struct job_queue_test_s job_queue_test_t;
37
38 /**
39 * @brief Informations for the involved test-thread used in this test
40 *
41 */
42 struct job_queue_test_s{
43 tester_t *tester;
44 job_queue_t *job_queue;
45 /**
46 * number of items to be inserted in the job-queue
47 */
48 int insert_item_count;
49 /**
50 * number of items to be removed by each
51 * receiver thread from the job-queue
52 */
53 int remove_item_count;
54 };
55
56 /**
57 * @brief sender thread used in the the job_queue test function
58 *
59 * @param testinfo informations for the specific thread.
60 */
61 static void test_job_queue_sender(job_queue_test_t * testinfo)
62 {
63 int i;
64 for (i = 0; i < testinfo->insert_item_count; i++)
65 {
66 int *value = alloc_thing(int,"int in test_job_queue_sender");
67 *value = i;
68 job_t *job = job_create(INCOMING_PACKET,value);
69 testinfo->job_queue->add(testinfo->job_queue,job);
70 }
71 }
72
73 /**
74 * @brief receiver thread used in the the job_queue test function
75 *
76 * @param testinfo informations for the specific thread.
77 */
78 static void test_job_queue_receiver(job_queue_test_t * testinfo)
79 {
80 int i;
81 for (i = 0; i < testinfo->remove_item_count; i++)
82 {
83 job_t *job;
84 testinfo->tester->assert_true(testinfo->tester,(testinfo->job_queue->get(testinfo->job_queue,&job) == SUCCESS), "get job call check");
85 testinfo->tester->assert_true(testinfo->tester,(job->type == INCOMING_PACKET), "job type check");
86 pfree(job->assigned_data);
87 testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check");
88 }
89 }
90
91 /*
92 * description is in header file
93 */
94 void test_job_queue(tester_t *tester)
95 {
96 int value, desired_value, i;
97 int sender_count = 10;
98 int receiver_count = 2;
99 pthread_t sender_threads[sender_count];
100 pthread_t receiver_threads[receiver_count];
101 job_queue_t *job_queue = job_queue_create();
102 job_queue_test_t test_infos;
103
104 test_infos.tester = tester;
105 test_infos.job_queue = job_queue;
106 test_infos.insert_item_count = 10000;
107 test_infos.remove_item_count = 50000;
108
109
110 desired_value = test_infos.insert_item_count * sender_count -
111 test_infos.remove_item_count * receiver_count;
112
113 for (i = 0; i < receiver_count;i++)
114 {
115 pthread_create( &receiver_threads[i], NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos);
116 }
117 for (i = 0; i < sender_count;i++)
118 {
119 pthread_create( &sender_threads[i], NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos);
120 }
121
122
123 /* Wait for all threads */
124 for (i = 0; i < sender_count;i++)
125 {
126 pthread_join(sender_threads[i], NULL);
127 }
128 for (i = 0; i < receiver_count;i++)
129 {
130 pthread_join(receiver_threads[i], NULL);
131 }
132
133 /* the job-queue has to have disered_value count entries! */
134 tester->assert_true(tester,(job_queue->get_count(job_queue,&value) == SUCCESS), "get count call check");
135 tester->assert_true(tester,(value == desired_value), "get count value check");
136
137 tester->assert_true(tester,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check");
138 }