13576e82c435530b4c0cbfd6f9174eeca6b6b284
[strongswan.git] / Source / charon / testcases / 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 <pthread.h>
26 #include <unistd.h>
27
28 #include "job_queue_test.h"
29
30 #include <utils/allocator.h>
31 #include <queues/job_queue.h>
32 #include <queues/jobs/initiate_ike_sa_job.h>
33
34
35 typedef struct job_queue_test_s job_queue_test_t;
36
37 /**
38 * @brief Informations for the involved test-thread used in this test
39 *
40 */
41 struct job_queue_test_s{
42 tester_t *tester;
43 job_queue_t *job_queue;
44 /**
45 * number of items to be inserted in the job-queue
46 */
47 int insert_item_count;
48 /**
49 * number of items to be removed by each
50 * receiver thread from the job-queue
51 */
52 int remove_item_count;
53 };
54
55 /**
56 * @brief sender thread used in the the job_queue test function
57 *
58 * @param testinfo informations for the specific thread.
59 */
60 static void test_job_queue_sender(job_queue_test_t * testinfo)
61 {
62 int i;
63 for (i = 0; i < testinfo->insert_item_count; i++)
64 {
65 job_t *job = (job_t *) initiate_ike_sa_job_create("test");
66 testinfo->job_queue->add(testinfo->job_queue,job);
67 }
68 }
69
70 /**
71 * @brief receiver thread used in the the job_queue test function
72 *
73 * @param testinfo informations for the specific thread.
74 */
75 static void test_job_queue_receiver(job_queue_test_t * testinfo)
76 {
77 int i;
78 for (i = 0; i < testinfo->remove_item_count; i++)
79 {
80 job_t *job;
81 testinfo->tester->assert_true(testinfo->tester,(testinfo->job_queue->get(testinfo->job_queue,&job) == SUCCESS), "get job call check");
82 testinfo->tester->assert_true(testinfo->tester,(job->get_type(job) == INITIATE_IKE_SA), "job type check");
83 testinfo->tester->assert_true(testinfo->tester,(job->destroy(job) == SUCCESS), "job destroy call check");
84 }
85 }
86
87 /*
88 * description is in header file
89 */
90 void test_job_queue(tester_t *tester)
91 {
92 int desired_value, i;
93 int sender_count = 10;
94 int receiver_count = 2;
95 pthread_t sender_threads[sender_count];
96 pthread_t receiver_threads[receiver_count];
97 job_queue_t *job_queue = job_queue_create();
98 job_queue_test_t test_infos;
99
100 test_infos.tester = tester;
101 test_infos.job_queue = job_queue;
102 test_infos.insert_item_count = 10000;
103 test_infos.remove_item_count = 50000;
104
105
106 desired_value = test_infos.insert_item_count * sender_count -
107 test_infos.remove_item_count * receiver_count;
108
109 for (i = 0; i < receiver_count;i++)
110 {
111 pthread_create( &receiver_threads[i], NULL,(void*(*)(void*)) &test_job_queue_receiver, (void*) &test_infos);
112 }
113 for (i = 0; i < sender_count;i++)
114 {
115 pthread_create( &sender_threads[i], NULL,(void*(*)(void*)) &test_job_queue_sender, (void*) &test_infos);
116 }
117
118
119 /* Wait for all threads */
120 for (i = 0; i < sender_count;i++)
121 {
122 pthread_join(sender_threads[i], NULL);
123 }
124 for (i = 0; i < receiver_count;i++)
125 {
126 pthread_join(receiver_threads[i], NULL);
127 }
128
129 /* the job-queue has to have disered_value count entries! */
130 tester->assert_true(tester,(job_queue->get_count(job_queue) == desired_value), "get count value check");
131
132 tester->assert_true(tester,(job_queue->destroy(job_queue) == SUCCESS), "destroy call check");
133 }