3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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 2, or (at your option)
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.
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.
25 #include <qa_timeouts.h>
26 #include <cppunit/TestAssert.h>
27 #include <mb_mblock.h>
28 #include <mb_runtime.h>
29 #include <mb_protocol_class.h>
30 #include <mb_exception.h>
31 #include <mb_msg_queue.h>
32 #include <mb_message.h>
33 #include <mb_mblock_impl.h>
34 #include <mb_msg_accepter.h>
35 #include <mb_class_registry.h>
36 #include <mb_timer_queue.h>
42 static pmt_t s_timeout = pmt_intern("%timeout");
43 static pmt_t s_done = pmt_intern("done");
46 // ------------------------------------------------------------------------
47 // Exercise the priority queue used to implement timeouts.
48 // ------------------------------------------------------------------------
50 qa_timeouts::test_timer_queue()
53 mb_msg_accepter_sptr accepter;
55 mb_timeout_sptr t1000_000 =
56 mb_timeout_sptr(new mb_timeout(mb_time(1000,0), PMT_F, accepter));
58 mb_timeout_sptr t2000_000 =
59 mb_timeout_sptr(new mb_timeout(mb_time(2000,0), PMT_F, accepter));
61 mb_timeout_sptr t3000_000 =
62 mb_timeout_sptr(new mb_timeout(mb_time(3000,0), PMT_F, accepter));
64 mb_timeout_sptr t3000_125 =
65 mb_timeout_sptr(new mb_timeout(mb_time(3000,125), PMT_F, accepter));
67 mb_timeout_sptr t3000_250 =
68 mb_timeout_sptr(new mb_timeout(mb_time(3000,250), PMT_F, accepter));
70 mb_timeout_sptr t4000_000 =
71 mb_timeout_sptr(new mb_timeout(mb_time(4000,0), PMT_F, accepter));
73 // insert in pseudo-random order
82 CPPUNIT_ASSERT_EQUAL(t1000_000, tq.top());
85 CPPUNIT_ASSERT_EQUAL(t2000_000, tq.top());
88 CPPUNIT_ASSERT_EQUAL(t3000_000, tq.top());
91 CPPUNIT_ASSERT_EQUAL(t3000_125, tq.top());
94 CPPUNIT_ASSERT_EQUAL(t3000_250, tq.top());
97 CPPUNIT_ASSERT_EQUAL(t4000_000, tq.top());
100 CPPUNIT_ASSERT(tq.empty());
102 // insert in pseudo-random order
111 tq.cancel(t1000_000->handle());
113 CPPUNIT_ASSERT_EQUAL(t2000_000, tq.top());
116 CPPUNIT_ASSERT_EQUAL(t3000_000, tq.top());
119 tq.cancel(t3000_250->handle());
121 CPPUNIT_ASSERT_EQUAL(t3000_125, tq.top());
124 CPPUNIT_ASSERT_EQUAL(t4000_000, tq.top());
127 CPPUNIT_ASSERT(tq.empty());
130 // ------------------------------------------------------------------------
131 // Test one-shot timeouts
132 // ------------------------------------------------------------------------
134 // FWIW, on SuSE 10.1 for x86-64, clock_getres returns 0.004 seconds.
136 #define TIMING_MARGIN 0.010 // seconds
138 class qa_timeouts_1_top : public mb_mblock
145 qa_timeouts_1_top(mb_runtime *runtime,
146 const std::string &instance_name, pmt_t user_arg);
148 void initial_transition();
149 void handle_message(mb_message_sptr msg);
152 qa_timeouts_1_top::qa_timeouts_1_top(mb_runtime *runtime,
153 const std::string &instance_name,
155 : mb_mblock(runtime, instance_name, user_arg),
156 d_nleft(0), d_nerrors(0)
161 qa_timeouts_1_top::initial_transition()
163 d_t0 = mb_time::time(); // now
165 schedule_one_shot_timeout(d_t0 + 0.200, pmt_from_double(0.200));
166 schedule_one_shot_timeout(d_t0 + 0.125, pmt_from_double(0.125));
167 schedule_one_shot_timeout(d_t0 + 0.075, pmt_from_double(0.075));
168 schedule_one_shot_timeout(d_t0 + 0.175, pmt_from_double(0.175));
174 qa_timeouts_1_top::handle_message(mb_message_sptr msg)
176 if (pmt_eq(msg->signal(), s_timeout)){
177 mb_time t_now = mb_time::time();
178 double expected_delta_t = pmt_to_double(msg->data());
179 double actual_delta_t = (t_now - d_t0).double_time();
180 double delta = expected_delta_t - actual_delta_t;
182 if (fabs(delta) > TIMING_MARGIN){
183 std::cerr << "qa_timeouts_1_top: expected_delta_t = " << expected_delta_t
184 << " actual_delta_t = " << actual_delta_t << std::endl;
189 shutdown_all(d_nerrors == 0 ? PMT_T : PMT_F);
193 REGISTER_MBLOCK_CLASS(qa_timeouts_1_top);
196 qa_timeouts::test_timeouts_1()
198 mb_runtime_sptr rt = mb_make_runtime();
199 pmt_t result = PMT_NIL;
201 rt->run("top", "qa_timeouts_1_top", PMT_F, &result);
203 CPPUNIT_ASSERT(pmt_equal(PMT_T, result));
206 // ------------------------------------------------------------------------
207 // Test periodic timeouts
208 // ------------------------------------------------------------------------
210 class qa_timeouts_2_top : public mb_mblock
218 qa_timeouts_2_top(mb_runtime *runtime,
219 const std::string &instance_name, pmt_t user_arg);
221 void initial_transition();
222 void handle_message(mb_message_sptr msg);
225 qa_timeouts_2_top::qa_timeouts_2_top(mb_runtime *runtime,
226 const std::string &instance_name,
228 : mb_mblock(runtime, instance_name, user_arg),
229 d_nhandled(0), d_nerrors(0), d_delta_t(0.075)
234 qa_timeouts_2_top::initial_transition()
236 d_t0 = mb_time::time(); // now
238 schedule_periodic_timeout(d_t0 + d_delta_t, mb_time(d_delta_t), PMT_T);
242 qa_timeouts_2_top::handle_message(mb_message_sptr msg)
244 static const int NMSGS_TO_HANDLE = 5;
246 if (pmt_eq(msg->signal(), s_timeout)
247 && !pmt_eq(msg->data(), s_done)){
249 mb_time t_now = mb_time::time();
253 double expected_delta_t = d_delta_t * d_nhandled;
254 double actual_delta_t = (t_now - d_t0).double_time();
255 double delta = expected_delta_t - actual_delta_t;
257 if (fabs(delta) > TIMING_MARGIN){
258 std::cerr << "qa_timeouts_2_top: expected_delta_t = " << expected_delta_t
259 << " actual_delta_t = " << actual_delta_t << std::endl;
263 if (d_nhandled == NMSGS_TO_HANDLE){
264 cancel_timeout(msg->metadata()); // test cancel_timeout...
265 schedule_one_shot_timeout(d_t0 + (d_delta_t * (d_nhandled + 2)), s_done);
269 if (pmt_eq(msg->signal(), s_timeout)
270 && pmt_eq(msg->data(), s_done)){
271 if (d_nhandled != NMSGS_TO_HANDLE){
272 std::cerr << "qa_timeouts_2_top: d_nhandled = " << d_nhandled
273 << " expected d_nhandled = " << NMSGS_TO_HANDLE
274 << " (cancel_timeout didn't work)\n";
277 shutdown_all(d_nerrors == 0 ? PMT_T : PMT_F);
281 REGISTER_MBLOCK_CLASS(qa_timeouts_2_top);
284 qa_timeouts::test_timeouts_2()
286 mb_runtime_sptr rt = mb_make_runtime();
287 pmt_t result = PMT_NIL;
289 rt->run("top", "qa_timeouts_2_top", PMT_F, &result);
291 CPPUNIT_ASSERT(pmt_equal(PMT_T, result));