Skip to content

Commit 72f1505

Browse files
goncaloamaralVictor Carvalho
authored andcommitted
fix/doc offered_services_info
Document and fix test scripts for offered_services_info. Test was also failing sporadically because it would not have the endpoints created and so, the reliability of the services would be wrong, messing up the local/remote offered services.
1 parent b64ddfd commit 72f1505

9 files changed

+110
-108
lines changed

test/network_tests/offered_services_info_tests/CMakeLists.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
1+
# Copyright (C) 2023-2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
22
# This Source Code Form is subject to the terms of the Mozilla Public
33
# License, v. 2.0. If a copy of the MPL was not distributed with this
44
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -32,14 +32,9 @@ set(executables
3232
targets_add_default_dependencies("${executables}")
3333
targets_link_default_libraries("${executables}")
3434

35-
# Skip valgrind
36-
if (DEFINED VALGRIND_TYPE AND NOT VALGRIND_TYPE STREQUAL "")
37-
set(TEST_ENTRYPOINT ${TEST_ENTRYPOINT} --trace-children-skip=*/offered_services_info_test_*)
38-
endif()
39-
4035
# Add custom test command.
4136
add_custom_test(
4237
NAME offered_services_info_test_local
4338
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/offered_services_info_test_local_starter.sh
44-
TIMEOUT 180
39+
TIMEOUT 60
4540
)

test/network_tests/offered_services_info_tests/conf/offered_services_info_test_local.json.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"unreliable" : "31004"
3737
}
3838
],
39-
"routing" : "routingmanagerd",
39+
"routing" : "service-sample",
4040
"service-discovery" :
4141
{
4242
"enable" : "false",
Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2+
# Copyright (C) 2015-2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
33
# This Source Code Form is subject to the terms of the Mozilla Public
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -12,47 +12,14 @@
1212

1313
FAIL=0
1414

15-
cat <<End-of-message
16-
*******************************************************************************
17-
*******************************************************************************
18-
** Running first test
19-
*******************************************************************************
20-
*******************************************************************************
21-
End-of-message
22-
23-
# Rejecting offer of service instance whose hosting application is still
24-
# alive:
25-
# * start application which offers service
26-
# * start two clients which continuously exchanges messages with the service
27-
# * start application which offers the same service again -> should be
28-
# rejected and an error message should be printed.
29-
# * Message exchange with client application should not be interrupted.
30-
31-
# Array for client pids
32-
CLIENT_PIDS=()
15+
export VSOMEIP_APPLICATION_NAME="service-sample"
3316
export VSOMEIP_CONFIGURATION=offered_services_info_test_local.json
34-
# Start the services (routingmanagerd as app name)
35-
./offered_services_info_test_service 1 & #routingmanagerd as app name
17+
./offered_services_info_test_service 1 &
3618
PID_SERVICE_ONE=$!
37-
./offered_services_info_test_client METHODCALL &
38-
CLIENT_PIDS+=($!)
39-
40-
# Wait until all clients are finished
41-
for job in ${CLIENT_PIDS[*]}
42-
do
43-
# Fail gets incremented if a client exits with a non-zero exit code
44-
wait $job || FAIL=$(($FAIL+1))
45-
done
4619

47-
# kill the services
48-
kill $PID_SERVICE_ONE
49-
sleep 1
20+
export VSOMEIP_APPLICATION_NAME="client-sample"
21+
./offered_services_info_test_client METHODCALL || FAIL=$(($FAIL+1))
5022

23+
wait $PID_SERVICE_ONE || FAIL=$(($FAIL+1))
5124

52-
# Check if everything went well
53-
if [ $FAIL -eq 0 ]
54-
then
55-
exit 0
56-
else
57-
exit 1
58-
fi
25+
exit $FAIL
19.8 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@startuml
2+
3+
participant Service_consumer as SC
4+
5+
participant Service_provider as SP
6+
7+
note across
8+
Service provider is routing manager
9+
endnote
10+
11+
SP -> SC : OFFER
12+
13+
SC -> SP : REQUEST
14+
15+
SP --\ SC : ON_AVAILABLE
16+
17+
SC -> SP : start_method_id request
18+
19+
SC -> SC : check offered services\n(local, remote and all)
20+
21+
SP -> SP : check offered services\n(local, remote and all)
22+
23+
SC -> SP : shutdown_method_id request
24+
25+
SP -> SC : STOP OFFER
26+
27+
@enduml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Offered Services Info Test
2+
3+
This test assures that, given an offer_type, all offered services that match it are retrievable by ```get_offered_services_async``` either by the offering application or the requesting one.
4+
5+
## Purpose
6+
7+
- Assure that only locally offered services are retrieved
8+
- Assure that only remotely offered services are retrieved
9+
- Assure that all offered services are retrieved
10+
11+
## Test Logic
12+
13+
### Service provider
14+
The service provider offers 5 services/instances, 2 locally and 3 remotely. It waits for the start_method_id request from service consumer, after that, it checks the locally offered services, then the remotely offered services and finally all offered services. After checking everything, it waits for a shutdown_method_id request from service consumer to stop.
15+
16+
### Service consumer
17+
The service consumer requests 5 services/instances after which waits for their availability. When everything is available, it sends a start_method_id after which it gets the locally offered services, then the remotely offered services and finally all offered services. It checks all number of services retrieved comparing them to the expected number. After everything it sends a shutdown_method_id request to service provider to trigger shutdown and stops.
18+
19+
![Diagram](docs/offered_services_info_test.png)

test/network_tests/offered_services_info_tests/offered_services_info_test_client.cpp

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2014-2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
1+
// Copyright (C) 2014-2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
22
// This Source Code Form is subject to the terms of the Mozilla Public
33
// License, v. 2.0. If a copy of the MPL was not distributed with this
44
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -42,8 +42,9 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
4242
vsomeip_utilities::base_logger("OFIC", "OFFERED SERVICES INFO TEST CLIENT"), service_info_(_service_info),
4343
remote_service_info_(_remote_service_info), operation_mode_(_mode),
4444
app_(vsomeip::runtime::get()->create_application("offered_services_info_test_client")), wait_until_registered_(true),
45-
wait_until_service_available_(true), wait_for_stop_(true), last_received_response_(std::chrono::steady_clock::now()),
46-
number_received_responses_(0), stop_thread_(std::bind(&offered_services_info_test_client::wait_for_stop, this)),
45+
wait_until_service_available_(true), services_available(0), wait_for_stop_(true),
46+
last_received_response_(std::chrono::steady_clock::now()),
47+
stop_thread_(std::bind(&offered_services_info_test_client::wait_for_stop, this)),
4748
test_offered_services_thread_(std::bind(&offered_services_info_test_client::test_offered_services, this)) {
4849
if (!app_->init()) {
4950
ADD_FAILURE() << "Couldn't initialize application";
@@ -72,9 +73,6 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
7273

7374
app_->register_state_handler(std::bind(&offered_services_info_test_client::on_state, this, std::placeholders::_1));
7475

75-
app_->register_message_handler(vsomeip::ANY_SERVICE, vsomeip::ANY_INSTANCE, vsomeip::ANY_METHOD,
76-
std::bind(&offered_services_info_test_client::on_message, this, std::placeholders::_1));
77-
7876
app_->register_availability_handler(vsomeip::ANY_SERVICE, vsomeip::ANY_INSTANCE,
7977
std::bind(&offered_services_info_test_client::on_availability, this, std::placeholders::_1,
8078
std::placeholders::_2, std::placeholders::_3));
@@ -109,11 +107,17 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
109107
void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) {
110108
VSOMEIP_INFO << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << _instance << "] is "
111109
<< (_is_available ? "available" : "not available") << ".";
112-
static int services_available = 0;
113110
std::lock_guard<std::mutex> its_lock(mutex_);
114111
if (_is_available) {
115112
services_available++;
116-
if (services_available == 5) {
113+
if (services_available == offer_test::num_all_offered_services) {
114+
115+
std::shared_ptr<vsomeip::message> its_req = vsomeip::runtime::get()->create_request();
116+
its_req->set_service(service_info_.service_id);
117+
its_req->set_instance(service_info_.instance_id);
118+
its_req->set_method(service_info_.start_method_id);
119+
app_->send(its_req);
120+
117121
wait_until_service_available_ = false;
118122
condition_.notify_one();
119123
}
@@ -123,41 +127,15 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
123127
}
124128
}
125129

126-
void on_message(const std::shared_ptr<vsomeip::message>& _message) {
127-
if (_message->get_message_type() == vsomeip::message_type_e::MT_RESPONSE) {
128-
on_response(_message);
129-
}
130-
}
131-
132-
void on_response(const std::shared_ptr<vsomeip::message>& _message) {
133-
++number_received_responses_;
134-
static bool first(true);
135-
if (first) {
136-
first = false;
137-
last_received_response_ = std::chrono::steady_clock::now();
138-
return;
139-
}
140-
EXPECT_EQ(service_info_.service_id, _message->get_service());
141-
EXPECT_EQ(service_info_.method_id, _message->get_method());
142-
EXPECT_EQ(service_info_.instance_id, _message->get_instance());
143-
ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_received_response_).count(),
144-
(std::chrono::milliseconds(VSOMEIP_DEFAULT_WATCHDOG_TIMEOUT) + std::chrono::milliseconds(1000)).count());
145-
last_received_response_ = std::chrono::steady_clock::now();
146-
std::cout << ".";
147-
std::cout.flush();
148-
}
149-
150130
void test_offered_services() {
151131
if (operation_mode_ != operation_mode_e::METHODCALL) {
152132
return;
153133
}
154-
std::unique_lock<std::mutex> its_lock(mutex_);
155-
condition_.wait(its_lock, [this] { return !wait_until_registered_; });
156-
condition_.wait(its_lock, [this] { return !wait_until_service_available_; });
157-
its_lock.unlock();
158-
its_lock.release();
159-
160-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
134+
{
135+
std::unique_lock<std::mutex> its_lock(mutex_);
136+
condition_.wait(its_lock, [this] { return !wait_until_registered_; });
137+
condition_.wait(its_lock, [this] { return !wait_until_service_available_; });
138+
}
161139

162140
VSOMEIP_INFO << "TEST LOCAL SERVICES";
163141
app_->get_offered_services_async(
@@ -173,10 +151,8 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
173151
its_req->set_instance(service_info_.instance_id);
174152
its_req->set_method(service_info_.shutdown_method_id);
175153
app_->send(its_req);
176-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
177154
}
178155

179-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
180156
{
181157
std::lock_guard<std::mutex> its_lock(stop_mutex_);
182158
wait_for_stop_ = false;
@@ -273,12 +249,13 @@ class offered_services_info_test_client : public vsomeip_utilities::base_logger
273249
std::mutex mutex_;
274250
std::condition_variable condition_;
275251

252+
int services_available;
253+
276254
bool wait_for_stop_;
277255
std::mutex stop_mutex_;
278256
std::condition_variable stop_condition_;
279257

280258
std::chrono::steady_clock::time_point last_received_response_;
281-
std::atomic<std::uint32_t> number_received_responses_;
282259
std::promise<void> all_callbacks_received_;
283260
std::thread stop_thread_;
284261
std::thread test_offered_services_thread_;

test/network_tests/offered_services_info_tests/offered_services_info_test_globals.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ struct service_info {
1414
vsomeip::method_t method_id;
1515
vsomeip::event_t event_id;
1616
vsomeip::eventgroup_t eventgroup_id;
17+
vsomeip::method_t start_method_id;
1718
vsomeip::method_t shutdown_method_id;
1819
};
1920

2021
uint8_t num_all_offered_services = 5;
2122
uint8_t num_local_offered_services = 2;
2223
uint8_t num_remote_offered_services = 3;
2324

24-
struct service_info service = {0x1111, 0x1, 0x1111, 0x1111, 0x1000, 0x1404};
25-
struct service_info remote_service = {0x2222, 0x2, 0x2222, 0x2222, 0x2000, 0x2808};
25+
struct service_info service = {0x1111, 0x1, 0x1111, 0x1111, 0x1000, 0x0404, 0x1404};
26+
struct service_info remote_service = {0x2222, 0x2, 0x2222, 0x2222, 0x2000, 0x0808, 0x2808};
2627
}
2728

2829
#endif /* OFFER_TEST_GLOBALS_HPP_ */

test/network_tests/offered_services_info_tests/offered_services_info_test_service.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2014-2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
1+
// Copyright (C) 2014-2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
22
// This Source Code Form is subject to the terms of the Mozilla Public
33
// License, v. 2.0. If a copy of the MPL was not distributed with this
44
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -38,13 +38,9 @@ class offer_test_service : public vsomeip_utilities::base_logger {
3838
public:
3939
offer_test_service(struct offer_test::service_info _service_info, struct offer_test::service_info _remote_service_info) :
4040
vsomeip_utilities::base_logger("OTS1", "OFFER TEST SERVICE"), service_info_(_service_info),
41-
remote_service_info_(_remote_service_info),
42-
// service with number 1 uses "routingmanagerd" as application name
43-
// this way the same json file can be reused for all local tests
44-
// including the ones with routingmanagerd
45-
app_(vsomeip::runtime::get()->create_application((service_number == "1") ? "routingmanagerd"
46-
: "offered_services_info_test_service" + service_number)),
47-
wait_until_registered_(true), shutdown_method_called_(false), offer_thread_(std::bind(&offer_test_service::run, this)) {
41+
remote_service_info_(_remote_service_info), app_(vsomeip::runtime::get()->create_application("service-sample")),
42+
wait_until_registered_(true), wait_until_start_(true), wait_until_done_(true), available_services(0),
43+
offer_thread_(std::bind(&offer_test_service::run, this)) {
4844
if (!app_->init()) {
4945
ADD_FAILURE() << "Couldn't initialize application";
5046
return;
@@ -54,6 +50,9 @@ class offer_test_service : public vsomeip_utilities::base_logger {
5450
app_->register_message_handler(service_info_.service_id, service_info_.instance_id, service_info_.method_id,
5551
std::bind(&offer_test_service::on_request, this, std::placeholders::_1));
5652

53+
app_->register_message_handler(service_info_.service_id, service_info_.instance_id, service_info_.start_method_id,
54+
std::bind(&offer_test_service::on_start_method_called, this, std::placeholders::_1));
55+
5756
app_->register_message_handler(service_info_.service_id, service_info_.instance_id, service_info_.shutdown_method_id,
5857
std::bind(&offer_test_service::on_shutdown_method_called, this, std::placeholders::_1));
5958
app_->start();
@@ -105,9 +104,21 @@ class offer_test_service : public vsomeip_utilities::base_logger {
105104

106105
void on_request(const std::shared_ptr<vsomeip::message>& _message) { app_->send(vsomeip::runtime::get()->create_response(_message)); }
107106

107+
void on_start_method_called(const std::shared_ptr<vsomeip::message>& _message) {
108+
(void)_message;
109+
{
110+
std::unique_lock its_lock{mutex_};
111+
wait_until_start_ = false;
112+
condition_.notify_one();
113+
}
114+
}
115+
108116
void on_shutdown_method_called(const std::shared_ptr<vsomeip::message>& _message) {
109117
(void)_message;
110-
shutdown_method_called_ = true;
118+
{
119+
std::unique_lock its_lock{mutex_};
120+
condition_.wait(its_lock, [this] { return !wait_until_done_; });
121+
}
111122

112123
app_->stop_offer_service(service_info_.service_id, service_info_.instance_id);
113124
app_->stop_offer_service(service_info_.service_id, (vsomeip::instance_t)(service_info_.instance_id + 1));
@@ -130,7 +141,8 @@ class offer_test_service : public vsomeip_utilities::base_logger {
130141

131142
VSOMEIP_DEBUG << "[" << std::hex << std::setfill('0') << std::setw(4) << service_info_.service_id << "] Offering";
132143
offer();
133-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
144+
145+
condition_.wait(its_lock, [this] { return !wait_until_start_; });
134146

135147
VSOMEIP_INFO << "TEST LOCAL SERVICES";
136148
app_->get_offered_services_async(vsomeip::offer_type_e::OT_LOCAL,
@@ -139,10 +151,6 @@ class offer_test_service : public vsomeip_utilities::base_logger {
139151
if (std::future_status::timeout == all_callbacks_received_.get_future().wait_for(std::chrono::seconds(15))) {
140152
ADD_FAILURE() << "Didn't receive all callbacks within time";
141153
}
142-
143-
while (!shutdown_method_called_) {
144-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
145-
}
146154
}
147155

148156
void on_offered_services_local(const std::vector<std::pair<vsomeip::service_t, vsomeip::instance_t>>& _services) {
@@ -223,17 +231,25 @@ class offer_test_service : public vsomeip_utilities::base_logger {
223231
EXPECT_EQ(offer_test::num_all_offered_services, i);
224232
std::cout << "ON OFFERED SERVICES ALL CALLBACK END" << std::endl;
225233
all_callbacks_received_.set_value();
234+
235+
{
236+
std::scoped_lock its_lock{mutex_};
237+
wait_until_done_ = false;
238+
condition_.notify_one();
239+
}
226240
}
227241

228242
private:
229243
struct offer_test::service_info service_info_;
230244
struct offer_test::service_info remote_service_info_;
231245
std::shared_ptr<vsomeip::application> app_;
232246

233-
bool wait_until_registered_;
247+
std::atomic_bool wait_until_registered_;
248+
std::atomic_bool wait_until_start_;
249+
std::atomic_bool wait_until_done_;
250+
int available_services;
234251
std::mutex mutex_;
235252
std::condition_variable condition_;
236-
std::atomic<bool> shutdown_method_called_;
237253
std::promise<void> all_callbacks_received_;
238254
std::thread offer_thread_;
239255
};

0 commit comments

Comments
 (0)