ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
testAsyncOpAbort.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 Unit test for op::AbortAsync
7  */
8 #include <daq/config.hpp>
9 
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
12 #include <log4cplus/logger.h>
13 
14 #include <daq/error.hpp>
15 #include <daq/op/initiate.hpp>
16 #include <daq/op/abort.hpp>
17 
18 #include "mock/metadaqifMock.hpp"
19 #include "testAsyncOpBase.hpp"
20 
21 using namespace daq;
22 using namespace ::testing;
23 
24 /**
25  * @ingroup daq_ocm_libdaq_test
26  */
28 
29 };
30 
32  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
33  auto reply_mock = std::make_shared<DaqReplyMock>();
34 
35  EXPECT_CALL(*m_meta_rr_client, AbortDaq(_))
36  .WillOnce(Return(ByMove(reply_promise.get_future())));
37 
38  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
39  auto reply_mock2 = std::make_shared<DaqReplyMock>();
40  EXPECT_CALL(*m_meta_rr_client2, AbortDaq(_))
41  .WillOnce(Return(ByMove(reply_promise2.get_future())));
42 
43  boost::promise<std::string> prim_promise_1;
44  boost::promise<std::string> prim_promise_2;
45  EXPECT_CALL(*m_prim_rr_client, RecAbort())
46  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
47  EXPECT_CALL(*m_prim_rr_client2, RecAbort())
48  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
49 
50  // Run
51  auto fut = op::InitiateOperation<op::AbortAsync>(ErrorPolicy::Strict, MakeParams());
52 
53  // "Send replies"
54  reply_promise.set_value(reply_mock);
55  reply_promise2.set_value(reply_mock);
56  prim_promise_1.set_value("OK"); // Unclear what RecIf is supposed to return here.
57  prim_promise_2.set_value("OK"); // Unclear what RecIf is supposed to return here.
58 
59  // Execute handlers
60  MakeTestProgress(m_io_ctx, &fut);
61 
62  ASSERT_TRUE(fut.is_ready());
63  ASSERT_FALSE(fut.has_exception()) << "Future has unexpected exception!";
64 }
65 
66 TEST_F(TestAsyncOpAbort, AbortFailsWithStrictPolicyIfMetaSourceFails) {
67  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
68  auto reply_mock = std::make_shared<DaqReplyMock>();
69 
70  EXPECT_CALL(*m_meta_rr_client, AbortDaq(_))
71  .WillOnce(Return(ByMove(reply_promise.get_future())));
72 
73  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
74  auto reply_mock2 = std::make_shared<DaqReplyMock>();
75  EXPECT_CALL(*m_meta_rr_client2, AbortDaq(_))
76  .WillOnce(Return(ByMove(reply_promise2.get_future())));
77 
78  boost::promise<std::string> prim_promise_1;
79  boost::promise<std::string> prim_promise_2;
80 
81  EXPECT_CALL(*m_prim_rr_client, RecAbort())
82  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
83  EXPECT_CALL(*m_prim_rr_client2, RecAbort())
84  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
85 
86  // Run
87  auto fut = op::InitiateOperation<op::AbortAsync>(ErrorPolicy::Strict, MakeParams());
88 
89  // "Send reply"
90  reply_promise.set_value(reply_mock);
91  reply_promise2.set_exception(metadaqif::DaqException(m_id, "message"));
92  prim_promise_1.set_value("OK"); // Unclear what RecIf is supposed to return here.
93  prim_promise_2.set_value("OK"); // Unclear what RecIf is supposed to return here.
94 
95  // Execute handlers
96  MakeTestProgress(m_io_ctx, &fut);
97 
98  ASSERT_TRUE(fut.is_ready());
99  EXPECT_THROW(fut.get(), daq::DaqSourceErrors);
100 }
101 
102 TEST_F(TestAsyncOpAbort, AbortFailsWithStrictPolicyIfPrimSourceFails) {
103  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
104  auto reply_mock = std::make_shared<DaqReplyMock>();
105 
106  EXPECT_CALL(*m_meta_rr_client, AbortDaq(_))
107  .WillOnce(Return(ByMove(reply_promise.get_future())));
108 
109  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
110  auto reply_mock2 = std::make_shared<DaqReplyMock>();
111  EXPECT_CALL(*m_meta_rr_client2, AbortDaq(_))
112  .WillOnce(Return(ByMove(reply_promise2.get_future())));
113 
114  boost::promise<std::string> prim_promise_1;
115  boost::promise<std::string> prim_promise_2;
116 
117  EXPECT_CALL(*m_prim_rr_client, RecAbort())
118  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
119  EXPECT_CALL(*m_prim_rr_client2, RecAbort())
120  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
121 
122  // Run
123  auto fut = op::InitiateOperation<op::AbortAsync>(ErrorPolicy::Strict, MakeParams());
124 
125  // "Send reply"
126  reply_promise.set_value(reply_mock);
127  reply_promise2.set_value(reply_mock);
128  prim_promise_1.set_value("OK"); // Unclear what RecIf is supposed to return here.
129  prim_promise_2.set_exception(metadaqif::DaqException(m_id, "message"));
130 
131  // Execute handlers
132  MakeTestProgress(m_io_ctx, &fut);
133 
134  ASSERT_TRUE(fut.is_ready());
135  EXPECT_THROW(fut.get(), daq::DaqSourceErrors);
136 }
137 
138 TEST_F(TestAsyncOpAbort, AbortFailsWithTolerantPolicyIfMetaSourceFails) {
139  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
140  auto reply_mock = std::make_shared<DaqReplyMock>();
141 
142  EXPECT_CALL(*m_meta_rr_client, AbortDaq(_))
143  .WillOnce(Return(ByMove(reply_promise.get_future())));
144 
145  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
146  auto reply_mock2 = std::make_shared<DaqReplyMock>();
147  EXPECT_CALL(*m_meta_rr_client2, AbortDaq(_))
148  .WillOnce(Return(ByMove(reply_promise2.get_future())));
149 
150  boost::promise<std::string> prim_promise_1;
151  boost::promise<std::string> prim_promise_2;
152  EXPECT_CALL(*m_prim_rr_client, RecAbort())
153  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
154  EXPECT_CALL(*m_prim_rr_client2, RecAbort())
155  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
156 
157  // Run
158  auto fut = op::InitiateOperation<op::AbortAsync>(ErrorPolicy::Tolerant, MakeParams());
159 
160  // "Send reply"
161  reply_promise.set_value(reply_mock);
162  reply_promise2.set_exception(metadaqif::DaqException(m_id, "message"));
163  prim_promise_1.set_value("OK"); // Unclear what RecIf is supposed to return here.
164  prim_promise_2.set_value("OK"); // Unclear what RecIf is supposed to return here.
165 
166  // Execute handlers
167  MakeTestProgress(m_io_ctx, &fut);
168 
169  ASSERT_TRUE(fut.is_ready());
170  EXPECT_FALSE(fut.has_exception());
171 }
172 
173 TEST_F(TestAsyncOpAbort, AbortFailsWithTolerantPolicyIfPrimSourceFails) {
174  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
175  auto reply_mock = std::make_shared<DaqReplyMock>();
176 
177  EXPECT_CALL(*m_meta_rr_client, AbortDaq(_))
178  .WillOnce(Return(ByMove(reply_promise.get_future())));
179 
180  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
181  auto reply_mock2 = std::make_shared<DaqReplyMock>();
182  EXPECT_CALL(*m_meta_rr_client2, AbortDaq(_))
183  .WillOnce(Return(ByMove(reply_promise2.get_future())));
184 
185  boost::promise<std::string> prim_promise_1;
186  boost::promise<std::string> prim_promise_2;
187  EXPECT_CALL(*m_prim_rr_client, RecAbort())
188  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
189  EXPECT_CALL(*m_prim_rr_client2, RecAbort())
190  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
191 
192  // Run
193  auto fut = op::InitiateOperation<op::AbortAsync>(ErrorPolicy::Tolerant, MakeParams());
194 
195  // "Send reply"
196  reply_promise.set_value(reply_mock);
197  reply_promise2.set_value(reply_mock);
198  prim_promise_1.set_value("OK");
199  prim_promise_2.set_exception(std::runtime_error("some error")); // recif does not defin exception
200 
201  // Execute handlers
202  MakeTestProgress(m_io_ctx, &fut);
203 
204  ASSERT_TRUE(fut.is_ready());
205  EXPECT_FALSE(fut.has_exception());
206 }
abort.hpp
Contains declaration for the AbortAsync operation.
initiate.hpp
Contains declarations for the helper functions to initiate operations.
metadaqifMock.hpp
Mockup of metadaqif classes.
daq::DaqSourceErrors
Exception thrown to carry reply errors.
Definition: error.hpp:84
daq
Definition: asyncProcess.cpp:15
config.hpp
testAsyncOpBase.hpp
Contains declaration for async operations shared base class.
daq::TEST_F
TEST_F(TestDpmDaqController, StatusUpdateInNotScheduledSucceeds)
Definition: testDpmDaqController.cpp:60
TestAsyncOpBase
Base fixture for async operation tests.
Definition: testAsyncOpBase.hpp:38
TestAsyncOpAbort
Definition: testAsyncOpAbort.cpp:27
daq::ErrorPolicy::Strict
@ Strict
Any error is considered fatal and may lead to the operation being aborted.
error.hpp
Contains error related declarations for DAQ.
MakeTestProgress
void MakeTestProgress(boost::asio::io_context &io_ctx, Future *fut=nullptr)
Test helper that progress the test by executing pending jobs and optionally wait for a future to be r...
Definition: utils.hpp:42