1 #define TRACE_NAME "SharedMemoryFragmentManager_t"
5 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
6 #include "artdaq-core/Utilities/configureMessageFacility.hh"
9 #define BOOST_TEST_MODULE(SharedMemoryFragmentManager_t)
10 #include "SharedMemoryTestShims.hh"
11 #include "cetlib/quiet_unit_test.hpp"
12 #include "cetlib_except/exception.h"
14 BOOST_AUTO_TEST_SUITE(SharedMemoryFragmentManager_test)
16 BOOST_AUTO_TEST_CASE(Construct)
19 TLOG(TLVL_INFO) <<
"BEGIN TEST Construct";
21 BOOST_REQUIRE_EQUAL(man.IsValid(),
true);
22 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
23 BOOST_REQUIRE_EQUAL(man.size(), 10);
24 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1);
25 TLOG(TLVL_INFO) <<
"END TEST Construct";
28 BOOST_AUTO_TEST_CASE(Attach)
30 TLOG(TLVL_INFO) <<
"BEGIN TEST Attach";
31 uint32_t key = GetRandomKey(0xF4A6);
35 BOOST_REQUIRE_EQUAL(man.IsValid(),
true);
36 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
37 BOOST_REQUIRE_EQUAL(man.size(), 10);
38 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 2);
40 BOOST_REQUIRE_EQUAL(man2.IsValid(),
true);
41 BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
42 BOOST_REQUIRE_EQUAL(man2.size(), 10);
43 BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 2);
44 TLOG(TLVL_INFO) <<
"END TEST Attach";
47 BOOST_AUTO_TEST_CASE(Reattach)
49 TLOG(TLVL_INFO) <<
"BEGIN TEST Reattach";
50 uint32_t key = GetRandomKey(0xF4A6);
54 BOOST_REQUIRE_EQUAL(man->IsValid(),
true);
55 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0);
56 BOOST_REQUIRE_EQUAL(man->size(), 10);
57 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2);
59 BOOST_REQUIRE_EQUAL(man2->IsValid(),
true);
60 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 1);
61 BOOST_REQUIRE_EQUAL(man2->size(), 10);
62 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2);
65 BOOST_REQUIRE_EQUAL(man->IsValid(),
true);
66 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 1);
68 man2 = std::make_unique<artdaq::SharedMemoryFragmentManager>(key);
69 BOOST_REQUIRE_EQUAL(man->IsValid(),
true);
70 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0);
71 BOOST_REQUIRE_EQUAL(man->size(), 10);
72 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2);
74 BOOST_REQUIRE_EQUAL(man2->IsValid(),
true);
75 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 2);
76 BOOST_REQUIRE_EQUAL(man2->size(), 10);
77 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2);
80 BOOST_REQUIRE_EQUAL(man2->IsValid(),
true);
81 BOOST_REQUIRE_EQUAL(man2->IsEndOfData(),
true);
82 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 2);
83 BOOST_REQUIRE_EQUAL(man2->size(), 10);
84 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 1);
87 BOOST_REQUIRE_EQUAL(man2->IsValid(),
false);
89 man = std::make_unique<artdaq::SharedMemoryFragmentManager>(key, 10, 0x1000);
90 BOOST_REQUIRE_EQUAL(man->IsValid(),
true);
91 BOOST_REQUIRE_EQUAL(man->GetMyId(), 0);
92 BOOST_REQUIRE_EQUAL(man->size(), 10);
93 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 1);
96 BOOST_REQUIRE_EQUAL(man2->IsValid(),
true);
97 BOOST_REQUIRE_EQUAL(man2->GetMyId(), 1);
98 BOOST_REQUIRE_EQUAL(man2->size(), 10);
99 BOOST_REQUIRE_EQUAL(man->GetAttachedCount(), 2);
100 BOOST_REQUIRE_EQUAL(man2->GetAttachedCount(), 2);
102 TLOG(TLVL_INFO) <<
"END TEST Reattach";
105 BOOST_AUTO_TEST_CASE(DataFlow)
107 TLOG(TLVL_INFO) <<
"BEGIN TEST DataFlow";
108 TLOG(TLVL_DEBUG) <<
"Initializing SharedMemoryFragmentManagers for DataFlow test";
109 uint32_t key = GetRandomKey(0xF4A6);
115 TLOG(TLVL_DEBUG) <<
"Creating test Fragment";
117 frag.setSequenceID(0x10);
118 frag.setFragmentID(0x20);
120 frag.setSystemType(type);
121 frag.setTimestamp(0x30);
122 for (
size_t ii = 0; ii < fragSizeWords; ++ii)
124 *(frag.dataBegin() + ii) = ii;
127 TLOG(TLVL_DEBUG) <<
"Writing Test Fragment to Shared Memory";
128 auto fragSize = frag.size();
129 man.WriteFragment(std::move(frag),
false, 0);
131 TLOG(TLVL_DEBUG) <<
"Reading Test Fragment Header";
133 auto sts = man2.ReadFragmentHeader(header);
135 TLOG(TLVL_DEBUG) <<
"Checking Test Fragment Header Contents";
136 BOOST_REQUIRE_EQUAL(sts, 0);
137 BOOST_REQUIRE_EQUAL(header.
word_count, fragSize);
140 BOOST_REQUIRE_EQUAL(header.
type, type);
141 BOOST_REQUIRE_EQUAL(header.
timestamp, 0x30);
143 TLOG(TLVL_DEBUG) <<
"Reading Test Fragment data";
147 TLOG(TLVL_DEBUG) <<
"Checking Test Fragment contents";
148 BOOST_REQUIRE_EQUAL(sts, 0);
149 for (
size_t ii = 0; ii < fragSizeWords; ++ii)
151 BOOST_REQUIRE_EQUAL(ii, *(frag2.dataBegin() + ii));
153 TLOG(TLVL_DEBUG) <<
"SharedMemoryFragmentManager DataFlow test complete";
154 TLOG(TLVL_INFO) <<
"END TEST DataFlow";
157 BOOST_AUTO_TEST_CASE(WholeFragment)
159 TLOG(TLVL_INFO) <<
"BEGIN TEST WholeFragment";
160 TLOG(TLVL_DEBUG) <<
"Initializing SharedMemoryFragmentManagers for WholeFragment Test";
161 uint32_t key = GetRandomKey(0xF4A6);
167 TLOG(TLVL_DEBUG) <<
"Creating test Fragment";
169 frag.setSequenceID(0x10);
170 frag.setFragmentID(0x20);
172 frag.setSystemType(type);
173 frag.setTimestamp(0x30);
174 for (
size_t ii = 0; ii < fragSizeWords; ++ii)
176 *(frag.dataBegin() + ii) = ii;
179 TLOG(TLVL_DEBUG) <<
"Writing Test Fragment to Shared Memory";
180 auto fragSize = frag.size();
181 man.WriteFragment(std::move(frag),
false, 0);
183 TLOG(TLVL_DEBUG) <<
"Reading Test Fragment Header";
185 auto sts = man2.ReadFragment(recvdFrag);
187 TLOG(TLVL_DEBUG) <<
"Checking Test Fragment Header Contents";
188 BOOST_REQUIRE_EQUAL(sts, 0);
189 BOOST_REQUIRE_EQUAL(recvdFrag.
size(), fragSize);
190 BOOST_REQUIRE_EQUAL(recvdFrag.
sequenceID(), 0x10);
191 BOOST_REQUIRE_EQUAL(recvdFrag.
fragmentID(), 0x20);
192 BOOST_REQUIRE_EQUAL(recvdFrag.
type(), type);
193 BOOST_REQUIRE_EQUAL(recvdFrag.
timestamp(), 0x30);
195 TLOG(TLVL_DEBUG) <<
"Checking Test Fragment Data Contents";
196 for (
size_t ii = 0; ii < fragSizeWords; ++ii)
199 BOOST_REQUIRE_EQUAL(ii, *(recvdFrag.
dataBegin() + ii));
201 TLOG(TLVL_DEBUG) <<
"SharedMemoryFragmentManager WholeFragment test complete";
202 TLOG(TLVL_INFO) <<
"END TEST WholeFragment";
205 BOOST_AUTO_TEST_CASE(Timeout)
207 TLOG(TLVL_INFO) <<
"BEGIN TEST Timeout";
208 TLOG(TLVL_DEBUG) <<
"Initializing SharedMemoryFragmentManagers for Timeout Test";
209 uint32_t key = GetRandomKey(0xF4A6);
214 TLOG(TLVL_DEBUG) <<
"Creating test Fragment";
216 frag.setSequenceID(0x10);
217 frag.setFragmentID(0x20);
219 frag.setSystemType(type);
220 frag.setTimestamp(0x30);
221 for (
size_t ii = 0; ii < fragSizeWords; ++ii)
223 *(frag.dataBegin() + ii) = ii;
226 TLOG(TLVL_DEBUG) <<
"Reserving buffer to cause timeout to happen";
227 auto ret = man.GetBufferForWriting(
true);
228 BOOST_REQUIRE_EQUAL(ret, 0);
230 TLOG(TLVL_DEBUG) <<
"Attempting to write Fragment to Shared Memory. This should time out.";
231 auto start_time = std::chrono::steady_clock::now();
232 ret = man.WriteFragment(std::move(frag),
true, 100000);
235 BOOST_REQUIRE_EQUAL(ret, -3);
236 BOOST_REQUIRE_GE(duration, 100000);
238 TLOG(TLVL_DEBUG) <<
"SharedMemoryFragmentManager Timeout test complete";
239 TLOG(TLVL_INFO) <<
"END TEST Timeout";
242 BOOST_AUTO_TEST_SUITE_END()
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
The SharedMemoryFragmentManager is a SharedMemoryManager that deals with Fragment transfers using a S...
sequence_id_t sequenceID() const
Sequence ID of the Fragment, from the Fragment header.
timestamp_t timestamp() const
Timestamp of the Fragment, from the Fragment header.
constexpr size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of microseconds in the given time interval
static constexpr type_t DataFragmentType
Copy DataFragmentType from RawFragmentHeader.
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
type_t type() const
Type of the Fragment, from the Fragment header.
void configureMessageFacility(char const *progname, bool useConsole=true, bool printDebug=false)
Configure and start the message facility. Provide the program name so that messages will be appropria...
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
fragment_id_t fragmentID() const
Fragment ID of the Fragment, from the Fragment header.