File: config_launcher_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 (216 lines) | stat: -rw-r--r-- 7,557 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
/************************************************************************
 *
 * Copyright (C) 2023-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 "config_launcher_test.hpp"

#include "test_service.hpp"

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

#include <service/extension/config.hpp>
#include <service/op.hpp>

#include <ui/__/parameter.hpp>

#include <utest/wait.hpp>

#include <app/extension/parameters.hpp>

#include <boost/config.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <filesystem>
#include <regex>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::app::ut::config_launcher_test);

namespace sight::app::ut
{

static constexpr auto MULTI_CFG_CTL_1 = "multi_cfg_ctl_1";
static constexpr auto MULTI_CFG_CTL_2 = "multi_cfg_ctl_2";

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

int get_current_id()
{
    auto current_id_str = sight::app::extension::config::get_unique_identifier("get_id");
    std::smatch match;
    CPPUNIT_ASSERT(std::regex_match(current_id_str, match, std::regex("get_id[^0-9]+([0-9]*)")));
    current_id_str = match[1].str();
    return std::stoi(current_id_str);
}

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

void config_launcher_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");
    core::runtime::load_module("config_test");

    app::extension::config::sptr app_config = app::extension::config::get();
    app_config->clear_registry();
    app_config->parse_plugin_infos();
}

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

void config_launcher_test::tearDown()
{
    // Clean up after the test run.
    // unregister the services that have not been unregistered because a test failed.

    auto services = sight::service::get_services("sight::app::config_launcher");
    for(const auto& srv : services)
    {
        if(srv->started())
        {
            srv->stop().get();
        }

        service::unregister_service(srv);
    }
}

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

void config_launcher_test::multi_config_test()
{
    auto config_id = std::make_shared<sight::data::string>();

    auto srv = service::add("sight::app::config_launcher");
    srv->set_inout(config_id, "config");

    srv->configure();
    srv->start().get();

    // start the test!
    // set config2 =>
    //  srv1 : not existing
    //  srv2 : started

    int current_id = get_current_id();
    config_id->set_value(MULTI_CFG_CTL_2);
    srv->update().get();
    auto srv1_uid = core::id::join(MULTI_CFG_CTL_1, current_id++, "srv");
    auto srv2_uid = core::id::join(MULTI_CFG_CTL_2, current_id++, "srv");

    auto srv1 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 == nullptr);
    auto srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->started());

    // set config1 =>
    //  srv1 : started
    //  srv2 : stopped
    config_id->set_value(MULTI_CFG_CTL_1);
    config_id->emit(sight::data::object::MODIFIED_SIG);

    srv1_uid = core::id::join(MULTI_CFG_CTL_1, current_id++, "srv");
    srv1     = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 != nullptr && srv1->started());
    srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->stopped());
    {
        // Ensuring the service is really destroyed by the app config manager by releasing our reference
        srv2.reset();
        auto srv2_test = core::id::get_object(srv2_uid);
        CPPUNIT_ASSERT(srv2_test == nullptr);
    }

    // set config2 =>
    //  srv1 : stopped
    //  srv2 : started
    config_id->set_value(MULTI_CFG_CTL_2);
    config_id->emit(sight::data::object::MODIFIED_SIG);

    srv2_uid = core::id::join(MULTI_CFG_CTL_2, current_id++, "srv");
    srv1     = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 != nullptr && srv1->stopped());
    srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->started());

    // set again config2 => nothing should have changed, the service is not updated
    //  srv1 : stopped
    //  srv2 : started
    config_id->emit(sight::data::object::MODIFIED_SIG);

    srv1 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 != nullptr && srv1->stopped());
    srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->started());

    srv->stop().wait();
}

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

void config_launcher_test::set_config_key_test()
{
    auto config_id = std::make_shared<sight::data::string>(MULTI_CFG_CTL_1);

    // Initialise the testing service
    service::base::sptr srv = service::add("sight::app::config_launcher");
    srv->set_inout(config_id, "config");

    srv->configure();
    srv->start().wait();

    // start the test!
    // set config2 =>
    //  multi_cfg_ctl_1_10_srv : not existing
    //  multi_cfg_ctl_2_8_srv : started

    int current_id = get_current_id();
    config_id->set_value(MULTI_CFG_CTL_2);
    srv->update().get();

    auto srv1_uid = core::id::join(MULTI_CFG_CTL_1, current_id++, "srv");
    auto srv2_uid = core::id::join(MULTI_CFG_CTL_2, current_id++, "srv");
    auto srv1     = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 == nullptr);
    auto srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->started());

    // set config with bad key
    config_id->set_value("Unknown config");
    CPPUNIT_ASSERT_THROW(srv->update().get(), core::exception);

    srv1 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv1_uid));
    CPPUNIT_ASSERT(srv1 == nullptr);
    srv2 = std::dynamic_pointer_cast<app::ut::test_service>(core::id::get_object(srv2_uid));
    CPPUNIT_ASSERT(srv2 != nullptr && srv2->stopped());

    srv->stop().wait();
}

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

} // namespace sight::app::ut