• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_client.hpp"
7 
routing_restart_test_client()8 routing_restart_test_client::routing_restart_test_client()
9     : app_(vsomeip::runtime::get()->create_application()),
10       is_available_(false),
11       sender_(std::bind(&routing_restart_test_client::run, this)),
12       received_responses_(0) {
13 
14 }
15 
init()16 bool routing_restart_test_client::init() {
17     if (!app_->init()) {
18         ADD_FAILURE() << "Couldn't initialize application";
19         return false;
20     }
21 
22     app_->register_state_handler(
23             std::bind(&routing_restart_test_client::on_state, this,
24                     std::placeholders::_1));
25 
26     app_->register_message_handler(vsomeip::ANY_SERVICE,
27             vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip::ANY_METHOD,
28             std::bind(&routing_restart_test_client::on_message, this,
29                     std::placeholders::_1));
30 
31     app_->register_availability_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
32             vsomeip_test::TEST_SERVICE_INSTANCE_ID,
33             std::bind(&routing_restart_test_client::on_availability, this,
34                     std::placeholders::_1, std::placeholders::_2,
35                     std::placeholders::_3));
36     return true;
37 }
38 
start()39 void routing_restart_test_client::start() {
40     VSOMEIP_INFO << "Starting...";
41 
42     app_->start();
43 }
44 
stop()45 void routing_restart_test_client::stop() {
46     VSOMEIP_INFO << "Stopping...";
47 
48     shutdown_service();
49 
50     std::this_thread::sleep_for(std::chrono::milliseconds(100));
51 
52     app_->clear_all_handler();
53     app_->stop();
54 }
55 
on_state(vsomeip::state_type_e _state)56 void routing_restart_test_client::on_state(vsomeip::state_type_e _state) {
57     if(_state == vsomeip::state_type_e::ST_REGISTERED) {
58         app_->request_service(vsomeip_test::TEST_SERVICE_SERVICE_ID,
59                 vsomeip_test::TEST_SERVICE_INSTANCE_ID, false);
60     }
61 }
62 
on_availability(vsomeip::service_t _service,vsomeip::instance_t _instance,bool _is_available)63 void routing_restart_test_client::on_availability(vsomeip::service_t _service,
64         vsomeip::instance_t _instance, bool _is_available) {
65 
66     VSOMEIP_INFO << std::hex << "Client 0x" << app_->get_client()
67             << " : Service [" << std::setw(4) << std::setfill('0') << std::hex
68             << _service << "." << _instance << "] is "
69             << (_is_available ? "available." : "NOT available.");
70 
71     if(vsomeip_test::TEST_SERVICE_SERVICE_ID == _service
72             && vsomeip_test::TEST_SERVICE_INSTANCE_ID == _instance) {
73         std::unique_lock<std::mutex> its_lock(mutex_);
74         if(is_available_ && !_is_available) {
75             is_available_ = false;
76         }
77         else if(_is_available && !is_available_) {
78             is_available_ = true;
79             condition_.notify_one();
80         }
81     }
82 }
83 
on_message(const std::shared_ptr<vsomeip::message> & _response)84 void routing_restart_test_client::on_message(const std::shared_ptr<vsomeip::message> &_response) {
85     VSOMEIP_INFO << "Received a response from Service ["
86                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_service()
87                  << "."
88                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_instance()
89                  << "] to Client/Session ["
90                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_client()
91                  << "/"
92                  << std::setw(4) << std::setfill('0') << std::hex << _response->get_session()
93                  << "]";
94 
95     if (_response->get_service() == vsomeip_test::TEST_SERVICE_SERVICE_ID &&
96             _response->get_instance()  == vsomeip_test::TEST_SERVICE_INSTANCE_ID) {
97         received_responses_++;
98         if (received_responses_ == vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_ROUTING_RESTART_TESTS) {
99             VSOMEIP_WARNING << std::hex << app_->get_client()
100                     << ": Received all messages ~> going down!";
101             all_responses_received_.set_value();
102         }
103     }
104 }
105 
run()106 void routing_restart_test_client::run() {
107     for (uint32_t i = 0; i < vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_ROUTING_RESTART_TESTS; ++i) {
108         {
109             std::unique_lock<std::mutex> its_lock(mutex_);
110             while (!is_available_)
111             {
112                 condition_.wait(its_lock);
113             }
114         }
115 
116         auto request = vsomeip::runtime::get()->create_request(false);
117         request->set_service(vsomeip_test::TEST_SERVICE_SERVICE_ID);
118         request->set_instance(vsomeip_test::TEST_SERVICE_INSTANCE_ID);
119         request->set_method(vsomeip_test::TEST_SERVICE_METHOD_ID);
120         app_->send(request);
121 
122         std::this_thread::sleep_for(std::chrono::milliseconds(250));
123     }
124 
125     if (std::future_status::ready == all_responses_received_.get_future().wait_for(std::chrono::milliseconds(10000))) {
126         EXPECT_EQ(vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND_ROUTING_RESTART_TESTS,
127                 received_responses_);
128         VSOMEIP_WARNING << "Received all answers";
129     } else {
130         ADD_FAILURE() << "Didn't receive all responses within time";
131     }
132 
133     stop();
134 }
135 
join_sender_thread()136 void routing_restart_test_client::join_sender_thread()
137 {
138     if (sender_.joinable()) {
139         sender_.join();
140     }
141 }
142 
shutdown_service()143 void routing_restart_test_client::shutdown_service() {
144     auto request = vsomeip::runtime::get()->create_request(false);
145     request->set_service(vsomeip_test::TEST_SERVICE_SERVICE_ID);
146     request->set_instance(vsomeip_test::TEST_SERVICE_INSTANCE_ID);
147     request->set_method(vsomeip_test::TEST_SERVICE_METHOD_ID_SHUTDOWN);
148     app_->send(request);
149 }
150 
TEST(someip_restart_routing_test,request_response_over_restart)151 TEST(someip_restart_routing_test, request_response_over_restart)
152 {
153     routing_restart_test_client test_client;
154     if (test_client.init()) {
155         test_client.start();
156         test_client.join_sender_thread();
157     }
158 }
159 
main(int argc,char ** argv)160 int main(int argc, char** argv) {
161     ::testing::InitGoogleTest(&argc, argv);
162     return RUN_ALL_TESTS();
163 }
164