Mercurial > hg > freeDiameter
annotate tests/testfifo.c @ 662:2e94ef0515d7 1.1.0-rc1
Updated copyright information
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Fri, 14 Jan 2011 16:27:21 +0900 |
parents | f198d16fa7f4 |
children | 4784f2cf325f |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * | |
4 * * | |
662
2e94ef0515d7
Updated copyright information
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
658
diff
changeset
|
5 * Copyright (c) 2011, WIDE Project and NICT * |
0 | 6 * All rights reserved. * |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
36 #include "tests.h" | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
37 #include <unistd.h> |
0 | 38 |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
39 /* Structure for testing threshold function */ |
0 | 40 static struct thrh_test { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
41 struct fifo * queue; /* pointer to the queue */ |
0 | 42 int h_calls; /* number of calls of h_cb */ |
43 int l_calls; /* number of calls of l_cb */ | |
44 } thrh_td; | |
45 | |
46 /* Callbacks for threasholds test */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
47 void thrh_cb_h(struct fifo *queue, void **data) |
0 | 48 { |
49 if (thrh_td.h_calls == thrh_td.l_calls) { | |
50 CHECK( NULL, *data ); | |
51 *data = &thrh_td; | |
52 } else { | |
53 CHECK( *data, &thrh_td ); | |
54 } | |
55 CHECK( queue, thrh_td.queue ); | |
56 | |
57 /* Update the count */ | |
58 thrh_td.h_calls ++; | |
59 } | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
60 void thrh_cb_l(struct fifo *queue, void **data) |
0 | 61 { |
62 CHECK( 1, data ? 1 : 0 ); | |
63 CHECK( *data, &thrh_td ); | |
64 | |
65 /* Check the queue parameter is correct */ | |
66 CHECK( queue, thrh_td.queue ); | |
67 | |
68 /* Update the count */ | |
69 thrh_td.l_calls ++; | |
70 /* Cleanup the data ptr if needed */ | |
71 if (thrh_td.l_calls == thrh_td.h_calls) | |
72 *data = NULL; | |
73 /* done */ | |
74 } | |
75 | |
76 | |
77 /* Structure that is passed to the test function */ | |
78 struct test_data { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
79 struct fifo * queue; /* pointer to the queue */ |
0 | 80 pthread_barrier_t * bar; /* if not NULL, barrier to synchronize before getting messages */ |
81 struct timespec * ts; /* if not NULL, use a timedget instead of a get */ | |
82 int nbr; /* number of messages to retrieve from the queue */ | |
83 }; | |
84 | |
85 /* The test function, to be threaded */ | |
86 static void * test_fct(void * data) | |
87 { | |
88 int ret = 0, i; | |
89 struct msg * msg = NULL; | |
90 struct test_data * td = (struct test_data *) data; | |
91 | |
92 if (td->bar != NULL) { | |
93 ret = pthread_barrier_wait(td->bar); | |
94 if (ret != PTHREAD_BARRIER_SERIAL_THREAD) { | |
95 CHECK( 0, ret); | |
96 } else { | |
97 CHECK( PTHREAD_BARRIER_SERIAL_THREAD, ret); /* just for the traces */ | |
98 } | |
99 } | |
100 | |
101 for (i=0; i< td->nbr; i++) { | |
102 if (td->ts != NULL) { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
103 CHECK( 0, fd_fifo_timedget(td->queue, &msg, td->ts) ); |
0 | 104 } else { |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
105 CHECK( 0, fd_fifo_get(td->queue, &msg) ); |
0 | 106 } |
107 } | |
108 | |
109 return NULL; | |
110 } | |
111 | |
112 | |
113 /* Main test routine */ | |
114 int main(int argc, char *argv[]) | |
115 { | |
116 struct timespec ts; | |
117 | |
118 struct msg * msg1 = NULL; | |
119 struct msg * msg2 = NULL; | |
120 struct msg * msg3 = NULL; | |
121 | |
122 /* First, initialize the daemon modules */ | |
123 INIT_FD(); | |
124 | |
125 /* Prolog: create the messages */ | |
126 { | |
127 struct dict_object * acr_model = NULL; | |
128 struct dict_object * cer_model = NULL; | |
129 struct dict_object * dwr_model = NULL; | |
130 | |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
131 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Accounting-Request", &acr_model, ENOENT ) ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
132 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Capabilities-Exchange-Request", &cer_model, ENOENT ) ); |
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
133 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Device-Watchdog-Request", &dwr_model, ENOENT ) ); |
0 | 134 CHECK( 0, fd_msg_new ( acr_model, 0, &msg1 ) ); |
135 CHECK( 0, fd_msg_new ( cer_model, 0, &msg2 ) ); | |
136 CHECK( 0, fd_msg_new ( dwr_model, 0, &msg3 ) ); | |
137 } | |
138 | |
139 /* Basic operation */ | |
140 { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
141 struct fifo * queue = NULL; |
0 | 142 int count; |
143 struct msg * msg = NULL; | |
144 | |
145 /* Create the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
146 CHECK( 0, fd_fifo_new(&queue) ); |
0 | 147 |
148 /* Check the count is 0 */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
149 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 150 CHECK( 0, count); |
151 | |
152 /* Now enqueue */ | |
153 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
154 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 155 msg = msg2; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
156 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 157 msg = msg3; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
158 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 159 |
160 /* Check the count is 3 */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
161 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 162 CHECK( 3, count); |
163 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
164 /* Retrieve the first message using fd_fifo_get */ |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
165 CHECK( 0, fd_fifo_get(queue, &msg) ); |
0 | 166 CHECK( msg1, msg); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
167 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 168 CHECK( 2, count); |
169 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
170 /* Retrieve the second message using fd_fifo_timedget */ |
0 | 171 CHECK(0, clock_gettime(CLOCK_REALTIME, &ts)); |
172 ts.tv_sec += 1; /* Set the timeout to 1 second */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
173 CHECK( 0, fd_fifo_timedget(queue, &msg, &ts) ); |
0 | 174 CHECK( msg2, msg); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
175 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 176 CHECK( 1, count); |
177 | |
178 /* Retrieve the third message using meq_tryget */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
179 CHECK( 0, fd_fifo_tryget(queue, &msg) ); |
0 | 180 CHECK( msg3, msg); |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
181 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 182 CHECK( 0, count); |
183 | |
184 /* Check that another meq_tryget does not block */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
185 CHECK( EWOULDBLOCK, fd_fifo_tryget(queue, &msg) ); |
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
186 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 187 CHECK( 0, count); |
188 | |
189 /* We're done for basic tests */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
190 CHECK( 0, fd_fifo_del(&queue) ); |
0 | 191 } |
192 | |
193 /* Test robustness, ensure no messages are lost */ | |
194 { | |
195 #define NBR_MSG 200 | |
196 #define NBR_THREADS 60 | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
197 struct fifo *queue = NULL; |
0 | 198 pthread_barrier_t bar; |
199 struct test_data td_1; | |
200 struct test_data td_2; | |
201 struct msg *msgs[NBR_MSG * NBR_THREADS * 2], *msg; | |
202 pthread_t thr [NBR_THREADS * 2]; | |
203 struct dict_object *dwr_model = NULL; | |
204 int count; | |
205 int i; | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
206 int nbr_threads; |
619
45a63d6448ea
Fix wrong operator in testfifo
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
615
diff
changeset
|
207 #ifdef _POSIX_THREAD_THREADS_MAX |
45a63d6448ea
Fix wrong operator in testfifo
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
615
diff
changeset
|
208 nbr_threads = _POSIX_THREAD_THREADS_MAX; |
45a63d6448ea
Fix wrong operator in testfifo
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
615
diff
changeset
|
209 #else /* _POSIX_THREAD_THREADS_MAX */ |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
210 nbr_threads = sysconf(_SC_THREAD_THREADS_MAX); |
619
45a63d6448ea
Fix wrong operator in testfifo
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
615
diff
changeset
|
211 #endif /* _POSIX_THREAD_THREADS_MAX */ |
45a63d6448ea
Fix wrong operator in testfifo
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
615
diff
changeset
|
212 if ((nbr_threads <= 0) || (nbr_threads > NBR_THREADS * 2)) { |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
213 nbr_threads = NBR_THREADS; |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
214 } else { |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
215 /* The local limit is bellow NBR_THREADS */ |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
216 nbr_threads = (nbr_threads / 2) - 1; |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
217 /* Ensure we create at least a few threads! */ |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
218 CHECK( 1, nbr_threads >= 10 ? 1 : 0 ); |
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
219 } |
0 | 220 |
221 /* Create the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
222 CHECK( 0, fd_fifo_new(&queue) ); |
0 | 223 |
224 /* Create the barrier */ | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
225 CHECK( 0, pthread_barrier_init(&bar, NULL, nbr_threads * 2 + 1) ); |
0 | 226 |
227 /* Initialize the ts */ | |
228 CHECK(0, clock_gettime(CLOCK_REALTIME, &ts)); | |
494
6e9c1ea44262
Increase timer for the test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
229 ts.tv_sec += 20; /* Set the timeout to 20 second */ |
0 | 230 |
231 /* Create the messages */ | |
10
c5c99c73c2bf
Added some extensions and functions in the daemon
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
8
diff
changeset
|
232 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Device-Watchdog-Request", &dwr_model, ENOENT ) ); |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
233 for (i = 0; i < NBR_MSG * nbr_threads * 2; i++) { |
0 | 234 CHECK( 0, fd_msg_new ( dwr_model, 0, &msgs[i] ) ); |
235 } | |
236 | |
237 /* Initialize the test data structures */ | |
238 td_1.queue = queue; | |
239 td_1.bar = &bar; | |
240 td_1.ts = &ts; | |
241 td_1.nbr = NBR_MSG; | |
242 td_2.queue = queue; | |
243 td_2.bar = &bar; | |
244 td_2.ts = NULL; | |
245 td_2.nbr = NBR_MSG; | |
246 | |
247 /* Create the threads */ | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
248 for (i=0; i < nbr_threads * 2; i++) { |
0 | 249 CHECK( 0, pthread_create( &thr[i], NULL, test_fct, (i & 1) ? &td_1 : &td_2 ) ); |
250 } | |
251 | |
252 /* Synchronize everyone */ | |
253 { | |
254 int ret = pthread_barrier_wait(&bar); | |
255 if (ret != PTHREAD_BARRIER_SERIAL_THREAD) { | |
256 CHECK( 0, ret); | |
257 } else { | |
258 CHECK( PTHREAD_BARRIER_SERIAL_THREAD, ret); /* for trace only */ | |
259 } | |
260 } | |
261 | |
262 /* Now post all the messages */ | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
263 for (i=0; i < NBR_MSG * nbr_threads * 2; i++) { |
0 | 264 msg = msgs[i]; |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
265 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 266 } |
267 | |
268 /* Join all threads. This blocks if messages are lost... */ | |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
269 for (i=0; i < nbr_threads * 2; i++) { |
0 | 270 CHECK( 0, pthread_join( thr[i], NULL ) ); |
271 } | |
272 | |
273 /* Check the count of the queue is back to 0 */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
274 CHECK( 0, fd_fifo_length(queue, &count) ); |
0 | 275 CHECK( 0, count); |
276 | |
277 /* Destroy this queue and the messages */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
278 CHECK( 0, fd_fifo_del(&queue) ); |
615
8abafd66a617
Adapt test for systems that cannot create many threads per process, such as OpenSUSE (bouuuuh)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
494
diff
changeset
|
279 for (i=0; i < NBR_MSG * nbr_threads * 2; i++) { |
0 | 280 CHECK( 0, fd_msg_free( msgs[i] ) ); |
281 } | |
282 } | |
283 | |
284 /* Test thread cancelation */ | |
285 { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
286 struct fifo *queue = NULL; |
0 | 287 pthread_barrier_t bar; |
288 struct test_data td; | |
289 pthread_t th; | |
290 | |
291 /* Create the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
292 CHECK( 0, fd_fifo_new(&queue) ); |
0 | 293 |
294 /* Create the barrier */ | |
295 CHECK( 0, pthread_barrier_init(&bar, NULL, 2) ); | |
296 | |
297 /* Initialize the ts */ | |
298 CHECK(0, clock_gettime(CLOCK_REALTIME, &ts)); | |
494
6e9c1ea44262
Increase timer for the test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
10
diff
changeset
|
299 ts.tv_sec += 10; /* Set the timeout to 10 second */ |
0 | 300 |
301 /* Initialize the test data structures */ | |
302 td.queue = queue; | |
303 td.bar = &bar; | |
304 td.ts = &ts; | |
305 td.nbr = 1; | |
306 | |
307 /* Create the thread */ | |
308 CHECK( 0, pthread_create( &th, NULL, test_fct, &td ) ); | |
309 | |
310 /* Wait for the thread to be running */ | |
311 { | |
312 int ret = pthread_barrier_wait(&bar); | |
313 if (ret != PTHREAD_BARRIER_SERIAL_THREAD) { | |
314 CHECK( 0, ret); | |
315 } else { | |
316 CHECK( PTHREAD_BARRIER_SERIAL_THREAD, ret ); | |
317 } | |
318 } | |
319 | |
320 /* Now cancel the thread */ | |
321 CHECK( 0, pthread_cancel( th ) ); | |
322 | |
323 /* Join it */ | |
324 CHECK( 0, pthread_join( th, NULL ) ); | |
325 | |
326 /* Do the same with the other function */ | |
327 td.ts = NULL; | |
328 | |
329 /* Create the thread */ | |
330 CHECK( 0, pthread_create( &th, NULL, test_fct, &td ) ); | |
331 | |
332 /* Wait for the thread to be running */ | |
333 { | |
334 int ret = pthread_barrier_wait(&bar); | |
335 if (ret != PTHREAD_BARRIER_SERIAL_THREAD) { | |
336 CHECK( 0, ret); | |
337 } else { | |
338 CHECK( PTHREAD_BARRIER_SERIAL_THREAD, ret ); | |
339 } | |
340 } | |
341 | |
342 /* Now cancel the thread */ | |
343 CHECK( 0, pthread_cancel( th ) ); | |
344 | |
345 /* Join it */ | |
346 CHECK( 0, pthread_join( th, NULL ) ); | |
347 | |
348 /* Destroy the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
349 CHECK( 0, fd_fifo_del(&queue) ); |
0 | 350 } |
351 | |
352 /* Test the threashold function */ | |
353 { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
354 struct fifo * queue = NULL; |
0 | 355 int i; |
356 struct msg * msg = NULL; | |
357 | |
358 /* Create the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
359 CHECK( 0, fd_fifo_new(&queue) ); |
0 | 360 |
361 /* Prepare the test data */ | |
362 memset(&thrh_td, 0, sizeof(thrh_td)); | |
363 thrh_td.queue = queue; | |
364 | |
365 /* Set the thresholds for the queue */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
366 CHECK( 0, fd_fifo_setthrhd ( queue, NULL, 6, thrh_cb_h, 4, thrh_cb_l ) ); |
0 | 367 |
368 /* Post 5 messages, no cb must be called. */ | |
369 for (i=0; i<5; i++) { | |
370 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
371 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 372 } /* 5 msg in queue */ |
373 CHECK( 0, thrh_td.h_calls ); | |
374 CHECK( 0, thrh_td.l_calls ); | |
375 | |
376 /* Get all these messages, and check again */ | |
377 for (i=0; i<5; i++) { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
378 CHECK( 0, fd_fifo_get(queue, &msg) ); |
0 | 379 } /* 0 msg in queue */ |
380 CHECK( 0, thrh_td.h_calls ); | |
381 CHECK( 0, thrh_td.l_calls ); | |
382 | |
383 /* Now, post 6 messages, the high threashold */ | |
384 for (i=0; i<6; i++) { | |
385 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
386 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 387 } /* 6 msg in queue */ |
388 CHECK( 1, thrh_td.h_calls ); | |
389 CHECK( 0, thrh_td.l_calls ); | |
390 | |
391 /* Remove 2 messages, to reach the low threshold */ | |
392 for (i=0; i<2; i++) { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
393 CHECK( 0, fd_fifo_get(queue, &msg) ); |
0 | 394 } /* 4 msg in queue */ |
395 CHECK( 1, thrh_td.h_calls ); | |
396 CHECK( 1, thrh_td.l_calls ); | |
397 | |
398 /* Come again at the high threshold */ | |
399 for (i=0; i<2; i++) { | |
400 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
401 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 402 } /* 6 msg in queue */ |
403 CHECK( 2, thrh_td.h_calls ); | |
404 CHECK( 1, thrh_td.l_calls ); | |
405 | |
406 /* Suppose the queue continues to grow */ | |
407 for (i=0; i<6; i++) { | |
408 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
409 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 410 } /* 12 msg in queue */ |
411 CHECK( 3, thrh_td.h_calls ); | |
412 CHECK( 1, thrh_td.l_calls ); | |
413 for (i=0; i<5; i++) { | |
414 msg = msg1; | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
415 CHECK( 0, fd_fifo_post(queue, &msg) ); |
0 | 416 } /* 17 msg in queue */ |
417 CHECK( 3, thrh_td.h_calls ); | |
418 CHECK( 1, thrh_td.l_calls ); | |
419 | |
420 /* Now the queue goes back to 0 messages */ | |
421 for (i=0; i<17; i++) { | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
422 CHECK( 0, fd_fifo_get(queue, &msg) ); |
0 | 423 } /* 0 msg in queue */ |
424 CHECK( 3, thrh_td.h_calls ); | |
425 CHECK( 3, thrh_td.l_calls ); | |
426 | |
427 /* We're done for this test */ | |
8
3e143f047f78
Backup for the week-end
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
428 CHECK( 0, fd_fifo_del(&queue) ); |
0 | 429 } |
430 | |
431 /* Delete the messages */ | |
432 CHECK( 0, fd_msg_free( msg1 ) ); | |
433 CHECK( 0, fd_msg_free( msg2 ) ); | |
434 CHECK( 0, fd_msg_free( msg3 ) ); | |
435 | |
436 /* That's all for the tests yet */ | |
437 PASSTEST(); | |
438 } |