ifw-daq  3.0.1
IFW Data Acquisition modules
testStatus.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_libdaq_test
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Test daq::Status and daq::ObservableStatus
7  */
8 #include <daq/status.hpp>
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
12 #include "statusObserver.hpp"
13 
14 using namespace ::testing;
15 using namespace daq;
16 
17 /**
18  * ObservableStatus test fixture.
19  *
20  * @ingroup daq_ocm_libdaq_test
21  */
22 class TestObservableStatus : public ::testing::Test {
23 public:
24 };
25 
27  ObservableStatus s("id", "fileid");
28 }
29 
31  ObservableStatus s1("id", "fileid");
32  ObservableStatus s2("id", "fileid");
33  ObservableStatus s3("id", "fileid");
34  s3.SetError(true);
35  EXPECT_EQ(s1, s2);
36  EXPECT_NE(s1, s3);
37 }
38 
40  ObservableStatus s("id", "fileid");
41 
42  EXPECT_EQ(State::NotStarted, s.GetState()) << "Default state should be State::NotStarted";
43  EXPECT_FALSE(s.GetError()) << "By default error flag should be false";
44 
47 }
48 
49 TEST_F(TestObservableStatus, OstreamOperator) {
50  ObservableStatus s("id", "fileid");
51  std::stringstream ss;
52  ss << s;
53  EXPECT_THAT(
54  ss.str(),
55  MatchesRegex("ObservableStatus\\(id='id', file_id='fileid', state=.*, error=false\\)"));
56 }
57 
58 TEST_F(TestObservableStatus, Observability) {
59  // Setup
60  InSequence seq; // Mock expectations must occur in sequence
61 
63  ObservableStatus s("id", "fileid");
64 
65  std::vector<DpPart> files = {{"origin1", "path1"}, {"origin2", "path2"}};
66 
67  auto time = Status::Clock::now();
68 
69  s.ConnectObserver(std::reference_wrapper(o));
70  EXPECT_CALL(o, CallOperator(Property(&ObservableStatus::GetState, State::Starting)));
71  EXPECT_CALL(o, CallOperator(Property(&ObservableStatus::GetState, State::Acquiring)));
72  EXPECT_CALL(o, CallOperator(Property(&ObservableStatus::GetTimestamp, time)));
73 
74  // Test
77 
78  auto new_status = s.GetStatus();
79  new_status.timestamp = time;
80  // Assigning complete status which should then use the timestamp from that status.
81  s = new_status;
82 }
83 
84 TEST(TestStatus, OstreamOperator) {
85  Status s("id", "fileid");
86  // Note that epoch is not really portable so this test may break on non-linux platforms
87  s.timestamp = std::chrono::time_point<std::chrono::system_clock>(
88  std::chrono::nanoseconds(1693899318896895717));
89  std::stringstream ss;
90  ss << s;
91  EXPECT_THAT(ss.str(),
92  MatchesRegex("Status\\(id='id', file_id='fileid', state=.*, error=false, "
93  "result=''\\, timestamp=2023-09-05T07:35:18.896)"));
94 }
95 
96 TEST(TestStatus, Comparison) {
97  Status s1("id", "fileid");
98  Status s2("id", "fileid");
99  Status s3("id", "fileid");
100  s3.error = true;
101  EXPECT_EQ(s1, s2);
102  EXPECT_NE(s1, s3);
103 }
104 
105 TEST(TestStatus, Alerts) {
106  Status s1("id", "fileid");
107  Alert alert = {};
108  alert.id.category = "cat";
109  alert.id.key = "key";
110  alert.description = "description";
111 
112  SetAlert(s1.alerts, alert);
113  ASSERT_EQ(1, s1.alerts.size());
114  EXPECT_EQ(s1.alerts[0], alert);
115  EXPECT_EQ(s1.alerts[0].description, "description");
116 
117  auto alert2 = alert;
118  alert2.id.key = "different key";
119 
120  SetAlert(s1.alerts, alert2);
121 
122  ASSERT_EQ(2, s1.alerts.size());
123 
124  alert.description = "new description";
125  SetAlert(s1.alerts, alert);
126  EXPECT_EQ(s1.alerts[0].description, "new description");
127 
128  // Remove alert1 which leaves alert2
129  ClearAlert(s1.alerts, alert.id);
130  ASSERT_EQ(1, s1.alerts.size());
131  EXPECT_EQ(s1.alerts[0], alert2);
132 }
133 
134 TEST(TestAlertId, Comparison) {
135  AlertId alert1 = {};
136  alert1.category = "cat";
137  alert1.key = "key";
138  EXPECT_EQ(alert1, alert1);
139 
140  auto alert2 = alert1;
141  alert2.key = "different key";
142  EXPECT_NE(alert1, alert2);
143 
144  auto alert3 = alert1;
145  alert3.category = "different category";
146  EXPECT_NE(alert1, alert3);
147 }
148 
149 TEST(TestAlert, Comparison) {
150  Alert alert1 = {};
151  alert1.id.category = "cat";
152  alert1.id.key = "key";
153  alert1.description = "description";
154  EXPECT_EQ(alert1, alert1);
155 
156  auto alert2 = alert1;
157  alert2.id.key = "different key";
158  EXPECT_NE(alert1, alert2);
159 
160  auto alert3 = alert1;
161  alert3.id.category = "different category";
162  EXPECT_NE(alert1, alert3);
163 }
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:210
boost::signals2::connection ConnectObserver(Observer o)
Connect observer that is invoked when state is modified.
Definition: status.hpp:383
State GetState() const noexcept
Definition: status.cpp:270
void SetError(bool error) noexcept
Set error flag for data acquisition.
Definition: status.cpp:309
Status const & GetStatus() const noexcept
Connect observer that is invoked when state is modified.
Definition: status.cpp:357
void SetState(State s, std::optional< bool > error=std::nullopt) noexcept
Set state of data acquisition.
Definition: status.cpp:278
bool GetError() const noexcept
Definition: status.cpp:274
Status::Clock::time_point GetTimestamp() const noexcept
Definition: status.cpp:262
ObservableStatus test fixture.
Definition: testStatus.cpp:22
Simple observer used for testing.
AlertId id
Definition: status.hpp:90
TEST_F(TestDpmClient, StartMonitoringSendsRequestAndReceivesReply)
std::string key
Unique key for each alert.
Definition: status.hpp:67
std::string description
Definition: status.hpp:91
std::string category
Standardized category.
Definition: status.hpp:63
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
Definition: status.cpp:19
bool ClearAlert(std::vector< Alert > &alerts, AlertId const &alert)
Clear alert.
Definition: status.cpp:30
State
Observable states of the data acquisition process.
Definition: state.hpp:39
@ Acquiring
All data sources have reported data acquisition is in progress.
@ Starting
Transitional state between NotStarted and Acquiring when sources have not begun acquiring data yet.
@ NotStarted
Initial state of data acquisition.
TEST(TestDaqContext, Files)
Describes an active Data Acquisition alert.
Definition: status.hpp:86
Uniquely identfies an alert.
Definition: status.hpp:59
Contains declaration for Status and ObservableStatus.
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:153
bool error
Definition: status.hpp:177
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:181
TimePoint timestamp
Timestamp of last update.
Definition: status.hpp:198
EXPECT_EQ(meta.rr_uri, "zpb.rr://meta")
ASSERT_EQ(meta.keyword_rules.size(), 1u)