ifw-daq  2.1.0-pre1
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 
45  s.SetState(State::Starting);
46  EXPECT_EQ(State::Starting, s.GetState());
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  s.ConnectObserver(std::reference_wrapper(o));
68  EXPECT_CALL(o, CallOperator(Property(&ObservableStatus::GetState, State::Starting)));
69  EXPECT_CALL(o, CallOperator(Property(&ObservableStatus::GetState, State::Acquiring)));
70 
71  // Test
72  s.SetState(State::Starting);
73  s.SetState(State::Acquiring);
74 }
75 
76 TEST(TestStatus, OstreamOperator) {
77  Status s("id", "fileid");
78  std::stringstream ss;
79  ss << s;
80  EXPECT_THAT(
81  ss.str(),
82  MatchesRegex("Status\\(id='id', file_id='fileid', state=.*, error=false, result=''\\)"));
83 }
84 
85 TEST(TestStatus, Comparison) {
86  Status s1("id", "fileid");
87  Status s2("id", "fileid");
88  Status s3("id", "fileid");
89  s3.error = true;
90  EXPECT_EQ(s1, s2);
91  EXPECT_NE(s1, s3);
92 }
93 
94 TEST(TestStatus, Alerts) {
95  Status s1("id", "fileid");
96  Alert alert = {};
97  alert.id.category = "cat";
98  alert.id.key = "key";
99  alert.description = "description";
100 
101  SetAlert(s1.alerts, alert);
102  ASSERT_EQ(1, s1.alerts.size());
103  EXPECT_EQ(s1.alerts[0], alert);
104  EXPECT_EQ(s1.alerts[0].description, "description");
105 
106  auto alert2 = alert;
107  alert2.id.key = "different key";
108 
109  SetAlert(s1.alerts, alert2);
110 
111  ASSERT_EQ(2, s1.alerts.size());
112 
113  alert.description = "new description";
114  SetAlert(s1.alerts, alert);
115  EXPECT_EQ(s1.alerts[0].description, "new description");
116 
117  // Remove alert1 which leaves alert2
118  ClearAlert(s1.alerts, alert.id);
119  ASSERT_EQ(1, s1.alerts.size());
120  EXPECT_EQ(s1.alerts[0], alert2);
121 }
122 
123 TEST(TestAlertId, Comparison) {
124  AlertId alert1 = {};
125  alert1.category = "cat";
126  alert1.key = "key";
127  EXPECT_EQ(alert1, alert1);
128 
129  auto alert2 = alert1;
130  alert2.key = "different key";
131  EXPECT_NE(alert1, alert2);
132 
133  auto alert3 = alert1;
134  alert3.category = "different category";
135  EXPECT_NE(alert1, alert3);
136 }
137 
138 TEST(TestAlert, Comparison) {
139  Alert alert1 = {};
140  alert1.id.category = "cat";
141  alert1.id.key = "key";
142  alert1.description = "description";
143  EXPECT_EQ(alert1, alert1);
144 
145  auto alert2 = alert1;
146  alert2.id.key = "different key";
147  EXPECT_NE(alert1, alert2);
148 
149  auto alert3 = alert1;
150  alert3.id.category = "different category";
151  EXPECT_NE(alert1, alert3);
152 }
daq::TEST
TEST(TestDaqContext, Files)
Definition: testDaqContext.cpp:16
TestObservableStatus
ObservableStatus test fixture.
Definition: testStatus.cpp:22
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:39
daq::Status::error
bool error
Definition: status.hpp:139
daq::Status::alerts
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:143
daq::SetAlert
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
Definition: status.cpp:9
daq::ObservableStatus::GetState
State GetState() const noexcept
Definition: status.cpp:184
daq::AlertId::key
std::string key
Unique key for each alert.
Definition: status.hpp:60
daq::ObservableStatus::GetError
bool GetError() const noexcept
Definition: status.cpp:188
daq::AlertId::category
std::string category
Standardized category.
Definition: status.hpp:56
daq::Alert::description
std::string description
Definition: status.hpp:76
daq
Definition: asyncProcess.cpp:15
daq::ObservableStatus
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:161
daq::ObservableStatus::SetError
void SetError(bool error) noexcept
Set error flag for data acquisition.
Definition: status.cpp:205
daq::Alert
Describes an active Data Acquisition alert.
Definition: status.hpp:71
daq::TEST_F
TEST_F(TestDpmDaqController, StatusUpdateInNotScheduledSucceeds)
Definition: testDpmDaqController.cpp:60
daq::Alert::id
AlertId id
Definition: status.hpp:75
daq::ClearAlert
bool ClearAlert(std::vector< Alert > &alerts, AlertId const &alert)
Clear alert.
Definition: status.cpp:20
statusObserver.hpp
status.hpp
Contains declaration for Status and ObservableStatus.
StatusObserverMock
Simple observer used for testing.
Definition: statusObserver.hpp:22
daq::AlertId
Uniquely identfies an alert.
Definition: status.hpp:52
daq::Status
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:120
daq::ObservableStatus::SetState
void SetState(State s, std::optional< bool > error=std::nullopt) noexcept
Set state of data acquisition.
Definition: status.cpp:192
daq::ObservableStatus::ConnectObserver
boost::signals2::connection ConnectObserver(Observer o)
Connect observer that is invoked when state is modified.
Definition: status.hpp:307