1 #include "artdaq-core/Core/SharedMemoryManager.hh"
2 #include "artdaq-core/Utilities/TimeUtils.hh"
3 #include "artdaq-core/Utilities/configureMessageFacility.hh"
5 #define BOOST_TEST_MODULE SharedMemoryManager_t
6 #include "cetlib/quiet_unit_test.hpp"
7 #include "cetlib_except/exception.h"
9 #define TRACE_NAME "SharedMemoryManager_t"
11 #include "SharedMemoryTestShims.hh"
13 BOOST_AUTO_TEST_SUITE(SharedMemoryManager_test)
15 BOOST_AUTO_TEST_CASE(Construct)
18 TLOG(TLVL_DEBUG) <<
"BEGIN TEST Construct" ;
19 uint32_t key = GetRandomKey(0x7357);
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 BOOST_REQUIRE_EQUAL(man.GetKey(), key);
26 TLOG(TLVL_DEBUG) <<
"END TEST Construct" ;
29 BOOST_AUTO_TEST_CASE(Attach)
31 TLOG(TLVL_DEBUG) <<
"BEGIN TEST Attach" ;
32 uint32_t key = GetRandomKey(0x7357);
36 BOOST_REQUIRE_EQUAL(man.IsValid(),
true);
37 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
38 BOOST_REQUIRE_EQUAL(man.size(), 10);
39 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 2);
40 BOOST_REQUIRE_EQUAL(man.GetKey(), key);
42 BOOST_REQUIRE_EQUAL(man2.IsValid(),
true);
43 BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
44 BOOST_REQUIRE_EQUAL(man2.size(), 10);
45 BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 2);
46 BOOST_REQUIRE_EQUAL(man2.GetKey(), key);
48 TLOG(TLVL_DEBUG) <<
"END TEST Attach" ;
51 BOOST_AUTO_TEST_CASE(DataFlow)
53 TLOG(TLVL_DEBUG) <<
"BEGIN TEST DataFlow" ;
54 uint32_t key = GetRandomKey(0x7357);
58 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(
false),
true);
59 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 10);
60 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
61 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
63 int buf = man.GetBufferForWriting(
false);
65 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
66 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
67 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(),0);
68 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
72 std::generate_n(data, 0x1000, [&]() {
return ++n; });
73 man.Write(buf, data, 0x1000);
74 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
75 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(buf), 0x1000);
76 man.MarkBufferFull(buf, 1);
78 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
79 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
81 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
82 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
true);
83 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
85 auto readbuf = man2.GetBufferForReading();
87 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
89 auto sts = man2.Read(readbuf, &byte, 1);
90 BOOST_REQUIRE_EQUAL(sts,
true);
91 BOOST_REQUIRE_EQUAL(byte, 1);
92 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
93 sts = man2.Read(readbuf, &byte, 1);
94 BOOST_REQUIRE_EQUAL(sts,
true);
95 BOOST_REQUIRE_EQUAL(byte, 2);
96 man2.IncrementReadPos(readbuf, 0x10);
97 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
98 sts = man2.Read(readbuf, &byte, 1);
99 BOOST_REQUIRE_EQUAL(sts,
true);
100 BOOST_REQUIRE_EQUAL(byte, 0x13);
101 man2.ResetReadPos(readbuf);
102 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
103 sts = man2.Read(readbuf, &byte, 1);
104 BOOST_REQUIRE_EQUAL(sts,
true);
105 BOOST_REQUIRE_EQUAL(byte, 1);
106 man2.IncrementReadPos(readbuf, 0xFFF);
107 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
false);
108 man2.MarkBufferEmpty(readbuf);
110 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
111 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
112 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 10);
113 TLOG(TLVL_DEBUG) <<
"END TEST DataFlow" ;
116 BOOST_AUTO_TEST_CASE(Exceptions)
119 TLOG(TLVL_DEBUG) <<
"BEGIN TEST Exceptions" ;
120 uint32_t key = GetRandomKey(0x7357);
123 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(
false),
true);
124 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 10);
125 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
126 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
129 BOOST_REQUIRE_EXCEPTION(man.ResetReadPos(11), cet::exception, [&](cet::exception e) {
return e.category() ==
"ArgumentOutOfRange"; });
130 BOOST_REQUIRE_EQUAL(man.IsValid(),
false);
134 BOOST_REQUIRE_EXCEPTION(man.MarkBufferEmpty(0),cet::exception, [&](cet::exception e) {
return e.category() ==
"StateAccessViolation"; });
135 BOOST_REQUIRE_EQUAL(man.IsValid(),
false);
140 int buf = man.GetBufferForWriting(
false);
142 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
143 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
144 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
147 uint8_t data[0x2000];
148 std::generate_n(data, 0x2000, [&]() {
return ++n; });
149 BOOST_REQUIRE_EXCEPTION(man.Write(buf, data, 0x2000), cet::exception, [&](cet::exception e) {
return e.category() ==
"SharedMemoryWrite"; });
150 BOOST_REQUIRE_EQUAL(man.IsValid(),
false);
154 buf = man.GetBufferForWriting(
false);
156 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
157 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
158 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
159 man.Write(buf, data, 0x1000);
160 man.MarkBufferFull(buf);
163 BOOST_REQUIRE_EQUAL(man2.IsValid(),
true);
164 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
true);
165 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
167 int readbuf = man2.GetBufferForReading();
168 BOOST_REQUIRE_EQUAL(readbuf, buf);
169 BOOST_REQUIRE_EXCEPTION(man2.Read(readbuf, data, 0x1001), cet::exception, [&](cet::exception e) {
return e.category() ==
"SharedMemoryRead"; });
170 BOOST_REQUIRE_EQUAL(man2.IsValid(),
false);
175 man.MarkBufferEmpty(readbuf,
true);
176 buf = man.GetBufferForWriting(
false);
178 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
179 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
180 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
181 man.Write(buf, data, 0x1000);
182 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
183 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
184 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
185 man.MarkBufferFull(buf);
186 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
187 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
189 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
true);
190 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
191 readbuf = man2.GetBufferForReading();
192 BOOST_REQUIRE_EQUAL(readbuf, buf);
193 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
194 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager()[0], readbuf);
195 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(readbuf), 0x1000);
197 BOOST_REQUIRE_EXCEPTION(man.MarkBufferEmpty(readbuf), cet::exception, [&](cet::exception e) {
return e.category() ==
"OwnerAccessViolation"; });
198 BOOST_REQUIRE_EQUAL(man.IsValid(),
false);
200 TLOG(TLVL_DEBUG) <<
"END TEST Exceptions" ;
203 BOOST_AUTO_TEST_CASE(Broadcast)
205 TLOG(TLVL_DEBUG) <<
"BEGIN TEST Broadcast" ;
206 uint32_t key = GetRandomKey(0x7357);
211 BOOST_REQUIRE_EQUAL(man.ReadyForWrite(
false),
true);
212 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 10);
213 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
214 BOOST_REQUIRE_EQUAL(man.ReadReadyCount(), 0);
216 int buf = man.GetBufferForWriting(
false);
218 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 1);
219 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager()[0], buf);
220 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
221 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0);
224 uint8_t data[0x1000];
225 std::generate_n(data, 0x1000, [&]() {
return ++n; });
226 man.Write(buf, data, 0x1000);
227 BOOST_REQUIRE_EQUAL(man.BufferDataSize(buf), 0x1000);
228 BOOST_REQUIRE_EQUAL(man2.BufferDataSize(buf), 0x1000);
229 man.MarkBufferFull(buf, -1);
231 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
233 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
234 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
true);
235 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 1);
237 auto readbuf = man2.GetBufferForReading();
238 BOOST_REQUIRE_EQUAL(readbuf, buf);
239 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 1);
240 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
241 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(),
false);
243 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
245 auto sts = man2.Read(readbuf, &byte, 1);
246 BOOST_REQUIRE_EQUAL(sts,
true);
247 BOOST_REQUIRE_EQUAL(byte, 1);
248 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
249 sts = man2.Read(readbuf, &byte, 1);
250 BOOST_REQUIRE_EQUAL(sts,
true);
251 BOOST_REQUIRE_EQUAL(byte, 2);
252 man2.IncrementReadPos(readbuf, 0x10);
253 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
254 sts = man2.Read(readbuf, &byte, 1);
255 BOOST_REQUIRE_EQUAL(sts,
true);
256 BOOST_REQUIRE_EQUAL(byte, 0x13);
257 man2.ResetReadPos(readbuf);
258 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
true);
259 sts = man2.Read(readbuf, &byte, 1);
260 BOOST_REQUIRE_EQUAL(sts,
true);
261 BOOST_REQUIRE_EQUAL(byte, 1);
262 man2.IncrementReadPos(readbuf, 0xFFF);
263 BOOST_REQUIRE_EQUAL(man2.MoreDataInBuffer(readbuf),
false);
264 man2.MarkBufferEmpty(readbuf);
265 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(),
true);
266 BOOST_REQUIRE_EQUAL(man3.ReadReadyCount(), 1);
267 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
false);
268 BOOST_REQUIRE_EQUAL(man2.ReadReadyCount(), 0);
269 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
272 readbuf = man3.GetBufferForReading();
273 BOOST_REQUIRE_EQUAL(readbuf, buf);
274 BOOST_REQUIRE_EQUAL(man3.GetBuffersOwnedByManager().size(), 1);
275 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
277 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf),
true);
278 sts = man3.Read(readbuf, &byte, 1);
279 BOOST_REQUIRE_EQUAL(sts,
true);
280 BOOST_REQUIRE_EQUAL(byte, 1);
281 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf),
true);
282 sts = man3.Read(readbuf, &byte, 1);
283 BOOST_REQUIRE_EQUAL(sts,
true);
284 BOOST_REQUIRE_EQUAL(byte, 2);
285 man3.IncrementReadPos(readbuf, 0x10);
286 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf),
true);
287 sts = man3.Read(readbuf, &byte, 1);
288 BOOST_REQUIRE_EQUAL(sts,
true);
289 BOOST_REQUIRE_EQUAL(byte, 0x13);
290 man3.ResetReadPos(readbuf);
291 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf),
true);
292 sts = man3.Read(readbuf, &byte, 1);
293 BOOST_REQUIRE_EQUAL(sts,
true);
294 BOOST_REQUIRE_EQUAL(byte, 1);
295 man3.IncrementReadPos(readbuf, 0xFFF);
296 BOOST_REQUIRE_EQUAL(man3.MoreDataInBuffer(readbuf),
false);
297 man3.MarkBufferEmpty(readbuf);
298 BOOST_REQUIRE_EQUAL(man3.ReadyForRead(),
false);
299 BOOST_REQUIRE_EQUAL(man2.ReadyForRead(),
false);
300 BOOST_REQUIRE_EQUAL(man.ReadyForRead(),
false);
302 BOOST_REQUIRE_EQUAL(man2.GetBuffersOwnedByManager().size(), 0);
303 BOOST_REQUIRE_EQUAL(man.GetBuffersOwnedByManager().size(), 0);
304 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 9);
305 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
true), 10);
307 BOOST_REQUIRE_EQUAL(man.WriteReadyCount(
false), 10);
308 TLOG(TLVL_DEBUG) <<
"END TEST Broadcast" ;
311 BOOST_AUTO_TEST_SUITE_END()
The buffer is full, and waiting for a reader.
The buffer is currently being read from.
The buffer is currently being written to.
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buf...
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...