1+ /*
2+ *
3+ * Copyright 2021-2024 Software Radio Systems Limited
4+ *
5+ * By using this file, you agree to the terms and conditions set
6+ * forth in the LICENSE file which can be found at the top level of
7+ * the distribution.
8+ *
9+ */
10+
11+ #include " srsran/support/executors/manual_task_worker.h"
12+ #include " srsran/support/io/io_broker_factory.h"
13+ #include " srsran/support/io/io_timer_source.h"
14+ #include " srsran/support/timers.h"
15+ #include < gtest/gtest.h>
16+ #include < optional>
17+
18+ using namespace srsran ;
19+
20+ class io_timer_source_test : public ::testing::Test
21+ {
22+ public:
23+ manual_task_worker worker{16 };
24+ timer_manager timers{16 };
25+ std::unique_ptr<io_broker> broker = create_io_broker(srsran::io_broker_type::epoll);
26+
27+ void start () { source.emplace (timers, *broker, std::chrono::milliseconds{1 }); }
28+
29+ void stop () { source.reset (); }
30+
31+ std::optional<io_timer_source> source;
32+ };
33+
34+ TEST_F (io_timer_source_test, timer_gets_ticked_when_source_starts)
35+ {
36+ std::chrono::milliseconds run_duration{100 };
37+ std::chrono::milliseconds timer_period{5 };
38+ unsigned count = 0 ;
39+ unique_timer t = timers.create_unique_timer (worker);
40+ t.set (timer_period, [&count, &t](timer_id_t tid) {
41+ count++;
42+ t.run ();
43+ });
44+ t.run ();
45+
46+ std::this_thread::sleep_for (std::chrono::milliseconds{10 });
47+ worker.run_pending_tasks ();
48+ ASSERT_EQ (count, 0 );
49+
50+ start ();
51+ for (unsigned i = 0 ; i != run_duration.count (); i++) {
52+ std::this_thread::sleep_for (std::chrono::milliseconds{1 });
53+ worker.run_pending_tasks ();
54+ }
55+ ASSERT_GT (count, 1 );
56+ fmt::print (" Tick count: expected={} actual={}\n " , run_duration / timer_period, count);
57+
58+ stop ();
59+ count = 0 ;
60+ std::this_thread::sleep_for (std::chrono::milliseconds{10 });
61+ worker.run_pending_tasks ();
62+ ASSERT_EQ (count, 0 );
63+ }
0 commit comments