ifw-daq  2.1.0-pre1
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  }
34  void TearDown() override {}
35 
36  boost::asio::io_context m_io_ctx;
37  std::shared_ptr<daq::ObservableStatus> m_status;
38 };
39 
40 
41 TEST_F(TestAsyncOpAwaitState, ConditionAlreadySatisfiedReturnsReadyFuture) {
42  // Test
43  op::AwaitStateAsync op(m_io_ctx, m_status, State::NotStarted, 100ms);
44  auto fut = op.Initiate();
45  ASSERT_TRUE(fut.is_ready());
46  auto result = fut.get();
47  EXPECT_FALSE(result.error);
48  EXPECT_EQ(result.result, *m_status);
49 }
50 
51 
52 TEST_F(TestAsyncOpAwaitState, ConditionSatisfiedByStateChange) {
53  // Setup
54  // Test
55  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
56  m_io_ctx, m_status, State::Acquiring, 100ms);
57  (void)abort;
58 
59  EXPECT_FALSE(fut.is_ready());
60  m_io_ctx.poll();
61  EXPECT_FALSE(fut.is_ready());
62 
63  m_status->SetState(State::Acquiring);
64  MakeTestProgress(m_io_ctx, &fut);
65  ASSERT_TRUE(fut.is_ready());
66 
67  auto result = fut.get();
68  EXPECT_FALSE(result.error);
69  EXPECT_EQ(result.result, *m_status);
70 }
71 
72 
73 TEST_F(TestAsyncOpAwaitState, ConditionTimesOut) {
74  // Setup
75  // Test
76  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
77  m_io_ctx, m_status, State::Acquiring, 0ms);
78  (void)abort;
79 
80  MakeTestProgress(m_io_ctx, &fut);
81  ASSERT_TRUE(fut.is_ready());
82 
83  auto result = fut.get();
84  EXPECT_TRUE(result.error);
85  EXPECT_EQ(result.result, *m_status);
86 }
87 
88 
89 TEST_F(TestAsyncOpAwaitState, OperationAborted) {
90  // Setup
91  // Test
92  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
93  m_io_ctx, m_status, State::Acquiring, 100ms);
94 
95  abort();
96  MakeTestProgress(m_io_ctx, &fut);
97  ASSERT_TRUE(fut.is_ready());
98 
99  EXPECT_THROW(fut.get(), DaqOperationAborted);
100 }
101 
102 
103 TEST_F(TestAsyncOpAwaitState, ConditionSatisfiedAtTimeout) {
104  // Setup
105  // Test
106  auto [fut, abort] = op::InitiateAbortableOperation<op::AwaitStateAsync>(
107  m_io_ctx, m_status, State::Acquiring, 0ms);
108  (void)abort;
109 
110  // Hack to bypass observer notification
111  auto& mut_status = const_cast<Status&>(m_status->GetStatus());
112  mut_status.state = State::Acquiring;
113 
114  MakeTestProgress(m_io_ctx, &fut);
115  ASSERT_TRUE(fut.is_ready());
116 
117  auto result = fut.get();
118  EXPECT_FALSE(result.error);
119  EXPECT_EQ(result.result, *m_status);
120 }
initiate.hpp
Contains declarations for the helper functions to initiate operations.
utils.hpp
Defines shared test utilities.
awaitState.hpp
Contains declaration for the AwaitStateAsync operation.
daq
Definition: asyncProcess.cpp:15
config.hpp
TestAsyncOpAwaitState::TearDown
void TearDown() override
Definition: testAsyncOpAwaitState.cpp:34
daq::op::AwaitStateAsync::Initiate
boost::future< ResultType > Initiate()
Initiates operation that await state.
Definition: awaitState.cpp:32
daq::TEST_F
TEST_F(TestDpmDaqController, StatusUpdateInNotScheduledSucceeds)
Definition: testDpmDaqController.cpp:60
daq::op::AwaitStateAsync
Async operation to await Data Acquisition state.
Definition: awaitState.hpp:32
TestAsyncOpAwaitState::TestAsyncOpAwaitState
TestAsyncOpAwaitState()=default
TestAsyncOpAwaitState::m_status
std::shared_ptr< daq::ObservableStatus > m_status
Definition: testAsyncOpAwaitState.cpp:37
TestAsyncOpAwaitState::SetUp
void SetUp() override
Definition: testAsyncOpAwaitState.cpp:31
TestAsyncOpAwaitState::m_io_ctx
boost::asio::io_context m_io_ctx
Definition: testAsyncOpAwaitState.cpp:36
daq::Status
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:120
daq::Status::state
State state
Definition: status.hpp:138
daq::DaqOperationAborted
Started operation was aborted.
Definition: error.hpp:47
TestAsyncOpAwaitState
Definition: testAsyncOpAwaitState.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