ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
testAsyncOpAwaitState.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::AwaitStateAsync
7  */
8 #include <daq/config.hpp>
9 
10 #include <gtest/gtest.h>
11 #include <log4cplus/logger.h>
12 
13 #include <daq/error.hpp>
14 #include <daq/op/initiate.hpp>
15 #include <daq/op/awaitState.hpp>
16 
17 #include "utils.hpp"
18 
19 
20 using namespace daq;
21 using namespace ::testing;
22 using namespace std::chrono_literals;
23 
24 
25 /**
26  * @ingroup daq_ocm_libdaq_test
27  */
28 struct TestAsyncOpAwaitState : ::testing::Test {
29  TestAsyncOpAwaitState() = default;
30 
31  void SetUp() override {
32  m_status = std::make_shared<ObservableStatus>("id", "fileid");
33  m_logger= log4cplus::Logger::getInstance("test");
34  }
35  void TearDown() override {}
36 
37  boost::asio::io_context m_io_ctx;
38  std::shared_ptr<daq::ObservableStatus> m_status;
39  log4cplus::Logger m_logger;
40 };
41 
42 
43 TEST_F(TestAsyncOpAwaitState, ConditionAlreadySatisfiedReturnsReadyFuture) {
44  // Test
45  op::AwaitStateAsync op(m_io_ctx, m_status, State::NotStarted, 100ms, m_logger);
46  auto fut = op.Initiate();
47  ASSERT_TRUE(fut.is_ready());
48  auto result = fut.get();
49  EXPECT_FALSE(result.error);
50  EXPECT_EQ(result.result, *m_status);
51 }
52 
53 
54 TEST_F(TestAsyncOpAwaitState, ConditionSatisfiedByStateChange) {
55  // Setup
56  // Test
57  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
58  m_io_ctx, m_status, State::Acquiring, 100ms, m_logger);
59  (void)abort;
60 
61  EXPECT_FALSE(fut.is_ready());
62  m_io_ctx.poll();
63  EXPECT_FALSE(fut.is_ready());
64 
65  m_status->SetState(State::Acquiring);
66  MakeTestProgress(m_io_ctx, &fut);
67  ASSERT_TRUE(fut.is_ready());
68 
69  auto result = fut.get();
70  EXPECT_FALSE(result.error);
71  EXPECT_EQ(result.result, *m_status);
72 }
73 
74 
75 TEST_F(TestAsyncOpAwaitState, ConditionTimesOut) {
76  // Setup
77  // Test
78  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
79  m_io_ctx, m_status, State::Acquiring, 0ms, m_logger);
80  (void)abort;
81 
82  MakeTestProgress(m_io_ctx, &fut);
83  ASSERT_TRUE(fut.is_ready());
84 
85  auto result = fut.get();
86  EXPECT_TRUE(result.error);
87  EXPECT_EQ(result.result, *m_status);
88 }
89 
90 
91 TEST_F(TestAsyncOpAwaitState, OperationAborted) {
92  // Setup
93  // Test
94  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
95  m_io_ctx, m_status, State::Acquiring, 100ms, m_logger);
96 
97  abort();
98  MakeTestProgress(m_io_ctx, &fut);
99  ASSERT_TRUE(fut.is_ready());
100 
101  EXPECT_THROW(fut.get(), DaqOperationAborted);
102 }
103 
104 
105 TEST_F(TestAsyncOpAwaitState, ConditionSatisfiedAtTimeout) {
106  // Setup
107  // Test
108  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
109  m_io_ctx, m_status, State::Acquiring, 0ms, m_logger);
110  (void)abort;
111 
112  // Hack to bypass observer notification
113  auto& mut_status = const_cast<Status&>(m_status->GetStatus());
114  mut_status.state = State::Acquiring;
115 
116  MakeTestProgress(m_io_ctx, &fut);
117  ASSERT_TRUE(fut.is_ready());
118 
119  auto result = fut.get();
120  EXPECT_FALSE(result.error);
121  EXPECT_EQ(result.result, *m_status);
122 }
Contains declaration for the AwaitStateAsync operation.
Started operation was aborted.
Definition: error.hpp:47
Contains error related declarations for DAQ.
boost::asio::io_context m_io_ctx
std::shared_ptr< daq::ObservableStatus > m_status
TestAsyncOpAwaitState()=default
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
Contains declarations for the helper functions to initiate operations.
TEST_F(TestDpmDaqController, StatusUpdateInNotScheduledSucceeds)
@ Acquiring
All data sources have reported data acquisition is in progress.
@ NotStarted
Initial state of data acquisition.
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:124
State state
Definition: status.hpp:142
Async operation to await Data Acquisition state.
Definition: awaitState.hpp:32
boost::future< ResultType > Initiate()
Initiates operation that await state.
Definition: awaitState.cpp:33
EXPECT_EQ(meta.rr_uri, "zpb.rr://meta")
Defines shared test utilities.