Line data Source code
1 : /**
2 : * @file Sender_test.cxx Sender 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 "ipm/Sender.hpp"
10 :
11 : #define BOOST_TEST_MODULE Sender_test // NOLINT
12 :
13 : #include "boost/test/unit_test.hpp"
14 :
15 : #include <string>
16 : #include <vector>
17 :
18 : using namespace dunedaq::ipm;
19 :
20 : BOOST_AUTO_TEST_SUITE(Sender_test)
21 :
22 : namespace {
23 :
24 : class SenderImpl : public Sender
25 : {
26 :
27 : public:
28 2 : SenderImpl()
29 2 : : m_can_send(false)
30 : {
31 2 : }
32 :
33 2 : std::string connect_for_sends(const nlohmann::json& /* connection_info */) override
34 : {
35 2 : m_can_send = true;
36 2 : return "";
37 : }
38 6 : bool can_send() const noexcept override { return m_can_send; }
39 1 : void sabotage_my_sending_ability() { m_can_send = false; }
40 :
41 : protected:
42 1 : bool send_(const void* /* message */,
43 : int /* N */,
44 : const duration_t& /* timeout */,
45 : const std::string& /* metadata */,
46 : bool /*no_tmoexcept_mode*/) override
47 : {
48 : // Pretty unexciting stub
49 1 : return true;
50 : }
51 :
52 : private:
53 : bool m_can_send;
54 : };
55 :
56 : } // namespace ""
57 :
58 2 : BOOST_AUTO_TEST_CASE(CopyAndMoveSemantics)
59 : {
60 1 : BOOST_REQUIRE(!std::is_copy_constructible_v<SenderImpl>);
61 1 : BOOST_REQUIRE(!std::is_copy_assignable_v<SenderImpl>);
62 1 : BOOST_REQUIRE(!std::is_move_constructible_v<SenderImpl>);
63 1 : BOOST_REQUIRE(!std::is_move_assignable_v<SenderImpl>);
64 1 : }
65 :
66 2 : BOOST_AUTO_TEST_CASE(Timeouts)
67 : {
68 1 : BOOST_REQUIRE_EQUAL(Sender::s_no_block.count(), 0);
69 1 : BOOST_REQUIRE_GT(Sender::s_block.count(), 3600000); // "Blocking" must be at least an hour!
70 1 : }
71 :
72 2 : BOOST_AUTO_TEST_CASE(StatusChecks)
73 : {
74 1 : SenderImpl the_sender;
75 1 : std::vector<char> random_data{ 'T', 'E', 'S', 'T' };
76 :
77 1 : BOOST_REQUIRE(!the_sender.can_send());
78 :
79 1 : nlohmann::json j;
80 1 : the_sender.connect_for_sends(j);
81 1 : BOOST_REQUIRE(the_sender.can_send());
82 :
83 1 : BOOST_REQUIRE_NO_THROW(the_sender.send(random_data.data(), random_data.size(), Sender::s_no_block));
84 :
85 1 : the_sender.sabotage_my_sending_ability();
86 1 : BOOST_REQUIRE(!the_sender.can_send());
87 :
88 4 : BOOST_REQUIRE_EXCEPTION(the_sender.send(random_data.data(), random_data.size(), Sender::s_no_block),
89 : dunedaq::ipm::KnownStateForbidsSend,
90 : [&](dunedaq::ipm::KnownStateForbidsSend) { return true; });
91 1 : }
92 :
93 2 : BOOST_AUTO_TEST_CASE(BadInput)
94 : {
95 1 : SenderImpl the_sender;
96 1 : nlohmann::json j;
97 1 : the_sender.connect_for_sends(j);
98 :
99 1 : const char* bad_bytes = nullptr;
100 4 : BOOST_REQUIRE_EXCEPTION(the_sender.send(bad_bytes, 10, Sender::s_no_block),
101 : dunedaq::ipm::NullPointerPassedToSend,
102 : [&](dunedaq::ipm::NullPointerPassedToSend) { return true; });
103 :
104 1 : std::vector<char> random_data{ 'T', 'E', 'S', 'T' };
105 1 : BOOST_REQUIRE_NO_THROW(the_sender.send(random_data.data(), 0, Sender::s_no_block));
106 1 : }
107 :
108 : BOOST_AUTO_TEST_SUITE_END()
|