Line data Source code
1 : /**
2 : * @file Receiver_test.cxx Receiver class Unit Tests
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : */
8 :
9 : #include "CallbackAdapter.hpp"
10 : #include "ipm/Receiver.hpp"
11 :
12 : #define BOOST_TEST_MODULE Receiver_test // NOLINT
13 :
14 : #include "boost/test/unit_test.hpp"
15 :
16 : #include <string>
17 : #include <vector>
18 :
19 : using namespace dunedaq::ipm;
20 :
21 : BOOST_AUTO_TEST_SUITE(Receiver_test)
22 :
23 : namespace {
24 :
25 : class ReceiverImpl : public Receiver
26 : {
27 :
28 : public:
29 : static const message_size_t s_bytes_on_each_receive = 10;
30 :
31 2 : ReceiverImpl()
32 2 : : m_can_receive(false)
33 : {
34 2 : }
35 :
36 1 : void register_callback(std::function<void(Response&)> callback) override
37 : {
38 1 : m_callback_adapter.set_callback(callback);
39 1 : }
40 2 : void unregister_callback() override { m_callback_adapter.clear_callback(); }
41 :
42 2 : std::string connect_for_receives(const ConnectionInfo& /* connection_info */) override
43 : {
44 2 : m_can_receive = true;
45 2 : m_callback_adapter.set_receiver(this);
46 2 : return "";
47 : }
48 35644 : bool can_receive() const noexcept override { return m_can_receive; }
49 1 : void sabotage_my_receiving_ability()
50 : {
51 1 : unregister_callback();
52 1 : m_can_receive = false;
53 1 : }
54 :
55 0 : bool data_pending() override { return true; }
56 :
57 : protected:
58 35639 : Receiver::Response receive_(const duration_t& /* timeout */, bool /*no_tmoexcept_mode*/) override
59 : {
60 35639 : Receiver::Response output;
61 35639 : output.data = std::vector<char>(s_bytes_on_each_receive, 'A');
62 35639 : output.metadata = "";
63 35639 : return output;
64 0 : }
65 :
66 : private:
67 : bool m_can_receive;
68 : CallbackAdapter m_callback_adapter;
69 : };
70 :
71 : } // namespace ""
72 :
73 2 : BOOST_AUTO_TEST_CASE(CopyAndMoveSemantics)
74 : {
75 1 : BOOST_REQUIRE(!std::is_copy_constructible_v<ReceiverImpl>);
76 1 : BOOST_REQUIRE(!std::is_copy_assignable_v<ReceiverImpl>);
77 1 : BOOST_REQUIRE(!std::is_move_constructible_v<ReceiverImpl>);
78 1 : BOOST_REQUIRE(!std::is_move_assignable_v<ReceiverImpl>);
79 1 : }
80 :
81 2 : BOOST_AUTO_TEST_CASE(Timeouts)
82 : {
83 1 : BOOST_REQUIRE_EQUAL(Receiver::s_no_block.count(), 0);
84 1 : BOOST_REQUIRE_GT(Receiver::s_block.count(), 3600000); // "Blocking" must be at least an hour!
85 1 : }
86 :
87 2 : BOOST_AUTO_TEST_CASE(StatusChecks)
88 : {
89 1 : ReceiverImpl the_receiver;
90 :
91 1 : BOOST_REQUIRE(!the_receiver.can_receive());
92 :
93 1 : Receiver::ConnectionInfo ci;
94 1 : the_receiver.connect_for_receives(ci);
95 1 : BOOST_REQUIRE(the_receiver.can_receive());
96 :
97 1 : BOOST_REQUIRE_NO_THROW(the_receiver.receive(Receiver::s_no_block));
98 1 : BOOST_REQUIRE_NO_THROW(the_receiver.receive(Receiver::s_no_block, ReceiverImpl::s_bytes_on_each_receive));
99 :
100 1 : BOOST_REQUIRE_EXCEPTION(the_receiver.receive(Receiver::s_no_block, ReceiverImpl::s_bytes_on_each_receive - 1),
101 : dunedaq::ipm::UnexpectedNumberOfBytes,
102 : [&](dunedaq::ipm::UnexpectedNumberOfBytes) { return true; });
103 :
104 1 : the_receiver.sabotage_my_receiving_ability();
105 1 : BOOST_REQUIRE(!the_receiver.can_receive());
106 :
107 1 : BOOST_REQUIRE_EXCEPTION(the_receiver.receive(Receiver::s_no_block),
108 : dunedaq::ipm::KnownStateForbidsReceive,
109 : [&](dunedaq::ipm::KnownStateForbidsReceive) { return true; });
110 1 : }
111 :
112 2 : BOOST_AUTO_TEST_CASE(Callback)
113 : {
114 1 : ReceiverImpl the_receiver;
115 :
116 1 : Receiver::ConnectionInfo ci;
117 1 : the_receiver.connect_for_receives(ci);
118 1 : BOOST_REQUIRE(the_receiver.can_receive());
119 :
120 1 : std::atomic<size_t> callback_call_count = 0;
121 35637 : auto callback_fun = [&](Receiver::Response&) { callback_call_count++; }; // NOLINT
122 :
123 1 : the_receiver.register_callback(callback_fun);
124 1 : usleep(10000);
125 1 : the_receiver.unregister_callback();
126 :
127 1 : BOOST_REQUIRE_GT(callback_call_count, 0);
128 1 : }
129 :
130 : BOOST_AUTO_TEST_SUITE_END()
|