• 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 "big_payload_test_service.hpp"
7 
8 #include "big_payload_test_globals.hpp"
9 
big_payload_test_service(big_payload_test::test_mode _test_mode)10 big_payload_test_service::big_payload_test_service(big_payload_test::test_mode _test_mode) :
11                 app_(vsomeip::runtime::get()->create_application("big_payload_test_service")),
12                 is_registered_(false),
13                 blocked_(false),
14                 test_mode_(_test_mode),
15                 number_of_received_messages_(0),
16                 offer_thread_(std::bind(&big_payload_test_service::run, this))
17 {
18     switch (test_mode_) {
19         case big_payload_test::test_mode::RANDOM:
20             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES_RANDOM;
21             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_RANDOM;
22             break;
23         case big_payload_test::test_mode::LIMITED:
24             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES / 2;
25             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_LIMITED;
26             break;
27         case big_payload_test::test_mode::LIMITED_GENERAL:
28             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES / 2;
29             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_LIMITED_GENERAL;
30             break;
31         case big_payload_test::test_mode::QUEUE_LIMITED_GENERAL:
32             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES / 2;
33             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_QUEUE_LIMITED_GENERAL;
34             break;
35         case big_payload_test::test_mode::QUEUE_LIMITED_SPECIFIC:
36             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES / 2;
37             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_QUEUE_LIMITED_SPECIFIC;
38             break;
39         case big_payload_test::test_mode::UDP:
40             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES;
41             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID_UDP;
42             break;
43         default:
44             expected_messages_ = big_payload_test::BIG_PAYLOAD_TEST_NUMBER_MESSAGES;
45             service_id_ = big_payload_test::TEST_SERVICE_SERVICE_ID;
46             break;
47     }
48 }
49 
init()50 bool big_payload_test_service::init()
51 {
52     std::lock_guard<std::mutex> its_lock(mutex_);
53     std::srand(static_cast<unsigned int>(std::time(0)));
54     if (!app_->init()) {
55         ADD_FAILURE() << "Couldn't initialize application";
56         return false;
57     }
58     app_->register_message_handler(vsomeip::ANY_SERVICE,
59             big_payload_test::TEST_SERVICE_INSTANCE_ID,
60             big_payload_test::TEST_SERVICE_METHOD_ID,
61             std::bind(&big_payload_test_service::on_message, this,
62                     std::placeholders::_1));
63 
64     app_->register_state_handler(
65             std::bind(&big_payload_test_service::on_state, this,
66                     std::placeholders::_1));
67     return true;
68 }
69 
start()70 void big_payload_test_service::start()
71 {
72     VSOMEIP_INFO << "Starting...";
73     app_->start();
74 }
75 
stop()76 void big_payload_test_service::stop()
77 {
78     VSOMEIP_INFO << "Stopping...";
79     stop_offer();
80     app_->clear_all_handler();
81     app_->stop();
82 }
83 
join_offer_thread()84 void big_payload_test_service::join_offer_thread()
85 {
86     offer_thread_.join();
87 }
88 
detach_offer_thread()89 void big_payload_test_service::detach_offer_thread()
90 {
91     offer_thread_.detach();
92 }
93 
offer()94 void big_payload_test_service::offer() {
95     app_->offer_service(service_id_,
96             big_payload_test::TEST_SERVICE_INSTANCE_ID);
97 }
98 
stop_offer()99 void big_payload_test_service::stop_offer() {
100     app_->stop_offer_service(service_id_,
101             big_payload_test::TEST_SERVICE_INSTANCE_ID);
102 }
103 
on_state(vsomeip::state_type_e _state)104 void big_payload_test_service::on_state(vsomeip::state_type_e _state)
105 {
106     VSOMEIP_INFO << "Application " << app_->get_name() << " is "
107             << (_state == vsomeip::state_type_e::ST_REGISTERED ? "registered." :
108                     "deregistered.");
109 
110     if(_state == vsomeip::state_type_e::ST_REGISTERED)
111     {
112         if(!is_registered_)
113         {
114             is_registered_ = true;
115             std::lock_guard<std::mutex> its_lock(mutex_);
116             blocked_ = true;
117             // "start" the run method thread
118             condition_.notify_one();
119         }
120     }
121     else
122     {
123         is_registered_ = false;
124     }
125 }
126 
on_message(const std::shared_ptr<vsomeip::message> & _request)127 void big_payload_test_service::on_message(const std::shared_ptr<vsomeip::message>& _request)
128 {
129     VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
130             << std::setfill('0') << std::hex << _request->get_client() << "/"
131             << std::setw(4) << std::setfill('0') << std::hex
132             << _request->get_session() << "] size: " << std::dec
133             << _request->get_payload()->get_length();
134     {
135         std::lock_guard<std::mutex> its_lock(mutex_);
136         incoming_requests_.push(_request);
137         condition_.notify_one();
138     }
139 
140 
141 }
142 
run()143 void big_payload_test_service::run()
144 {
145     {
146         std::unique_lock<std::mutex> its_lock(mutex_);
147         while (!blocked_) {
148             condition_.wait(its_lock);
149         }
150 
151         offer();
152 
153         // wait for shutdown
154         blocked_ = false;
155         while (!blocked_ || !incoming_requests_.empty()) {
156             if (incoming_requests_.empty()) {
157                 condition_.wait(its_lock);
158             }
159             auto _request = incoming_requests_.front();
160             incoming_requests_.pop();
161             number_of_received_messages_++;
162             its_lock.unlock();
163 
164             static vsomeip::session_t last_session(0);
165             ASSERT_GT(_request->get_session(), last_session);
166             last_session = _request->get_session();
167             if (test_mode_ == big_payload_test::test_mode::RANDOM) {
168                 EXPECT_LT(_request->get_payload()->get_length(), big_payload_test::BIG_PAYLOAD_SIZE_RANDOM);
169             } else if (test_mode_ == big_payload_test::test_mode::UDP) {
170                 EXPECT_EQ(big_payload_test::BIG_PAYLOAD_SIZE_UDP, _request->get_payload()->get_length());
171             } else {
172                 EXPECT_EQ(big_payload_test::BIG_PAYLOAD_SIZE, _request->get_payload()->get_length());
173             }
174             bool check(true);
175             vsomeip::length_t len = _request->get_payload()->get_length();
176             vsomeip::byte_t* datap = _request->get_payload()->get_data();
177             for(unsigned int i = 0; i < len; ++i) {
178                 check = check && datap[i] == big_payload_test::DATA_CLIENT_TO_SERVICE;
179             }
180             if(!check) {
181                 GTEST_FATAL_FAILURE_("wrong data transmitted");
182             }
183 
184             // send response
185             std::shared_ptr<vsomeip::message> its_response =
186                     vsomeip::runtime::get()->create_response(_request);
187 
188             std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()
189             ->create_payload();
190             std::vector<vsomeip::byte_t> its_payload_data;
191             if (test_mode_ == big_payload_test::test_mode::RANDOM) {
192                 its_payload_data.assign(static_cast<unsigned int>(std::rand()) % big_payload_test::BIG_PAYLOAD_SIZE_RANDOM,
193                         big_payload_test::DATA_SERVICE_TO_CLIENT);
194             } else if (test_mode_ == big_payload_test::test_mode::LIMITED
195                     || test_mode_ == big_payload_test::test_mode::LIMITED_GENERAL
196                     || test_mode_ == big_payload_test::test_mode::QUEUE_LIMITED_GENERAL
197                     || test_mode_ == big_payload_test::test_mode::QUEUE_LIMITED_SPECIFIC) {
198                 if (number_of_received_messages_ % 2) {
199                     // try to send to big response for half of the received messsages.
200                     // this way the client will only get replies for a fourth of his sent
201                     // requests as he tries to sent to big data for every second request
202                     // as well
203                     its_payload_data.assign(big_payload_test::BIG_PAYLOAD_SIZE + 1,
204                             big_payload_test::DATA_SERVICE_TO_CLIENT);
205                 } else {
206                     its_payload_data.assign(big_payload_test::BIG_PAYLOAD_SIZE,
207                             big_payload_test::DATA_SERVICE_TO_CLIENT);
208                 }
209             } else if (test_mode_ == big_payload_test::test_mode::UDP) {
210                 its_payload_data.assign(big_payload_test::BIG_PAYLOAD_SIZE_UDP,
211                         big_payload_test::DATA_SERVICE_TO_CLIENT);
212             } else {
213                 its_payload_data.assign(big_payload_test::BIG_PAYLOAD_SIZE,
214                         big_payload_test::DATA_SERVICE_TO_CLIENT);
215             }
216 
217             its_payload->set_data(its_payload_data);
218             its_response->set_payload(its_payload);
219 
220             app_->send(its_response);
221 
222             if(number_of_received_messages_ == expected_messages_) {
223                 ASSERT_EQ(expected_messages_, number_of_received_messages_);
224                 blocked_ = true;
225             }
226             its_lock.lock();
227         }
228     }
229     std::this_thread::sleep_for(std::chrono::seconds(3));
230     if (test_mode_ == big_payload_test::test_mode::LIMITED
231             || test_mode_ == big_payload_test::test_mode::LIMITED_GENERAL
232             || test_mode_ == big_payload_test::test_mode::QUEUE_LIMITED_GENERAL
233             || test_mode_ == big_payload_test::test_mode::QUEUE_LIMITED_SPECIFIC
234             || test_mode_ == big_payload_test::test_mode::UDP) {
235         EXPECT_EQ(expected_messages_, number_of_received_messages_);
236     }
237     stop();
238 }
239 
240 static big_payload_test::test_mode test_mode(big_payload_test::test_mode::UNKNOWN);
241 
242 
TEST(someip_big_payload_test,receive_ten_messages_and_send_reply)243 TEST(someip_big_payload_test, receive_ten_messages_and_send_reply)
244 {
245     big_payload_test_service test_service(test_mode);
246     if (test_service.init()) {
247         test_service.start();
248         test_service.join_offer_thread();
249     } else {
250         test_service.detach_offer_thread();
251     }
252 }
253 
254 #ifndef _WIN32
main(int argc,char ** argv)255 int main(int argc, char** argv)
256 {
257     ::testing::InitGoogleTest(&argc, argv);
258     if (argc > 1) {
259         if (std::string("RANDOM") == std::string(argv[1])) {
260             test_mode = big_payload_test::test_mode::RANDOM;
261         } else if (std::string("LIMITED") == std::string(argv[1])) {
262             test_mode = big_payload_test::test_mode::LIMITED;
263         } else if (std::string("LIMITEDGENERAL") == std::string(argv[1])) {
264             test_mode = big_payload_test::test_mode::LIMITED_GENERAL;
265         } else if (std::string("QUEUELIMITEDGENERAL") == std::string(argv[1])) {
266             test_mode = big_payload_test::test_mode::QUEUE_LIMITED_GENERAL;
267         } else if (std::string("QUEUELIMITEDSPECIFIC") == std::string(argv[1])) {
268             test_mode = big_payload_test::test_mode::QUEUE_LIMITED_SPECIFIC;
269         } else if (std::string("UDP") == std::string(argv[1])) {
270             test_mode = big_payload_test::test_mode::UDP;
271         }
272     }
273     return RUN_ALL_TESTS();
274 }
275 #endif
276