7e3d0bca2ef7ec584357f07d3f6f3248d4418898
[strongswan.git] / Source / charon / testcases / send_queue_test.c
1 /**
2 * @file send_queue_test.c
3 *
4 * @brief Tests to test the Send-Queue type send_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 #include <pthread.h>
24
25 #include "send_queue_test.h"
26
27 #include <queues/send_queue.h>
28
29
30 /**
31 * @brief Informations for the involved test-thread used in this test
32 *
33 */
34 typedef struct send_queue_test_s send_queue_test_t;
35
36
37 struct send_queue_test_s{
38 /**
39 * Associated tester_t object
40 */
41 tester_t *tester;
42
43 /**
44 * Queue to test
45 */
46 send_queue_t *send_queue;
47
48 /**
49 * number of items to be inserted in the send-queue by each thread
50 */
51 int insert_item_count;
52
53 /**
54 * number of items to be removed by each
55 * receiver thread from the send-queue
56 */
57 int remove_item_count;
58 };
59
60 /**
61 * @brief sender thread used in the the send_queue test function
62 *
63 * @param testinfo informations for the specific thread.
64 */
65 static void test_send_queue_sender(send_queue_test_t * testinfo)
66 {
67 int i;
68 for (i = 0; i < testinfo->insert_item_count; i++)
69 {
70 packet_t *packet = packet_create(AF_INET);
71 testinfo->tester->assert_true(testinfo->tester,(packet != NULL), "create packet call check");
72 testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->add(testinfo->send_queue,packet) == SUCCESS), "add packet call check");
73 }
74 }
75
76 /**
77 * @brief receiver thread used in the the send_queue test function
78 *
79 * @param testinfo informations for the specific thread.
80 */
81 static void test_send_queue_receiver(send_queue_test_t * testinfo)
82 {
83 int i;
84 for (i = 0; i < testinfo->remove_item_count; i++)
85 {
86 packet_t *packet;
87 testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->get(testinfo->send_queue,&packet) == SUCCESS), "get packet call check");
88
89 testinfo->tester->assert_true(testinfo->tester,( packet != NULL), "packet not NULL call check");
90
91 testinfo->tester->assert_true(testinfo->tester,( packet->destroy(packet) == SUCCESS), "packet destroy call check");
92 }
93 }
94
95 /*
96 * description is in header file
97 */
98 void test_send_queue(tester_t *tester)
99 {
100 int desired_value, i;
101 int sender_count = 10;
102 int receiver_count = 2;
103 pthread_t sender_threads[sender_count];
104 pthread_t receiver_threads[receiver_count];
105 send_queue_t *send_queue = send_queue_create();
106 send_queue_test_t test_infos;
107
108 test_infos.tester = tester;
109 test_infos.send_queue = send_queue;
110 test_infos.insert_item_count = 10000;
111 test_infos.remove_item_count = 10000;
112
113
114 desired_value = test_infos.insert_item_count * sender_count -
115 test_infos.remove_item_count * receiver_count;
116
117 for (i = 0; i < receiver_count;i++)
118 {
119 pthread_create( &receiver_threads[i], NULL,(void*(*)(void*)) &test_send_queue_receiver, (void*) &test_infos);
120 }
121
122 for (i = 0; i < sender_count;i++)
123 {
124 pthread_create( &sender_threads[i], NULL,(void*(*)(void*)) &test_send_queue_sender, (void*) &test_infos);
125 }
126
127
128 /* Wait for all threads */
129 for (i = 0; i < sender_count;i++)
130 {
131 pthread_join(sender_threads[i], NULL);
132 }
133 for (i = 0; i < receiver_count;i++)
134 {
135 pthread_join(receiver_threads[i], NULL);
136 }
137
138
139 /* the send-queue has to have diserd_value count entries*/
140 tester->assert_true(tester,(send_queue->get_count(send_queue) == desired_value), "count value check");
141 send_queue->destroy(send_queue);
142 }