1 // Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6 #include "restart_routing_test_service.hpp"
7
routing_restart_test_service()8 routing_restart_test_service::routing_restart_test_service() :
9 app_(vsomeip::runtime::get()->create_application()),
10 is_registered_(false),
11 blocked_(false),
12 number_of_received_messages_(0),
13 offer_thread_(std::bind(&routing_restart_test_service::run, this)) {
14 }
15
init()16 bool routing_restart_test_service::init() {
17 std::lock_guard<std::mutex> its_lock(mutex_);
18
19 if (!app_->init()) {
20 ADD_FAILURE() << "Couldn't initialize application";
21 return false;
22 }
23 app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
24 vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip_test::TEST_SERVICE_METHOD_ID,
25 std::bind(&routing_restart_test_service::on_message, this,
26 std::placeholders::_1));
27
28 app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
29 vsomeip_test::TEST_SERVICE_INSTANCE_ID,
30 vsomeip_test::TEST_SERVICE_METHOD_ID_SHUTDOWN,
31 std::bind(&routing_restart_test_service::on_message_shutdown, this,
32 std::placeholders::_1));
33
34 app_->register_state_handler(
35 std::bind(&routing_restart_test_service::on_state, this,
36 std::placeholders::_1));
37 return true;
38 }
39
start()40 void routing_restart_test_service::start() {
41 VSOMEIP_INFO << "Starting...";
42 app_->start();
43 }
44
stop()45 void routing_restart_test_service::stop() {
46 VSOMEIP_INFO << "Stopping...";
47 app_->clear_all_handler();
48 app_->stop();
49 }
50
join_offer_thread()51 void routing_restart_test_service::join_offer_thread() {
52 if (offer_thread_.joinable()) {
53 offer_thread_.join();
54 }
55 }
56
offer()57 void routing_restart_test_service::offer() {
58 app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
59 }
60
stop_offer()61 void routing_restart_test_service::stop_offer() {
62 app_->stop_offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
63 }
64
on_state(vsomeip::state_type_e _state)65 void routing_restart_test_service::on_state(vsomeip::state_type_e _state) {
66 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
67 << (_state == vsomeip::state_type_e::ST_REGISTERED ? "registered." :
68 "deregistered.");
69
70 if(_state == vsomeip::state_type_e::ST_REGISTERED) {
71 if(!is_registered_) {
72 is_registered_ = true;
73 std::lock_guard<std::mutex> its_lock(mutex_);
74 blocked_ = true;
75 // "start" the run method thread
76 condition_.notify_one();
77 }
78 }
79 else {
80 is_registered_ = false;
81 }
82 }
83
on_message(const std::shared_ptr<vsomeip::message> & _request)84 void routing_restart_test_service::on_message(const std::shared_ptr<vsomeip::message>& _request) {
85 ASSERT_EQ(vsomeip_test::TEST_SERVICE_SERVICE_ID, _request->get_service());
86 ASSERT_EQ(vsomeip_test::TEST_SERVICE_METHOD_ID, _request->get_method());
87
88 VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
89 << std::setfill('0') << std::hex << _request->get_client() << "/"
90 << std::setw(4) << std::setfill('0') << std::hex
91 << _request->get_session() << "]";
92
93 // send response
94 std::shared_ptr<vsomeip::message> its_response =
95 vsomeip::runtime::get()->create_response(_request);
96
97 app_->send(its_response);
98
99 number_of_received_messages_++;
100 if(number_of_received_messages_ == vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_ROUTING_RESTART_TESTS) {
101 VSOMEIP_INFO << "Received all messages!";
102 }
103 }
104
on_message_shutdown(const std::shared_ptr<vsomeip::message> & _request)105 void routing_restart_test_service::on_message_shutdown(
106 const std::shared_ptr<vsomeip::message>& _request) {
107 (void)_request;
108 VSOMEIP_INFO << "Shutdown method was called, going down now.";
109 stop();
110 }
111
run()112 void routing_restart_test_service::run() {
113 std::unique_lock<std::mutex> its_lock(mutex_);
114 while (!blocked_)
115 condition_.wait(its_lock);
116
117 offer();
118 }
119
TEST(someip_restart_routing_test,send_response_for_every_request)120 TEST(someip_restart_routing_test, send_response_for_every_request) {
121 routing_restart_test_service test_service;
122 if (test_service.init()) {
123 test_service.start();
124 test_service.join_offer_thread();
125 }
126 }
127
128 #ifndef _WIN32
main(int argc,char ** argv)129 int main(int argc, char** argv) {
130 ::testing::InitGoogleTest(&argc, argv);
131 return RUN_ALL_TESTS();
132 }
133 #endif
134