File: fixture_dds_gateway.hpp

package info (click to toggle)
iceoryx 2.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,260 kB
  • sloc: cpp: 94,127; ansic: 1,443; sh: 1,436; python: 1,377; xml: 80; makefile: 61
file content (109 lines) | stat: -rw-r--r-- 4,523 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef TEST_HELPERS_FIXTURE_DDS_GATEWAY_H
#define TEST_HELPERS_FIXTURE_DDS_GATEWAY_H

#include "iceoryx_posh/gateway/channel.hpp"

#include "mocks/google_mocks.hpp"
#include "test.hpp"

template <typename IceoryxTerminal, typename DDSTerminal>
class DDSGatewayTestFixture : public Test
{
  public:
    // Holds mocks created by tests to be used by the channel factory.
    std::vector<std::shared_ptr<IceoryxTerminal>> m_stagedMockIceoryxTerminal;
    std::vector<std::shared_ptr<DDSTerminal>> m_stagedMockDDSTerminal;

    // Marks where in the array to look for a valid mock.
    // Indexes lower than the marker are assumed to have been moved out and thus undefined.
    size_t m_mockIceoryxTerminalCursor = 0;
    size_t m_mockDDSTerminalMarker = 0;

    void SetUp(){};
    void TearDown()
    {
        m_stagedMockIceoryxTerminal.clear();
        m_mockIceoryxTerminalCursor = 0;
        m_stagedMockDDSTerminal.clear();
        m_mockDDSTerminalMarker = 0;
    };

    // Create a new dds mock
    std::shared_ptr<DDSTerminal> createMockDDSTerminal(const iox::capro::ServiceDescription& sd)
    {
        return std::shared_ptr<DDSTerminal>(new DDSTerminal(sd));
    }
    // Stage the given mock to be used in the channel factory
    void stageMockDDSTerminal(std::shared_ptr<DDSTerminal>&& mock)
    {
        // Pass ownership - do not hold a reference here.
        m_stagedMockDDSTerminal.emplace_back(std::move(mock));
    };
    // Create a new iceoryx mock
    template <typename IceoryxPubSubOptions>
    std::shared_ptr<IceoryxTerminal> createMockIceoryxTerminal(const iox::capro::ServiceDescription& sd,
                                                               const IceoryxPubSubOptions& options)
    {
        return std::shared_ptr<IceoryxTerminal>(new IceoryxTerminal(sd, options));
    }
    // Stage the given mock to be used in the channel factory
    void stageMockIceoryxTerminal(std::shared_ptr<IceoryxTerminal>&& mock)
    {
        m_stagedMockIceoryxTerminal.emplace_back(std::move(mock));
    };

    // Creates channels to be used in tests.
    // Channels will contain staged mocks, or empty mocks if none are staged.
    // The factory method can be passed to test gateways, allowing injection of mocks.
    template <typename IceoryxPubSubOptions>
    iox::cxx::expected<iox::gw::Channel<IceoryxTerminal, DDSTerminal>, iox::gw::GatewayError>
    channelFactory(iox::capro::ServiceDescription sd, const IceoryxPubSubOptions& options) noexcept
    {
        // Get or create a mock iceoryx terminal
        std::shared_ptr<IceoryxTerminal> mockIceoryxTerminal;
        if (m_mockIceoryxTerminalCursor < m_stagedMockIceoryxTerminal.size())
        {
            // Important - must pass ownership to receiver so object is deleted when the receiver is done with it.
            mockIceoryxTerminal = std::move(m_stagedMockIceoryxTerminal.at(m_mockIceoryxTerminalCursor++));
        }
        else
        {
            mockIceoryxTerminal = createMockIceoryxTerminal(sd, options);
        }

        // Get or create a mock dds terminal
        std::shared_ptr<DDSTerminal> mockDataWriter;
        if (m_mockDDSTerminalMarker < m_stagedMockDDSTerminal.size())
        {
            // Important - must pass ownership to receiver so object is deleted when the receiver is done with it.
            mockDataWriter = std::move(m_stagedMockDDSTerminal.at(m_mockDDSTerminalMarker++));
        }
        else
        {
            mockDataWriter = createMockDDSTerminal(sd);
        }

        return iox::cxx::success<iox::gw::Channel<IceoryxTerminal, DDSTerminal>>(
            iox::gw::Channel<IceoryxTerminal, DDSTerminal>(
                sd, std::move(mockIceoryxTerminal), std::move(mockDataWriter)));
    }
};

#endif // TEST_HELPERS_FIXTURE_DDS_GATEWAY_H