File: update_parallel_test.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (249 lines) | stat: -rw-r--r-- 8,100 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/************************************************************************
 *
 * Copyright (C) 2024-2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "update_parallel_test.hpp"

#include <core/runtime/path.hpp>
#include <core/runtime/runtime.hpp>

#include <service/op.hpp>

#include <utest/wait.hpp>

#include <boost/property_tree/xml_parser.hpp>

#include <ranges>

CPPUNIT_TEST_SUITE_REGISTRATION(sight::app::ut::update_parallel_test);

namespace sight::app::ut
{

/**
 * @brief Service interface for test
 */
class test_update_srv final : public service::base
{
public:

    SIGHT_DECLARE_SERVICE(test_update_srv, service::base);
    ~test_update_srv() noexcept final = default;

    //------------------------------------------------------------------------------

    void configuring(const config_t& /*unused*/) final
    {
    }

    //------------------------------------------------------------------------------

    void starting() final
    {
    }

    //------------------------------------------------------------------------------

    void stopping() final
    {
    }

    //------------------------------------------------------------------------------

    void updating() final
    {
        m_updated = true;
    }

    bool m_updated {false};
};

SIGHT_REGISTER_SERVICE(sight::service::base, sight::app::ut::test_update_srv);

//------------------------------------------------------------------------------

auto create_srv()
{
    auto srv = service::add<sight::app::ut::test_update_srv>("sight::app::ut::test_update_srv");
    CPPUNIT_ASSERT_NO_THROW(srv->configure());
    CPPUNIT_ASSERT_NO_THROW(srv->start().get());
    return srv;
}

//------------------------------------------------------------------------------

void update_parallel_test::setUp()
{
    // Set up context before running a test
    core::runtime::init();

    std::filesystem::path location = core::runtime::get_resource_file_path("app_ut");
    CPPUNIT_ASSERT(std::filesystem::exists(location));

    core::runtime::add_modules(location);
    core::runtime::load_module("sight::module::app");
}

//------------------------------------------------------------------------------

void update_parallel_test::tearDown()
{
}

//------------------------------------------------------------------------------

void update_parallel_test::basic_test()
{
    auto srv0 = create_srv();
    auto srv1 = create_srv();
    auto srv2 = create_srv();
    auto srv3 = create_srv();

    std::stringstream srv_config;
    srv_config
    << "<config>"
    << "<service uid=" << std::quoted(srv0->get_id()) << "/>"
    << "<service uid=" << std::quoted(srv1->get_id()) << "/>"
    << "<service uid=" << std::quoted(srv2->get_id()) << "/>"
    << "<service uid=" << std::quoted(srv3->get_id()) << "/>"
    << "</config>";
    service::config_t config;
    boost::property_tree::read_xml(srv_config, config);

    auto update_srv = service::add("sight::app::update_parallel");
    CPPUNIT_ASSERT(update_srv->is_a("sight::app::update_parallel"));
    CPPUNIT_ASSERT(update_srv->is_a("sight::app::updater"));
    update_srv->set_config(config);
    CPPUNIT_ASSERT_NO_THROW(update_srv->configure());
    CPPUNIT_ASSERT_NO_THROW(update_srv->start().get());

    CPPUNIT_ASSERT_NO_THROW(update_srv->update().get());
    CPPUNIT_ASSERT_EQUAL(true, srv0->m_updated);
    CPPUNIT_ASSERT_EQUAL(true, srv1->m_updated);
    CPPUNIT_ASSERT_EQUAL(true, srv2->m_updated);
    CPPUNIT_ASSERT_EQUAL(true, srv3->m_updated);

    CPPUNIT_ASSERT_NO_THROW(update_srv->stop().get());
    CPPUNIT_ASSERT_NO_THROW(srv0->stop().get());
    CPPUNIT_ASSERT_NO_THROW(srv1->stop().get());
    CPPUNIT_ASSERT_NO_THROW(srv2->stop().get());
    CPPUNIT_ASSERT_NO_THROW(srv3->stop().get());

    service::remove(update_srv);
    service::remove(srv0);
    service::remove(srv1);
    service::remove(srv2);
    service::remove(srv3);
}

//------------------------------------------------------------------------------

void update_parallel_test::parent_test()
{
    std::array<sight::app::ut::test_update_srv::sptr, 7> srv;
    for(const auto i : std::views::iota(0U, 7U))
    {
        srv[i] = create_srv();
    }

    const auto create_updater = [](std::stringstream& _config)
                                {
                                    service::config_t srv_config;
                                    boost::property_tree::read_xml(_config, srv_config);

                                    auto updater = service::add("sight::app::update_parallel");
                                    updater->set_config(srv_config);
                                    CPPUNIT_ASSERT_NO_THROW(updater->configure());
                                    CPPUNIT_ASSERT_NO_THROW(updater->start().get());
                                    return updater;
                                };

    sight::service::base::sptr main_updater;
    sight::service::base::sptr child_updater_1;
    sight::service::base::sptr child_updater_2;
    sight::service::base::sptr child_updater_1_1;

    const std::string updater_1   = "updater_1";
    const std::string updater_2   = "updater_2";
    const std::string updater_1_1 = "updater_1_1";
    {
        std::stringstream srv_config;
        srv_config
        << "<config>"
        << "<service uid=" << std::quoted(srv[0]->get_id()) << "/>"
        << "<updater uid=" << std::quoted(updater_1) << "/>"
        << "<service uid=" << std::quoted(srv[3]->get_id()) << "/>"
        << "<updater uid=" << std::quoted(updater_2) << "/>"
        << "<service uid=" << std::quoted(srv[6]->get_id()) << "/>"
        << "</config>";
        main_updater = create_updater(srv_config);
    }
    {
        std::stringstream srv_config;
        srv_config
        << "<config parent=" << std::quoted(updater_1) << ">"
        << "<updater uid=" << std::quoted(updater_1_1) << "/>"
        << "<service uid=" << std::quoted(srv[2]->get_id()) << "/>"
        << "</config>";
        child_updater_1 = create_updater(srv_config);
    }
    {
        std::stringstream srv_config;
        srv_config
        << "<config parent=" << std::quoted(updater_2) << ">"
        << "<service uid=" << std::quoted(srv[4]->get_id()) << "/>"
        << "<service uid=" << std::quoted(srv[5]->get_id()) << "/>"
        << "</config>";
        child_updater_2 = create_updater(srv_config);
    }
    {
        std::stringstream srv_config;
        srv_config
        << "<config parent=" << std::quoted(updater_1_1) << ">"
        << "<service uid=" << std::quoted(srv[1]->get_id()) << "/>"
        << "</config>";
        child_updater_1_1 = create_updater(srv_config);
    }

    main_updater->update().get();

    for(const auto i : std::views::iota(0U, 7U))
    {
        CPPUNIT_ASSERT_EQUAL(true, srv[i]->m_updated);
    }

    CPPUNIT_ASSERT_NO_THROW(main_updater->stop().get());
    CPPUNIT_ASSERT_NO_THROW(child_updater_1->stop().get());
    CPPUNIT_ASSERT_NO_THROW(child_updater_1_1->stop().get());
    CPPUNIT_ASSERT_NO_THROW(child_updater_2->stop().get());

    service::remove(main_updater);
    service::remove(child_updater_1);
    service::remove(child_updater_1_1);
    service::remove(child_updater_2);

    for(const auto i : std::views::iota(0U, 7U))
    {
        CPPUNIT_ASSERT_NO_THROW(srv[i]->stop().get());
        service::remove(srv[i]);
    }
}

} // namespace sight::app::ut