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