00001 #define TRACE_NAME "SharedMemoryFragmentManager_t"
00002
00003 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
00004 #include "artdaq-core/Utilities/configureMessageFacility.hh"
00005 #include "tracemf.h"
00006
00007 #define BOOST_TEST_MODULE(SharedMemoryFragmentManager_t)
00008 #include "cetlib/quiet_unit_test.hpp"
00009 #include "cetlib_except/exception.h"
00010 #include "SharedMemoryTestShims.hh"
00011
00012
00013 BOOST_AUTO_TEST_SUITE(SharedMemoryFragmentManager_test)
00014
00015 BOOST_AUTO_TEST_CASE(Construct)
00016 {
00017 artdaq::configureMessageFacility("SharedMemoryFragmentManager_t", true, true);
00018 TLOG(TLVL_INFO) << "BEGIN TEST Construct" ;
00019 artdaq::SharedMemoryFragmentManager man(GetRandomKey(0xF4A6), 10, 0x1000);
00020 BOOST_REQUIRE_EQUAL(man.IsValid(), true);
00021 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
00022 BOOST_REQUIRE_EQUAL(man.size(), 10);
00023 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 1);
00024 TLOG(TLVL_INFO) << "END TEST Construct";
00025 }
00026
00027 BOOST_AUTO_TEST_CASE(Attach)
00028 {
00029 TLOG(TLVL_INFO) << "BEGIN TEST Attach";
00030 uint32_t key = GetRandomKey(0xF4A6);
00031 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
00032 artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
00033
00034 BOOST_REQUIRE_EQUAL(man.IsValid(), true);
00035 BOOST_REQUIRE_EQUAL(man.GetMyId(), 0);
00036 BOOST_REQUIRE_EQUAL(man.size(), 10);
00037 BOOST_REQUIRE_EQUAL(man.GetAttachedCount(), 2);
00038
00039 BOOST_REQUIRE_EQUAL(man2.IsValid(), true);
00040 BOOST_REQUIRE_EQUAL(man2.GetMyId(), 1);
00041 BOOST_REQUIRE_EQUAL(man2.size(), 10);
00042 BOOST_REQUIRE_EQUAL(man2.GetAttachedCount(), 2);
00043 TLOG(TLVL_INFO) << "END TEST Attach";
00044
00045 }
00046
00047 BOOST_AUTO_TEST_CASE(DataFlow)
00048 {
00049 TLOG(TLVL_INFO) << "BEGIN TEST DataFlow";
00050 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for DataFlow test" ;
00051 uint32_t key = GetRandomKey(0xF4A6);
00052 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
00053 artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
00054
00055 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
00056
00057 TLOG(TLVL_DEBUG) << "Creating test Fragment" ;
00058 artdaq::Fragment frag(fragSizeWords);
00059 frag.setSequenceID(0x10);
00060 frag.setFragmentID(0x20);
00061 auto type = artdaq::Fragment::DataFragmentType;
00062 frag.setSystemType(type);
00063 frag.setTimestamp(0x30);
00064 for (size_t ii = 0; ii < fragSizeWords; ++ii)
00065 {
00066 *(frag.dataBegin() + ii) = ii;
00067 }
00068
00069 TLOG(TLVL_DEBUG) << "Writing Test Fragment to Shared Memory" ;
00070 man.WriteFragment(std::move(frag), false, 0);
00071
00072 TLOG(TLVL_DEBUG) << "Reading Test Fragment Header" ;
00073 artdaq::detail::RawFragmentHeader header;
00074 auto sts = man2.ReadFragmentHeader(header);
00075
00076 TLOG(TLVL_DEBUG) << "Checking Test Fragment Header Contents" ;
00077 BOOST_REQUIRE_EQUAL(sts, 0);
00078 BOOST_REQUIRE_EQUAL(header.word_count, frag.size());
00079 BOOST_REQUIRE_EQUAL(header.sequence_id, 0x10);
00080 BOOST_REQUIRE_EQUAL(header.fragment_id, 0x20);
00081 BOOST_REQUIRE_EQUAL(header.type, type);
00082 BOOST_REQUIRE_EQUAL(header.timestamp, 0x30);
00083
00084 TLOG(TLVL_DEBUG) << "Reading Test Fragment data" ;
00085 artdaq::Fragment frag2(header.word_count);
00086 sts = man2.ReadFragmentData(frag2.dataBegin(), header.word_count - header.num_words());
00087
00088 TLOG(TLVL_DEBUG) << "Checking Test Fragment contents" ;
00089 BOOST_REQUIRE_EQUAL(sts, 0);
00090 for(size_t ii = 0; ii < fragSizeWords; ++ii)
00091 {
00092 BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(frag2.dataBegin() + ii));
00093 }
00094 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager DataFlow test complete" ;
00095 TLOG(TLVL_INFO) << "END TEST DataFlow";
00096 }
00097
00098 BOOST_AUTO_TEST_CASE(WholeFragment)
00099 {
00100 TLOG(TLVL_INFO) << "BEGIN TEST WholeFragment";
00101 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for WholeFragment Test" ;
00102 uint32_t key = GetRandomKey(0xF4A6);
00103 artdaq::SharedMemoryFragmentManager man(key, 10, 0x1000);
00104 artdaq::SharedMemoryFragmentManager man2(key, 10, 0x1000);
00105
00106 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
00107
00108 TLOG(TLVL_DEBUG) << "Creating test Fragment" ;
00109 artdaq::Fragment frag(fragSizeWords);
00110 frag.setSequenceID(0x10);
00111 frag.setFragmentID(0x20);
00112 auto type = artdaq::Fragment::DataFragmentType;
00113 frag.setSystemType(type);
00114 frag.setTimestamp(0x30);
00115 for (size_t ii = 0; ii < fragSizeWords; ++ii)
00116 {
00117 *(frag.dataBegin() + ii) = ii;
00118 }
00119
00120 TLOG(TLVL_DEBUG) << "Writing Test Fragment to Shared Memory" ;
00121 man.WriteFragment(std::move(frag), false, 0);
00122
00123 TLOG(TLVL_DEBUG) << "Reading Test Fragment Header" ;
00124 artdaq::Fragment recvdFrag;
00125 auto sts = man2.ReadFragment(recvdFrag);
00126
00127 TLOG(TLVL_DEBUG) << "Checking Test Fragment Header Contents" ;
00128 BOOST_REQUIRE_EQUAL(sts, 0);
00129 BOOST_REQUIRE_EQUAL(recvdFrag.size(), frag.size());
00130 BOOST_REQUIRE_EQUAL(recvdFrag.sequenceID(), 0x10);
00131 BOOST_REQUIRE_EQUAL(recvdFrag.fragmentID(), 0x20);
00132 BOOST_REQUIRE_EQUAL(recvdFrag.type(), type);
00133 BOOST_REQUIRE_EQUAL(recvdFrag.timestamp(), 0x30);
00134
00135 TLOG(TLVL_DEBUG) << "Checking Test Fragment Data Contents" ;
00136 for (size_t ii = 0; ii < fragSizeWords; ++ii)
00137 {
00138
00139 BOOST_REQUIRE_EQUAL(*(frag.dataBegin() + ii), *(recvdFrag.dataBegin() + ii));
00140 }
00141 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager WholeFragment test complete" ;
00142 TLOG(TLVL_INFO) << "END TEST WholeFragment";
00143 }
00144
00145
00146 BOOST_AUTO_TEST_CASE(Timeout)
00147 {
00148 TLOG(TLVL_INFO) << "BEGIN TEST Timeout";
00149 TLOG(TLVL_DEBUG) << "Initializing SharedMemoryFragmentManagers for Timeout Test" ;
00150 uint32_t key = GetRandomKey(0xF4A6);
00151 artdaq::SharedMemoryFragmentManager man(key, 1, 0x1000);
00152
00153 auto fragSizeWords = 0x1000 / sizeof(artdaq::RawDataType) - artdaq::detail::RawFragmentHeader::num_words() - 1;
00154
00155 TLOG(TLVL_DEBUG) << "Creating test Fragment" ;
00156 artdaq::Fragment frag(fragSizeWords);
00157 frag.setSequenceID(0x10);
00158 frag.setFragmentID(0x20);
00159 auto type = artdaq::Fragment::DataFragmentType;
00160 frag.setSystemType(type);
00161 frag.setTimestamp(0x30);
00162 for (size_t ii = 0; ii < fragSizeWords; ++ii)
00163 {
00164 *(frag.dataBegin() + ii) = ii;
00165 }
00166
00167 TLOG(TLVL_DEBUG) << "Reserving buffer to cause timeout to happen" ;
00168 auto ret = man.GetBufferForWriting(true);
00169 BOOST_REQUIRE_EQUAL(ret, 0);
00170
00171 TLOG(TLVL_DEBUG) << "Attempting to write Fragment to Shared Memory. This should time out." ;
00172 auto start_time = std::chrono::steady_clock::now();
00173 ret = man.WriteFragment(std::move(frag), true, 100000);
00174 auto duration = artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time);
00175
00176 BOOST_REQUIRE_EQUAL(ret, -3);
00177 BOOST_REQUIRE_GE(duration, 100000);
00178
00179 TLOG(TLVL_DEBUG) << "SharedMemoryFragmentManager Timeout test complete" ;
00180 TLOG(TLVL_INFO) << "END TEST Timeout";
00181 }
00182
00183 BOOST_AUTO_TEST_SUITE_END()