8 #include <gtest/gtest.h>
11 #include <mal/MalException.hpp>
17 using namespace ::testing;
18 using namespace std::chrono;
34 m_status->SetState(State::NotScheduled);
38 m_dpm_status = *m_status;
39 m_dpm_client = std::make_shared<DpmClientMock>();
43 m_controller = std::make_shared<DpmDaqController>(
44 m_io_ctx, m_props, m_status, m_event_log, m_dpm_client);
63 m_dpm_status.state = State::Scheduled;
67 EXPECT_EQ(m_controller->GetState(), State::NotScheduled);
68 m_dpm_client->status_signal(m_dpm_status);
69 EXPECT_EQ(m_controller->GetState(), State::Scheduled)
70 <<
"Expected new state from status update signal";
79 m_status->SetState(State::Aborted,
true);
81 m_dpm_status.state = State::Merging;
85 EXPECT_EQ(m_controller->GetState(), State::Aborted);
86 m_dpm_client->status_signal(m_dpm_status);
87 EXPECT_EQ(m_controller->GetState(), State::Merging)
88 <<
"Expected new state from status update signal";
89 EXPECT_FALSE(m_controller->GetErrorFlag());
95 auto fut = m_controller->StartAsync();
96 ASSERT_TRUE(fut.is_ready());
97 EXPECT_THROW(fut.get(), std::runtime_error);
103 auto fut = m_controller->StopAsync(ErrorPolicy::Tolerant);
104 ASSERT_TRUE(fut.is_ready());
105 EXPECT_THROW(fut.get(), std::runtime_error);
112 EXPECT_THROW(m_controller->UpdateKeywords({}), std::runtime_error);
120 boost::promise<R> reply;
121 EXPECT_CALL(*m_dpm_client, ScheduleAsync(_)).WillOnce(InvokeWithoutArgs([&] {
122 return reply.get_future();
126 auto fut = m_controller->ScheduleMergeAsync();
129 reply.set_value(State::Scheduled);
132 EXPECT_TRUE(fut.is_ready());
133 auto result = fut.get();
134 EXPECT_EQ(State::Scheduled, result);
135 EXPECT_EQ(State::Scheduled, m_controller->GetState());
139 ScheduleMergeAsyncSucceedsIfDpmSucceedsWithStatusSignalReceivedBeforeReply) {
144 boost::promise<R> reply;
145 EXPECT_CALL(*m_dpm_client, ScheduleAsync(_)).WillOnce(InvokeWithoutArgs([&] {
146 return reply.get_future();
150 auto fut = m_controller->ScheduleMergeAsync();
153 m_dpm_status.state = State::Scheduled;
154 m_dpm_client->status_signal(m_dpm_status);
155 EXPECT_EQ(State::Scheduled, m_controller->GetState());
158 reply.set_value(State::Scheduled);
161 EXPECT_TRUE(fut.is_ready());
162 auto result = fut.get();
163 EXPECT_EQ(State::Scheduled, result);
164 EXPECT_EQ(State::Scheduled, m_controller->GetState());
172 boost::promise<R> reply;
173 EXPECT_CALL(*m_dpm_client, ScheduleAsync(_)).WillOnce(InvokeWithoutArgs([&] {
174 return reply.get_future();
178 auto fut = m_controller->ScheduleMergeAsync();
181 reply.set_exception(std::runtime_error(
"some_error"));
184 EXPECT_TRUE(fut.is_ready());
185 EXPECT_THROW(fut.get(), std::exception);
186 EXPECT_EQ(State::NotScheduled, m_controller->GetState());
187 EXPECT_TRUE(m_controller->GetStatus()->GetError());
195 boost::promise<R> reply;
196 EXPECT_CALL(*m_dpm_client, ScheduleAsync(_)).WillOnce(InvokeWithoutArgs([&] {
197 return reply.get_future();
201 auto fut = m_controller->ScheduleMergeAsync();
202 reply.set_exception(elt::mal::TimeoutException(
"TIMEOUT"));
204 EXPECT_TRUE(fut.is_ready());
207 EXPECT_THROW(fut.get(), elt::mal::TimeoutException);
208 EXPECT_EQ(State::NotScheduled, m_controller->GetState()) <<
"State shoudln't have changed";
209 EXPECT_FALSE(m_controller->GetStatus()->GetError())
210 <<
"Error flag should not be set for timeouts";
215 m_status->SetState(State::Scheduled);
219 auto fut = m_controller->ScheduleMergeAsync();
220 EXPECT_TRUE(fut.is_ready());
223 EXPECT_THROW(fut.get(), std::runtime_error);
224 EXPECT_EQ(State::Scheduled, m_controller->GetState());
225 EXPECT_FALSE(m_controller->GetStatus()->GetError());
233 auto fut = m_controller->AbortAsync(ErrorPolicy::Strict);
235 EXPECT_TRUE(fut.is_ready());
236 auto result = fut.get();
237 EXPECT_EQ(State::Aborted, result.state);
238 EXPECT_FALSE(result.error);
239 EXPECT_EQ(State::Aborted, m_controller->GetState());
245 m_status->SetState(State::Scheduled);
249 boost::promise<R> reply;
250 EXPECT_CALL(*m_dpm_client, AbortAsync(
"id"))
251 .WillOnce(InvokeWithoutArgs([&] {
return reply.get_future(); }));
254 auto fut = m_controller->AbortAsync(ErrorPolicy::Strict);
257 reply.set_value(State::Aborted);
262 EXPECT_TRUE(fut.is_ready());
263 auto result = fut.get();
264 EXPECT_EQ(State::Aborted, result.state);
265 EXPECT_FALSE(result.error);
266 EXPECT_EQ(State::Aborted, m_controller->GetState());
271 m_status->SetState(State::Scheduled);
275 boost::promise<R> reply;
276 EXPECT_CALL(*m_dpm_client, AbortAsync(
"id"))
277 .WillOnce(InvokeWithoutArgs([&] {
return reply.get_future(); }));
280 auto fut = m_controller->AbortAsync(ErrorPolicy::Strict);
283 reply.set_exception(std::runtime_error(
"ERROR"));
288 EXPECT_TRUE(fut.is_ready());
289 EXPECT_THROW(fut.get(), std::runtime_error);
290 EXPECT_EQ(State::Scheduled, m_controller->GetState());
291 EXPECT_FALSE(m_controller->GetStatus()->GetError())
292 <<
"Failed to abort is not a condition for marking DAQ as error";