Skip to content

Commit

Permalink
Add precondition to offer_stop_offer
Browse files Browse the repository at this point in the history
Added precondition to the offer_stop_offer test to wait for registration of the service application.
If the application was not registered, it could lead to the test getting stuck waiting for the service
availabilities.
  • Loading branch information
Rui Graça authored and Duarte Fonseca committed Jan 9, 2025
1 parent c4f3707 commit 40122f7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std::future<bool> client_t::request(bool is_tcp, vsomeip::service_t service,
auto promise_response = std::promise<bool>();
auto future_response = std::future<bool>(promise_response.get_future());

std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);
if (availability_table[service]) {
auto request = vsomeip::runtime::get()->create_request(is_tcp);

Expand All @@ -84,7 +84,7 @@ std::future<bool> client_t::request(bool is_tcp, vsomeip::service_t service,
}

bool client_t::is_available() {
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

for (const auto& availability_entry : availability_table) {
if (!availability_entry.second) {
Expand All @@ -95,7 +95,7 @@ bool client_t::is_available() {
}

void client_t::on_message(const std::shared_ptr<vsomeip::message>& message) {
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

if (message->get_payload()->get_data()) {
VSOMEIP_INFO << "client_t::" << __func__ << ": "
Expand All @@ -120,7 +120,7 @@ void client_t::on_message(const std::shared_ptr<vsomeip::message>& message) {

void client_t::on_availability(vsomeip::service_t service, vsomeip::instance_t instance,
bool is_available) {
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

VSOMEIP_INFO << "client_t::" << __func__ << " Service [" << std::setw(4) << std::setfill('0')
<< std::hex << service << "." << instance << "] is "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class client_t : public vsomeip_utilities::base_logger {
/// @param method Method to send the request to.
///
/// @return returns a future that will notify that responde to this requests was received
std::future<bool> request(bool is_tcp, vsomeip::service_t service,
vsomeip::instance_t instance,
vsomeip::method_t method);
std::future<bool> request(bool is_tcp, vsomeip::service_t service, vsomeip::instance_t instance,
vsomeip::method_t method);

/// @brief Check if both services are available
///
Expand Down Expand Up @@ -98,7 +97,6 @@ class client_t : public vsomeip_utilities::base_logger {
/// @brief List to hold the current client_request_t awaiting responses.
/// client_request_t are removed after the response is received and promise is set.
std::list<client_request_t> pending_requests;

};

#endif // VSOMEIP_CLIENT_HPP
45 changes: 31 additions & 14 deletions test/network_tests/offer_stop_offer_test/applications/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ constexpr std::size_t PAYLOAD_SIZE = 1000UL;
service_t::service_t() :
vsomeip_utilities::base_logger("SRV", "VSOMEIP SERVICE PROVIDER"),
vsomeip_app(vsomeip::runtime::get()->create_application("service-sample")),
payload(std::vector<uint8_t>(PAYLOAD_SIZE, 0)) {
payload(std::vector<uint8_t>(PAYLOAD_SIZE, 0)),
app_registration_state(vsomeip::state_type_e::ST_DEREGISTERED) {

availability_table[SERVICE_ID] = false;
availability_table[OTHER_SERVICE_ID] = false;
Expand All @@ -37,6 +38,9 @@ bool service_t::init() {
OTHER_SERVICE_ID, OTHER_INSTANCE_ID, OTHER_METHOD_ID,
std::bind(&service_t::on_message, this, std::placeholders::_1));

vsomeip_app->register_state_handler(
std::bind(&service_t::on_state, this, std::placeholders::_1));

vsomeip_app->register_availability_handler(
SERVICE_ID, INSTANCE_ID,
std::bind(&service_t::on_availability, this, std::placeholders::_1,
Expand Down Expand Up @@ -71,37 +75,36 @@ void service_t::stop() {
std::future<bool> service_t::offer() {
// Lock the entire function to not allow the on_availability callback to interfier while the
// offer is being created
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

promise_availability = std::promise<bool>();
auto future_availability = std::future<bool>(promise_availability.get_future());
is_offering = true;

VSOMEIP_INFO << "service_t::" << __func__;
vsomeip_app->offer_service(SERVICE_ID, INSTANCE_ID, 0, 0);
vsomeip_app->offer_service(OTHER_SERVICE_ID, OTHER_INSTANCE_ID, 0, 0);

promise_availability = std::promise<bool>();
auto future_availability = std::future<bool>(promise_availability.get_future());

is_offering = true;
return future_availability;
}

std::future<bool> service_t::stop_offer() {
// Lock the entire function to not allow the on_availability callback to interfier while the
// offer is being created
std::lock_guard<std::mutex> lk(availability_mutex);

VSOMEIP_INFO << "service_t::" << __func__;
vsomeip_app->stop_offer_service(SERVICE_ID, INSTANCE_ID, 0, 0);
vsomeip_app->stop_offer_service(OTHER_SERVICE_ID, OTHER_INSTANCE_ID, 0, 0);
std::scoped_lock lk(availability_mutex);

promise_availability = std::promise<bool>();
auto future_availability = std::future<bool>(promise_availability.get_future());

is_offering = false;

VSOMEIP_INFO << "service_t::" << __func__;
vsomeip_app->stop_offer_service(SERVICE_ID, INSTANCE_ID, 0, 0);
vsomeip_app->stop_offer_service(OTHER_SERVICE_ID, OTHER_INSTANCE_ID, 0, 0);
return future_availability;
}

void service_t::on_message(const std::shared_ptr<vsomeip::message>& message) {
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

payload.at(0)++;
VSOMEIP_INFO << "service_t::" << __func__ << ": [" << std::hex << message->get_service() << "] "
Expand All @@ -116,7 +119,7 @@ void service_t::on_message(const std::shared_ptr<vsomeip::message>& message) {

void service_t::on_availability(vsomeip::service_t service, vsomeip::instance_t instance,
bool is_available) {
std::lock_guard<std::mutex> lk(availability_mutex);
std::scoped_lock lk(availability_mutex);

VSOMEIP_INFO << "service_t::" << __func__ << " Service [" << std::setw(4) << std::setfill('0')
<< std::hex << service << "." << instance << "] is "
Expand All @@ -136,3 +139,17 @@ void service_t::on_availability(vsomeip::service_t service, vsomeip::instance_t
promise_availability.set_value(is_offering);
}
}

void service_t::on_state(vsomeip::state_type_e state) {
app_registration_state = state;
if (!is_registered()) {
std::scoped_lock lk(availability_mutex);
// we are no longer register -> unlock the promise
is_offering = false;
promise_availability.set_value(is_offering);
}
}

bool service_t::is_registered() const {
return app_registration_state == vsomeip::state_type_e::ST_REGISTERED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class service_t : public vsomeip_utilities::base_logger {
/// @return returns a future that will notify that the availability to this service was changed
std::future<bool> stop_offer();

/// @brief Getter for application registration state
///
/// @return true if registered
bool is_registered() const;

private:
/// @brief handler for receiving requests. Will send a response back with a big payload and a
/// changing first byte
Expand All @@ -65,6 +70,11 @@ class service_t : public vsomeip_utilities::base_logger {
void on_availability(vsomeip::service_t service, vsomeip::instance_t instance,
bool is_available);

/// @brief handler for application registration state change.
///
/// @param state Current registration state
void on_state(vsomeip::state_type_e state);

/// @brief vsomeip app interface
std::shared_ptr<vsomeip::application> vsomeip_app;

Expand All @@ -80,10 +90,12 @@ class service_t : public vsomeip_utilities::base_logger {
std::thread worker;

/// @brief application offer state for both services.
bool is_offering;
std::atomic_bool is_offering;

/// @brief promise which value shall be set once the availability is received
std::promise<bool> promise_availability;

std::atomic<vsomeip::state_type_e> app_registration_state;
};

#endif // VSOMEIP_SERVICE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ constexpr vsomeip::event_t EVENT_ID = 0x8778;
/// @brief Both services eventgroup id
constexpr vsomeip::eventgroup_t EVENTGROUP_ID = 0x4465;


#endif // VSOMEIP_EXAMPLES_IDS_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ TEST(test_offer_stop_offer, test_offer_stop_offer_service) {
ASSERT_TRUE(service_provider.init());
service_provider.start();

// Precondition 2: routingmanagerd is able to route
// Precondition 2: Wait for the routing host to be available and for the service app to register
while (!service_provider.is_registered()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

// Precondition 3: routingmanagerd is able to route
auto routing_availability_check = service_provider.offer();
ASSERT_TRUE(routing_availability_check.valid());
routing_availability_check.wait();
Expand All @@ -41,7 +46,10 @@ TEST(test_offer_stop_offer, test_offer_stop_offer_service) {
// 4: validate that the services are available
// Repeate above steps for SERVICE_UP_TIME
while (!test_timer.has_elapsed()) {
// Check if we are still registered to the routing host
ASSERT_TRUE(service_provider.is_registered());

// Send STOP OFFER
auto stop_offer_confirmation = service_provider.stop_offer();
// Wait confirmation that all services have became unavailable
ASSERT_TRUE(stop_offer_confirmation.valid());
Expand All @@ -50,6 +58,7 @@ TEST(test_offer_stop_offer, test_offer_stop_offer_service) {

std::this_thread::sleep_for(SERVICE_STOP_OFFER_TIME);

// Send OFFER
auto offer_confirmation = service_provider.offer();
// Wait confirmation that all services have became available
ASSERT_TRUE(offer_confirmation.valid());
Expand Down

0 comments on commit 40122f7

Please sign in to comment.