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