]>
Commit | Line | Data |
---|---|---|
e15f7292 JH |
1 | /** |
2 | * @file send_queue_test.c | |
c3dc6f1a | 3 | * |
ed37dee6 | 4 | * @brief Tests for the send_queue_t class. |
c3dc6f1a | 5 | * |
e15f7292 JH |
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" | |
88878242 | 26 | |
94852e75 | 27 | #include <queues/send_queue.h> |
e15f7292 JH |
28 | |
29 | ||
30 | /** | |
31 | * @brief Informations for the involved test-thread used in this test | |
c3dc6f1a | 32 | * |
e15f7292 JH |
33 | */ |
34 | typedef struct send_queue_test_s send_queue_test_t; | |
35 | ||
36 | ||
37 | struct send_queue_test_s{ | |
38 | /** | |
51d56047 | 39 | * Associated protected_tester_t object |
e15f7292 | 40 | */ |
51d56047 | 41 | protected_tester_t *tester; |
c3dc6f1a | 42 | |
e15f7292 JH |
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 | */ | |
c3dc6f1a | 51 | int insert_item_count; |
e15f7292 JH |
52 | |
53 | /** | |
c3dc6f1a JH |
54 | * number of items to be removed by each |
55 | * receiver thread from the send-queue | |
e15f7292 | 56 | */ |
c3dc6f1a | 57 | int remove_item_count; |
e15f7292 JH |
58 | }; |
59 | ||
60 | /** | |
61 | * @brief sender thread used in the the send_queue test function | |
c3dc6f1a | 62 | * |
e15f7292 JH |
63 | * @param testinfo informations for the specific thread. |
64 | */ | |
65 | static void test_send_queue_sender(send_queue_test_t * testinfo) | |
66 | { | |
c3dc6f1a | 67 | int i; |
e15f7292 JH |
68 | for (i = 0; i < testinfo->insert_item_count; i++) |
69 | { | |
9317c2a1 | 70 | packet_t *packet = packet_create(AF_INET); |
e15f7292 | 71 | testinfo->tester->assert_true(testinfo->tester,(packet != NULL), "create packet call check"); |
d0cc48e5 | 72 | testinfo->send_queue->add(testinfo->send_queue,packet); |
e15f7292 JH |
73 | } |
74 | } | |
75 | ||
76 | /** | |
77 | * @brief receiver thread used in the the send_queue test function | |
c3dc6f1a | 78 | * |
e15f7292 JH |
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; | |
d0cc48e5 | 87 | packet = testinfo->send_queue->get(testinfo->send_queue); |
e15f7292 JH |
88 | |
89 | testinfo->tester->assert_true(testinfo->tester,( packet != NULL), "packet not NULL call check"); | |
c3dc6f1a | 90 | |
df3c59d0 | 91 | packet->destroy(packet); |
e15f7292 JH |
92 | } |
93 | } | |
94 | ||
95 | /* | |
96 | * description is in header file | |
97 | */ | |
51d56047 | 98 | void test_send_queue(protected_tester_t *tester) |
e15f7292 | 99 | { |
1061c878 | 100 | int desired_value, i; |
e15f7292 JH |
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; | |
c3dc6f1a JH |
112 | |
113 | ||
114 | desired_value = test_infos.insert_item_count * sender_count - | |
e15f7292 | 115 | test_infos.remove_item_count * receiver_count; |
c3dc6f1a | 116 | |
e15f7292 JH |
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 | } | |
c3dc6f1a | 121 | |
e15f7292 JH |
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 | } | |
c3dc6f1a JH |
126 | |
127 | ||
e15f7292 JH |
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 | } | |
c3dc6f1a JH |
137 | |
138 | ||
e15f7292 | 139 | /* the send-queue has to have diserd_value count entries*/ |
1061c878 | 140 | tester->assert_true(tester,(send_queue->get_count(send_queue) == desired_value), "count value check"); |
d048df5c | 141 | send_queue->destroy(send_queue); |
e15f7292 | 142 | } |