diff options
Diffstat (limited to 'test/initial_event_tests/initial_event_test_client.cpp')
-rw-r--r-- | test/initial_event_tests/initial_event_test_client.cpp | 180 |
1 files changed, 146 insertions, 34 deletions
diff --git a/test/initial_event_tests/initial_event_test_client.cpp b/test/initial_event_tests/initial_event_test_client.cpp index 2f49516..8f843c6 100644 --- a/test/initial_event_tests/initial_event_test_client.cpp +++ b/test/initial_event_tests/initial_event_test_client.cpp @@ -14,17 +14,27 @@ #include <gtest/gtest.h> +#ifndef _WIN32 +#include <signal.h> +#endif + #include <vsomeip/vsomeip.hpp> #include "../../implementation/logging/include/logger.hpp" #include "initial_event_test_globals.hpp" +class initial_event_test_client; +static initial_event_test_client* the_client; +extern "C" void signal_handler(int _signum); class initial_event_test_client { public: initial_event_test_client(int _client_number, vsomeip::subscription_type_e _subscription_type, - std::array<initial_event_test::service_info, 7> _service_infos) : + std::array<initial_event_test::service_info, 7> _service_infos, + bool _subscribe_on_available, std::uint32_t _events_to_subscribe, + bool _initial_event_strict_checking, + bool _dont_exit) : client_number_(_client_number), service_infos_(_service_infos), subscription_type_(_subscription_type), @@ -32,11 +42,27 @@ public: wait_until_registered_(true), wait_until_other_services_available_(true), wait_for_stop_(true), + subscribe_on_available_(_subscribe_on_available), + events_to_subscribe_(_events_to_subscribe), + initial_event_strict_checking_(_initial_event_strict_checking), + dont_exit_(_dont_exit), stop_thread_(std::bind(&initial_event_test_client::wait_for_stop, this)) { if (!app_->init()) { ADD_FAILURE() << "Couldn't initialize application"; return; } + + // register signal handler + the_client = this; + struct sigaction sa_new, sa_old; + sa_new.sa_handler = signal_handler; + sa_new.sa_flags = 0; + sigemptyset(&sa_new.sa_mask); + ::sigaction(SIGUSR1, &sa_new, &sa_old); + ::sigaction(SIGINT, &sa_new, &sa_old); + ::sigaction(SIGTERM, &sa_new, &sa_old); + ::sigaction(SIGABRT, &sa_new, &sa_old); + app_->register_state_handler( std::bind(&initial_event_test_client::on_state, this, std::placeholders::_1)); @@ -59,13 +85,26 @@ public: std::set<vsomeip::eventgroup_t> its_eventgroups; its_eventgroups.insert(i.eventgroup_id); - app_->request_event(i.service_id, i.instance_id, i.event_id, its_eventgroups, true); + for (std::uint32_t j = 0; j < events_to_subscribe_; j++ ) { + app_->request_event(i.service_id, i.instance_id, + static_cast<vsomeip::event_t>(i.event_id + j), + its_eventgroups, true); + } other_services_available_[std::make_pair(i.service_id, i.instance_id)] = false; other_services_received_notification_[std::make_pair(i.service_id, i.method_id)] = 0; - - app_->subscribe(i.service_id, i.instance_id, i.eventgroup_id, - vsomeip::DEFAULT_MAJOR, subscription_type_); + if (!subscribe_on_available_) { + if (events_to_subscribe_ == 1 ) { + app_->subscribe(i.service_id, i.instance_id, i.eventgroup_id, + vsomeip::DEFAULT_MAJOR, subscription_type_); + } else if (events_to_subscribe_ > 1) { + for (std::uint32_t j = 0; j < events_to_subscribe_; j++ ) { + app_->subscribe(i.service_id, i.instance_id, i.eventgroup_id, + vsomeip::DEFAULT_MAJOR, subscription_type_, + static_cast<vsomeip::event_t>(i.event_id + j)); + } + } + } } app_->start(); @@ -109,6 +148,23 @@ public: return v.second;})) { VSOMEIP_INFO << "[" << std::setw(4) << std::setfill('0') << std::hex << client_number_ << "] all services are available."; + if (subscribe_on_available_) { + for(const auto& i : service_infos_) { + if (i.service_id == 0xFFFF && i.instance_id == 0xFFFF) { + continue; + } + if (events_to_subscribe_ == 1 ) { + app_->subscribe(i.service_id, i.instance_id, i.eventgroup_id, + vsomeip::DEFAULT_MAJOR, subscription_type_); + } else if (events_to_subscribe_ > 1) { + for (std::uint32_t j = 0; j < events_to_subscribe_; j++ ) { + app_->subscribe(i.service_id, i.instance_id, i.eventgroup_id, + vsomeip::DEFAULT_MAJOR, subscription_type_, + static_cast<vsomeip::event_t>(i.event_id + j)); + } + } + } + } } } } @@ -141,18 +197,14 @@ public: case vsomeip::subscription_type_e::SU_RELIABLE: case vsomeip::subscription_type_e::SU_PREFER_UNRELIABLE: case vsomeip::subscription_type_e::SU_PREFER_RELIABLE: - if (all_notifications_received()) { - notify = true; - } - break; case vsomeip::subscription_type_e::SU_RELIABLE_AND_UNRELIABLE: - if (all_notifications_received_tcp_and_udp()) { + if (all_notifications_received()) { notify = true; } break; } - if(notify) { + if(notify && !dont_exit_) { std::lock_guard<std::mutex> its_lock(stop_mutex_); wait_for_stop_ = false; stop_condition_.notify_one(); @@ -169,17 +221,26 @@ public: { if (v.second == initial_event_test::notifications_to_send) { return true; - } else if (v.second >= initial_event_test::notifications_to_send){ - VSOMEIP_WARNING - << " Received multiple initial events from service/instance: " - << std::setw(4) << std::setfill('0') << std::hex << v.first.first - << "." - << std::setw(4) << std::setfill('0') << std::hex << v.first.second - << " number of received events: " << v.second - << ". This is caused by StopSubscribe/Subscribe messages."; - return true; } else { - return false; + if (initial_event_strict_checking_) { + return false; + } else { + if (v.second >= initial_event_test::notifications_to_send) { + VSOMEIP_WARNING + << "[" << std::setw(4) << std::setfill('0') << std::hex + << client_number_ << "] " + << " Received multiple initial events from service/instance: " + << std::setw(4) << std::setfill('0') << std::hex << v.first.first + << "." + << std::setw(4) << std::setfill('0') << std::hex << v.first.second + << " number of received events: " << v.second + << ". This is caused by StopSubscribe/Subscribe messages and/or" + << " service offered via UDP and TCP"; + return true; + } else { + return false; + } + } } } ); @@ -189,14 +250,17 @@ public: std::uint32_t received_twice(0); std::uint32_t received_normal(0); for(const auto &v : other_services_received_notification_) { - if (v.second > initial_event_test::notifications_to_send * 2) { + if (!initial_event_strict_checking_ && + v.second > initial_event_test::notifications_to_send * 2) { VSOMEIP_WARNING + << "[" << std::setw(4) << std::setfill('0') << std::hex + << client_number_ << "] " << " Received multiple initial events from service/instance: " << std::setw(4) << std::setfill('0') << std::hex << v.first.first << "." << std::setw(4) << std::setfill('0') << std::hex << v.first.second - << " number of received events: " << v.second - << ". This is caused by StopSubscribe/Subscribe messages."; + << ". This is caused by StopSubscribe/Subscribe messages and/or" + << " service offered via UDP and TCP"; received_twice++; } else if (v.second == initial_event_test::notifications_to_send * 2) { received_twice++; @@ -205,8 +269,8 @@ public: } } - if( received_twice == (service_infos_.size() - 1) / 2 - && received_normal == (service_infos_.size() - 1) / 2) { + if( received_twice == ((service_infos_.size() - 1) * events_to_subscribe_)/ 2 + && received_normal == ((service_infos_.size() - 1) * events_to_subscribe_)/ 2) { // routing manager stub receives the notification // - twice from external nodes // - and normal from all internal nodes @@ -220,6 +284,15 @@ public: return false; } + void handle_signal(int _signum) { + VSOMEIP_DEBUG << "[" << std::setw(4) << std::setfill('0') << std::hex + << client_number_ << "] Catched signal, going down (" + << std::dec <<_signum << ")"; + std::lock_guard<std::mutex> its_lock(stop_mutex_); + wait_for_stop_ = false; + stop_condition_.notify_one(); + } + void wait_for_stop() { { std::unique_lock<std::mutex> its_lock(stop_mutex_); @@ -254,6 +327,12 @@ private: std::condition_variable condition_; bool wait_for_stop_; + + bool subscribe_on_available_; + std::uint32_t events_to_subscribe_; + bool initial_event_strict_checking_; + bool dont_exit_; + std::mutex stop_mutex_; std::condition_variable stop_condition_; std::thread stop_thread_; @@ -262,16 +341,26 @@ private: static int client_number; static vsomeip::subscription_type_e subscription_type; static bool use_same_service_id; +static bool subscribe_on_available; +static std::uint32_t subscribe_multiple_events; +static bool initial_event_strict_checking; +static bool dont_exit; + +extern "C" void signal_handler(int signum) { + the_client->handle_signal(signum); +} TEST(someip_initial_event_test, wait_for_initial_events_of_all_services) { if(use_same_service_id) { initial_event_test_client its_sample(client_number, subscription_type, - initial_event_test::service_infos_same_service_id); + initial_event_test::service_infos_same_service_id, subscribe_on_available, + subscribe_multiple_events, initial_event_strict_checking, dont_exit); } else { initial_event_test_client its_sample(client_number, subscription_type, - initial_event_test::service_infos); + initial_event_test::service_infos, subscribe_on_available, + subscribe_multiple_events, initial_event_strict_checking, dont_exit); } } @@ -280,11 +369,16 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); if(argc < 3) { - std::cerr << "Please specify a client number and subscription type, like: " << argv[0] << " 2 UDP SAME_SERVICE_ID" << std::endl; + std::cerr << "Please specify a client number and subscription type, like: " << argv[0] << " 2 UDP SUBSCRIBE_BEFORE_START SAME_SERVICE_ID" << std::endl; std::cerr << "Valid client numbers are from 0 to 0xFFFF" << std::endl; std::cerr << "Valid subscription types include:" << std::endl; std::cerr << "[TCP_AND_UDP, PREFER_UDP, PREFER_TCP, UDP, TCP]" << std::endl; - std::cerr << "If SAME_SERVICE_ID is specified as third parameter the test is run w/ multiple instances of the same service" << std::endl; + std::cerr << "After client number and subscription types one/multiple of these flags can be specified:"; + std::cerr << " - Time of subscription, valid values: [SUBSCRIBE_ON_AVAILABILITY, SUBSCRIBE_BEFORE_START], default SUBSCRIBE_BEFORE_START" << std::endl; + std::cerr << " - SAME_SERVICE_ID flag. If set the test is run w/ multiple instances of the same service, default false" << std::endl; + std::cerr << " - MULTIPLE_EVENTS flag. If set the test will subscribe to multiple events in the eventgroup, default false" << std::endl; + std::cerr << " - STRICT_CHECKING flag. If set the test will only successfully finish if exactly the number of initial events were received (and not more). Default false" << std::endl; + std::cerr << " - DONT_EXIT flag. If set the test will not exit if all notifications have been received. Default false" << std::endl; return 1; } @@ -307,11 +401,29 @@ int main(int argc, char** argv) return 1; } - if (argc >= 4 && std::string("SAME_SERVICE_ID") == std::string(argv[3])) { - use_same_service_id = true; - } else { - use_same_service_id = false; + subscribe_on_available = false; + initial_event_strict_checking = false; + use_same_service_id = false; + subscribe_multiple_events = 1; + dont_exit = false; + if (argc > 3) { + for (int i = 3; i < argc; i++) { + if (std::string("SUBSCRIBE_ON_AVAILABILITY") == std::string(argv[i])) { + subscribe_on_available = true; + } else if (std::string("SUBSCRIBE_BEFORE_START") == std::string(argv[i])) { + subscribe_on_available = false; + } else if (std::string("SAME_SERVICE_ID") == std::string(argv[i])) { + use_same_service_id = true; + } else if (std::string("MULTIPLE_EVENTS") == std::string(argv[i])) { + subscribe_multiple_events = 5; + } else if (std::string("STRICT_CHECKING") == std::string(argv[i])) { + initial_event_strict_checking = true; + } else if (std::string("DONT_EXIT") == std::string(argv[i])) { + dont_exit = true; + } + } } + return RUN_ALL_TESTS(); } #endif |