]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_frescan/test_frescan_bwres_robjs_timedwait.c
Unified header for FNA
[frescor/fna.git] / tests / tests_frescan / test_frescan_bwres_robjs_timedwait.c
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2009 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FNA (Frescor Network Adaptation)
32 //
33 // FNA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FNA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FNA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FNA header files in a file,
45 // instantiating FNA generics or templates, or linking other files
46 // with FNA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 /*!
53  * @file test_frescan_bwres_robjs_timedwait.c
54  *
55  * @brief test for the basic behaviour of timedwait
56  *
57  * @version 0.01
58  *
59  * @date 1-Apr-2008
60  *
61  * @author
62  *      Daniel Sangorrin <daniel.sangorrin@unican.es>
63  *
64  * @comments
65  *
66  * This file contains a basic test for the frescan_reply_objects module.
67  * The main thread allocates a reply object, creates a thread and waits
68  * on the reply object until the thread signals it. Then it waits again, but
69  * as the thread only signals the reply one time, this tima a timeout will be
70  * produced.
71  *
72  * @license
73  *
74  * See the COPYING file in the FNA's root directory
75  *
76  */
77
78 #include <stdio.h>  /* for printf */
79 #include <assert.h> /* for assert */
80 #include <unistd.h> /* for sleep */
81 #include "fosa_threads_and_signals.h" /* for fosa_thread_xxx */
82 #include "fosa_clocks_and_timers.h" /* for fosa_clock_get_time */
83 #include "frescan_bwres_robjs.h"
84
85 #define CEILING 10
86
87 static void *thread_code(void *arg);
88
89 int main()
90 {
91         int err;
92         frescan_bwres_robj_id_t id;
93         fosa_thread_attr_t th_attr;
94         fosa_thread_id_t tid;
95         struct timespec now;
96
97         printf("TEST REPLY OBJECTS\n");
98
99         err = fosa_thread_set_prio(fosa_thread_self(), CEILING - 2);
100         assert(err == 0);
101
102         err = frescan_bwres_robjs_init(CEILING);
103         assert(err == 0);
104
105         err = frescan_bwres_robjs_alloc(&id, CEILING);
106         assert(err == 0);
107
108         err = fosa_thread_attr_init(&th_attr);
109         assert(err == 0);
110
111         err = fosa_thread_attr_set_prio(&th_attr, CEILING - 1);
112         assert(err == 0);
113
114         err = fosa_thread_create (&tid, &th_attr, thread_code, (void *) id);
115         assert(err == 0);
116
117         err = fosa_thread_attr_destroy(&th_attr);
118         assert(err == 0);
119
120         err = fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &now);
121         assert(err == 0);
122
123         printf("wait: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
124
125         now.tv_sec = now.tv_sec + 2;
126         err = frescan_bwres_robjs_timedwait(id, &now);
127         assert (err == 0);
128
129         err = fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &now);
130         assert(err == 0);
131
132         printf("wait again: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
133
134         now.tv_sec = now.tv_sec + 2;
135         err = frescan_bwres_robjs_timedwait(id, &now);
136         assert (err == FRESCAN_ETIMEDOUT);
137
138         printf("timeout!: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
139
140         err = frescan_bwres_robjs_free(id);
141         assert(err == 0);
142
143         printf("TEST [OK!]\n");
144
145         return 0;
146 }
147
148 static void *thread_code(void *arg)
149 {
150         int err;
151         frescan_bwres_robj_id_t reply = (frescan_bwres_robj_id_t)arg;
152
153         printf("Thread signaling\n");
154         err = frescan_bwres_robjs_signal(reply);
155         assert(err == 0);
156
157         printf("Thread terminating\n");
158         return NULL;
159 }