ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
testAsyncOpStart.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::StartAsync
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/op/initiate.hpp>
15 #include <daq/op/start.hpp>
16 #include <daq/error.hpp>
17 
18 #include "mock/metadaqifMock.hpp"
19 #include "mock/recifMock.hpp"
20 #include "testAsyncOpBase.hpp"
21 
22 using namespace daq;
23 using namespace ::testing;
24 
25 /**
26  * @ingroup daq_ocm_libdaq_test
27  */
29 
30 };
31 
33  // Setup
34  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
35  auto reply_mock = std::make_shared<DaqReplyMock>();
36  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
37  auto reply_mock2 = std::make_shared<DaqReplyMock>();
38  // Set up mock so that StartDaq invocation returns the future from our promise.
39  EXPECT_CALL(*m_meta_rr_client, StartDaq("id"))
40  .WillOnce(Return(ByMove(reply_promise.get_future())));
41  EXPECT_CALL(*m_meta_rr_client2, StartDaq("id"))
42  .WillOnce(Return(ByMove(reply_promise2.get_future())));
43 
44  boost::promise<std::shared_ptr<recif::RecStatus>> prim_promise_1;
45  boost::promise<std::shared_ptr<recif::RecStatus>> prim_promise_2;
46  auto prim_reply_mock_1 = std::make_shared<RecStatusMock>();
47  EXPECT_CALL(*m_prim_rr_client, RecStart(RecPropertyIdEq("id")))
48  .WillOnce(Return(ByMove(prim_promise_1.get_future())));
49  EXPECT_CALL(*m_prim_rr_client2, RecStart(RecPropertyIdEq("id")))
50  .WillOnce(Return(ByMove(prim_promise_2.get_future())));
51 
52  // Run
53  boost::future<void> fut = op::InitiateOperation<op::StartAsync>(MakeParams());
54  EXPECT_FALSE(fut.is_ready());
55 
56  // "Send reply"
57  reply_promise.set_value(reply_mock);
58  reply_promise2.set_value(reply_mock2);
59  prim_promise_1.set_value(prim_reply_mock_1);
60  prim_promise_2.set_value(prim_reply_mock_1);
61 
62  // Execute scheduled handlers
63  MakeTestProgress(m_io_ctx, &fut);
64 
65  ASSERT_TRUE(fut.is_ready());
66  ASSERT_FALSE(fut.has_exception());
67 }
68 
69 
70 TEST_F(TestAsyncOpStart, StartingFailsToSendStartDaqWillAbortAndSetErrorFlagAndStayInStarting) {
71  // Setup
72  // Set up mock so that StartDaq throws exception.
73  EXPECT_CALL(*m_meta_rr_client, StartDaq(_))
74  .WillOnce(Throw(std::runtime_error("Fake test failure")));
75 
76  // Second client is never started since first fails
77  EXPECT_CALL(*m_meta_rr_client2, StartDaq(_)).Times(0);
78 
79  // Run
80  boost::future<void> fut = op::InitiateOperation<op::StartAsync>(MakeParams());
81  EXPECT_FALSE(fut.is_ready());
82 
83  // Make source 2 ready
84  // Run async handlers
85  MakeTestProgress(m_io_ctx, &fut);
86 
87  ASSERT_TRUE(fut.is_ready());
88  EXPECT_TRUE(fut.has_exception()) << "Expected future to contain exception";
89  EXPECT_THROW(fut.get(), std::exception) << "Expected exception to derive from std::exception";
90 }
91 
92 
93 TEST_F(TestAsyncOpStart, StartAsyncFutureHasExceptionIfStartDaqReturnsError) {
94  // Setup
95  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise;
96  auto reply_mock = std::make_shared<DaqReplyMock>();
97  EXPECT_CALL(*reply_mock, getMessage()).WillRepeatedly(Return(std::string("some message")));
98 
99  boost::promise<std::shared_ptr<metadaqif::DaqReply>> reply_promise2;
100  auto reply_mock2 = std::make_shared<DaqReplyMock>();
101  EXPECT_CALL(*reply_mock2, getMessage()).WillRepeatedly(Return(std::string("some message")));
102 
103  // Set up mock so that StartDaq invocation returns the future from our promise.
104  EXPECT_CALL(*m_meta_rr_client, StartDaq(_))
105  .WillOnce(Return(ByMove(reply_promise.get_future())));
106 
107  EXPECT_CALL(*m_meta_rr_client2, StartDaq(_))
108  .WillOnce(Return(ByMove(reply_promise2.get_future())));
109 
110  // Run
111  boost::future<void> fut = op::InitiateOperation<op::StartAsync>(MakeParams());
112  EXPECT_FALSE(fut.is_ready());
113 
114  // "Send error reply"
115  reply_promise.set_exception(metadaqif::DaqException("foo", "baz"));
116  reply_promise2.set_value(reply_mock2);
117 
118  // Execute scheduled handlers
119  MakeTestProgress(m_io_ctx, &fut);
120 
121  ASSERT_TRUE(fut.is_ready());
122  EXPECT_THROW(fut.get(), DaqSourceErrors);
123 }
initiate.hpp
Contains declarations for the helper functions to initiate operations.
metadaqifMock.hpp
Mockup of metadaqif classes.
start.hpp
Contains declaration for the StartAsync operation.
daq::DaqSourceErrors
Exception thrown to carry reply errors.
Definition: error.hpp:84
recifMock.hpp
Mockup of metadaqif classes.
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
TestAsyncOpStart
Definition: testAsyncOpStart.cpp:28
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