Convert gr-audio-portaudio to Boost via gruel
[debian/gnuradio] / mblock / src / lib / qa_timeouts.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <qa_timeouts.h>
26 #include <cppunit/TestAssert.h>
27 #include <mblock/mblock.h>
28 #include <mblock/runtime.h>
29 #include <mblock/protocol_class.h>
30 #include <mblock/message.h>
31 #include <mblock/msg_accepter.h>
32 #include <mblock/class_registry.h>
33 #include <mb_timer_queue.h>
34 #include <string.h>
35 #include <iostream>
36
37 using namespace pmt;
38
39 static pmt_t s_timeout = pmt_intern("%timeout");
40 static pmt_t s_done = pmt_intern("done");
41
42
43 // ------------------------------------------------------------------------
44 //    Exercise the priority queue used to implement timeouts.
45 // ------------------------------------------------------------------------
46 void
47 qa_timeouts::test_timer_queue()
48 {
49   mb_timer_queue        tq;
50   mb_msg_accepter_sptr  accepter;
51
52   mb_timeout_sptr       t1000_000 =
53     mb_timeout_sptr(new mb_timeout(mb_time(1000,0), PMT_F, accepter));
54
55   mb_timeout_sptr       t2000_000 =
56     mb_timeout_sptr(new mb_timeout(mb_time(2000,0), PMT_F, accepter));
57                                                                     
58   mb_timeout_sptr       t3000_000 =
59     mb_timeout_sptr(new mb_timeout(mb_time(3000,0), PMT_F, accepter));
60                                                                     
61   mb_timeout_sptr       t3000_125 =
62     mb_timeout_sptr(new mb_timeout(mb_time(3000,125), PMT_F, accepter));
63                                                                     
64   mb_timeout_sptr       t3000_250 =
65     mb_timeout_sptr(new mb_timeout(mb_time(3000,250), PMT_F, accepter));
66                                                                     
67   mb_timeout_sptr       t4000_000 =
68     mb_timeout_sptr(new mb_timeout(mb_time(4000,0), PMT_F, accepter));
69                                                                     
70   // insert in pseudo-random order
71
72   tq.push(t3000_125);
73   tq.push(t1000_000);
74   tq.push(t4000_000);
75   tq.push(t3000_250);
76   tq.push(t2000_000);
77   tq.push(t3000_000);
78
79   CPPUNIT_ASSERT_EQUAL(t1000_000, tq.top());
80   tq.pop();
81   
82   CPPUNIT_ASSERT_EQUAL(t2000_000, tq.top());
83   tq.pop();
84   
85   CPPUNIT_ASSERT_EQUAL(t3000_000, tq.top());
86   tq.pop();
87   
88   CPPUNIT_ASSERT_EQUAL(t3000_125, tq.top());
89   tq.pop();
90   
91   CPPUNIT_ASSERT_EQUAL(t3000_250, tq.top());
92   tq.pop();
93   
94   CPPUNIT_ASSERT_EQUAL(t4000_000, tq.top());
95   tq.pop();
96
97   CPPUNIT_ASSERT(tq.empty());
98
99   // insert in pseudo-random order
100
101   tq.push(t3000_000);
102   tq.push(t4000_000);
103   tq.push(t3000_125);
104   tq.push(t1000_000);
105   tq.push(t2000_000);
106   tq.push(t3000_250);
107
108   tq.cancel(t1000_000->handle());
109
110   CPPUNIT_ASSERT_EQUAL(t2000_000, tq.top());
111   tq.pop();
112   
113   CPPUNIT_ASSERT_EQUAL(t3000_000, tq.top());
114   tq.pop();
115   
116   tq.cancel(t3000_250->handle());
117
118   CPPUNIT_ASSERT_EQUAL(t3000_125, tq.top());
119   tq.pop();
120   
121   CPPUNIT_ASSERT_EQUAL(t4000_000, tq.top());
122   tq.pop();
123   
124   CPPUNIT_ASSERT(tq.empty());
125 }
126
127 // ------------------------------------------------------------------------
128 //   Test one-shot timeouts
129 // ------------------------------------------------------------------------
130
131 // FWIW, on SuSE 10.1 for x86-64, clock_getres returns 0.004 seconds.
132
133 // #define TIMING_MARGIN 0.010  // seconds   // was failing on some systems
134 #define TIMING_MARGIN 0.025     // seconds  (really sloppy; consider enabling RT scheduler)
135
136
137 class qa_timeouts_1_top : public mb_mblock
138 {
139   int           d_nleft;
140   int           d_nerrors;
141   mb_time       d_t0;
142   
143 public:
144   qa_timeouts_1_top(mb_runtime *runtime,
145                     const std::string &instance_name, pmt_t user_arg);
146
147   void initial_transition();
148   void handle_message(mb_message_sptr msg);
149 };
150
151 qa_timeouts_1_top::qa_timeouts_1_top(mb_runtime *runtime,
152                                      const std::string &instance_name,
153                                      pmt_t user_arg)
154   : mb_mblock(runtime, instance_name, user_arg),
155     d_nleft(0), d_nerrors(0)
156 {
157 }
158
159 void
160 qa_timeouts_1_top::initial_transition()
161 {
162   d_t0 = mb_time::time();       // now
163
164   schedule_one_shot_timeout(d_t0 + 0.200, pmt_from_double(0.200));
165   schedule_one_shot_timeout(d_t0 + 0.125, pmt_from_double(0.125));
166   schedule_one_shot_timeout(d_t0 + 0.075, pmt_from_double(0.075));
167   schedule_one_shot_timeout(d_t0 + 0.175, pmt_from_double(0.175));
168
169   d_nleft = 4;
170 }
171
172 void
173 qa_timeouts_1_top::handle_message(mb_message_sptr msg)
174 {
175   if (pmt_eq(msg->signal(), s_timeout)){
176     mb_time t_now = mb_time::time();
177     double expected_delta_t = pmt_to_double(msg->data());
178     double actual_delta_t = (t_now - d_t0).double_time();
179     double delta = expected_delta_t - actual_delta_t;
180
181     if (fabs(delta) > TIMING_MARGIN){
182       std::cerr << "qa_timeouts_1_top: expected_delta_t = " << expected_delta_t
183                 << " actual_delta_t = " << actual_delta_t << std::endl;
184       d_nerrors++;
185     }
186
187     if (--d_nleft <= 0)
188       shutdown_all(d_nerrors == 0 ? PMT_T : PMT_F);
189   }
190 }
191
192 REGISTER_MBLOCK_CLASS(qa_timeouts_1_top);
193
194 void
195 qa_timeouts::test_timeouts_1()
196 {
197   mb_runtime_sptr rt = mb_make_runtime();
198   pmt_t result = PMT_NIL;
199
200   rt->run("top", "qa_timeouts_1_top", PMT_F, &result);
201
202   CPPUNIT_ASSERT(pmt_equal(PMT_T, result));
203 }
204
205 // ------------------------------------------------------------------------
206 //   Test periodic timeouts
207 // ------------------------------------------------------------------------
208
209 class qa_timeouts_2_top : public mb_mblock
210 {
211   int           d_nhandled;
212   int           d_nerrors;
213   double        d_delta_t;
214   mb_time       d_t0;
215   
216 public:
217   qa_timeouts_2_top(mb_runtime *runtime,
218                     const std::string &instance_name, pmt_t user_arg);
219
220   void initial_transition();
221   void handle_message(mb_message_sptr msg);
222 };
223
224 qa_timeouts_2_top::qa_timeouts_2_top(mb_runtime *runtime,
225                                      const std::string &instance_name,
226                                      pmt_t user_arg)
227   : mb_mblock(runtime, instance_name, user_arg),
228     d_nhandled(0), d_nerrors(0), d_delta_t(0.075)
229 {
230 }
231
232 void
233 qa_timeouts_2_top::initial_transition()
234 {
235   d_t0 = mb_time::time();       // now
236
237   schedule_periodic_timeout(d_t0 + d_delta_t, mb_time(d_delta_t), PMT_T);
238 }
239
240 void
241 qa_timeouts_2_top::handle_message(mb_message_sptr msg)
242 {
243   static const int NMSGS_TO_HANDLE = 5;
244
245   if (pmt_eq(msg->signal(), s_timeout)
246       && !pmt_eq(msg->data(), s_done)){
247
248     mb_time t_now = mb_time::time();
249
250     d_nhandled++;
251
252     double expected_delta_t = d_delta_t * d_nhandled;
253     double actual_delta_t = (t_now - d_t0).double_time();
254     double delta = expected_delta_t - actual_delta_t;
255
256     if (fabs(delta) > TIMING_MARGIN){
257       std::cerr << "qa_timeouts_2_top: expected_delta_t = " << expected_delta_t
258                 << " actual_delta_t = " << actual_delta_t << std::endl;
259       d_nerrors++;
260     }
261
262     if (d_nhandled == NMSGS_TO_HANDLE){
263       cancel_timeout(msg->metadata());  // test cancel_timeout...
264       schedule_one_shot_timeout(d_t0 + (d_delta_t * (d_nhandled + 2)), s_done);
265     }
266   }
267
268   if (pmt_eq(msg->signal(), s_timeout)
269       && pmt_eq(msg->data(), s_done)){
270     if (d_nhandled != NMSGS_TO_HANDLE){
271       std::cerr << "qa_timeouts_2_top: d_nhandled = " << d_nhandled
272                 << " expected d_nhandled = " << NMSGS_TO_HANDLE
273                 << " (cancel_timeout didn't work)\n";
274       d_nerrors++;
275     }
276     shutdown_all(d_nerrors == 0 ? PMT_T : PMT_F);
277   }
278 }
279
280 REGISTER_MBLOCK_CLASS(qa_timeouts_2_top);
281
282 void
283 qa_timeouts::test_timeouts_2()
284 {
285   mb_runtime_sptr rt = mb_make_runtime();
286   pmt_t result = PMT_NIL;
287
288   rt->run("top", "qa_timeouts_2_top", PMT_F, &result);
289
290   CPPUNIT_ASSERT(pmt_equal(PMT_T, result));
291 }