]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_frescan/test_frescan_bwres_robjs_timedwait.c
b0b0cec5348f46e92f4654ccc7a97a120bdbb644
[frescor/fna.git] / tests / tests_frescan / test_frescan_bwres_robjs_timedwait.c
1 /*!
2  * @file test_frescan_bwres_robjs_timedwait.c
3  *
4  * @brief test for the basic behaviour of timedwait
5  *
6  * @version 0.01
7  *
8  * @date 1-Apr-2008
9  *
10  * @author
11  *      Daniel Sangorrin <daniel.sangorrin@unican.es>
12  *
13  * @comments
14  *
15  * This file contains a basic test for the frescan_reply_objects module.
16  * The main thread allocates a reply object, creates a thread and waits
17  * on the reply object until the thread signals it. Then it waits again, but
18  * as the thread only signals the reply one time, this tima a timeout will be
19  * produced.
20  *
21  * @license
22  *
23  * See the COPYING file in the FNA's root directory
24  *
25  */
26
27 #include <stdio.h>  /* for printf */
28 #include <assert.h> /* for assert */
29 #include <unistd.h> /* for sleep */
30 #include "fosa_threads_and_signals.h" /* for fosa_thread_xxx */
31 #include "fosa_clocks_and_timers.h" /* for fosa_clock_get_time */
32 #include "frescan_bwres_robjs.h"
33
34 #define CEILING 10
35
36 static void *thread_code(void *arg);
37
38 int main()
39 {
40         int err;
41         frescan_robj_id_t id;
42         fosa_thread_attr_t th_attr;
43         fosa_thread_id_t tid;
44         struct timespec now;
45
46         printf("TEST REPLY OBJECTS\n");
47
48         err = fosa_thread_set_prio(fosa_thread_self(), CEILING - 2);
49         assert(err == 0);
50
51         err = frescan_bwres_robjs_init(CEILING);
52         assert(err == 0);
53
54         err = frescan_bwres_robjs_alloc(&id, CEILING);
55         assert(err == 0);
56
57         err = fosa_thread_attr_init(&th_attr);
58         assert(err == 0);
59
60         err = fosa_thread_attr_set_prio(&th_attr, CEILING - 1);
61         assert(err == 0);
62
63         err = fosa_thread_create (&tid, &th_attr, thread_code, (void *) id);
64         assert(err == 0);
65
66         err = fosa_thread_attr_destroy(&th_attr);
67         assert(err == 0);
68
69         err = fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &now);
70         assert(err == 0);
71
72         printf("wait: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
73
74         now.tv_sec = now.tv_sec + 2;
75         err = frescan_bwres_robjs_timedwait(id, &now);
76         assert (err == 0);
77
78         err = fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &now);
79         assert(err == 0);
80
81         printf("wait again: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
82
83         now.tv_sec = now.tv_sec + 2;
84         err = frescan_bwres_robjs_timedwait(id, &now);
85         assert (err == FRESCAN_ETIMEDOUT);
86
87         printf("timeout!: %d sec %d nsec\n", now.tv_sec, now.tv_nsec);
88
89         err = frescan_bwres_robjs_free(id);
90         assert(err == 0);
91
92         printf("TEST [OK!]\n");
93
94         return 0;
95 }
96
97 static void *thread_code(void *arg)
98 {
99         int err;
100         frescan_robj_id_t reply = (frescan_robj_id_t)arg;
101
102         printf("Thread signaling\n");
103         err = frescan_bwres_robjs_signal(reply);
104         assert(err == 0);
105
106         printf("Thread terminating\n");
107         return NULL;
108 }