Squashed 'libs/CommService/' content from commit 7ccc0fc
git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
This commit is contained in:
139
libs/libbattle-com/tests/test_simple_service_udp.cpp
Normal file
139
libs/libbattle-com/tests/test_simple_service_udp.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief base test file for the SimpleService class
|
||||
* @author Dominik Meyer <dmeyer@hsu-hh.de>
|
||||
* @date 2019-01-08
|
||||
* @copyright 2019 no yet defined
|
||||
@test testing that the SimpleServiceUDP service can exchange messages
|
||||
*/
|
||||
#include <BC/BC.hpp>
|
||||
#include <BC/SimpleServiceUDP.hpp>
|
||||
#include <BC/Message.hpp>
|
||||
#include <BC/receiveable.hpp>
|
||||
#include <BC/Payloads/Pong.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <atomic>
|
||||
|
||||
class recv : public BC::receiveable
|
||||
{
|
||||
private:
|
||||
std::atomic<int> firstMsg;
|
||||
|
||||
std::mutex mutexService;
|
||||
|
||||
public:
|
||||
std::atomic<bool> joinedBS0;
|
||||
std::atomic<bool> joinedBS1;
|
||||
std::atomic<int> succeed;
|
||||
BC::SimpleServiceUDP *s;
|
||||
|
||||
|
||||
recv() : firstMsg(0), joinedBS0(false), joinedBS1(false),succeed(0),s(nullptr) {};
|
||||
void receive(BC::Message *m)
|
||||
{
|
||||
if (m->topic == "Test" && firstMsg==0)
|
||||
{
|
||||
BC::Message msg;
|
||||
msg.sourceID=10;
|
||||
msg.destinationID=20;
|
||||
msg.domain="Test";
|
||||
msg.topic="Return";
|
||||
|
||||
BC::Payloads::Pong pong;
|
||||
msg.setData(pong);
|
||||
s->publish(msg);
|
||||
firstMsg=1;
|
||||
}
|
||||
else if (m->topic == "Return" && firstMsg==1)
|
||||
{
|
||||
succeed=1;
|
||||
}
|
||||
else if (m->topic == BC::Constants::cHotplugJoin)
|
||||
{
|
||||
if (m->sourceID==10)
|
||||
{
|
||||
joinedBS1=true;
|
||||
} else if (m->sourceID==20)
|
||||
{
|
||||
joinedBS0=true;
|
||||
}
|
||||
|
||||
}
|
||||
else if (m->topic == BC::Constants::cHotplugLeave)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
else {
|
||||
std::cout << "Test failed unexspected message " << m->topic << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
// Seed with a real random value, if available
|
||||
std::random_device random;
|
||||
|
||||
// Choose a random mean between 1025 and 200000
|
||||
std::default_random_engine e1(random());
|
||||
std::uniform_int_distribution<int> uniform_dist(1025, 20000);
|
||||
int port = uniform_dist(e1);
|
||||
|
||||
BC::Message msg;
|
||||
recv r;
|
||||
|
||||
BC::SimpleServiceUDP bs0(20, BC::DataTypes::deviceMajorType::RADAR, "Test Radar", port,"127.0.0.255","127.0.0.1");
|
||||
BC::SimpleServiceUDP bs1(10, BC::DataTypes::deviceMajorType::UNKNOWN, "TE Test", port,"127.0.0.255","127.0.0.1");
|
||||
r.s=&bs1;
|
||||
|
||||
bs1.subscribe(&r,BC::Constants::cAllDomains,BC::Constants::cAllTopics);
|
||||
bs0.subscribe(&r,BC::Constants::cAllDomains,BC::Constants::cAllTopics);
|
||||
|
||||
bs0.connect();
|
||||
bs1.connect();
|
||||
|
||||
msg.sourceID=20;
|
||||
msg.destinationID=10;
|
||||
msg.domain="Test";
|
||||
msg.topic="Test";
|
||||
|
||||
BC::Payloads::Pong pong;
|
||||
msg.setData(pong);
|
||||
|
||||
int waiting=100;
|
||||
while(!r.joinedBS1 && waiting!=0)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
waiting--;
|
||||
}
|
||||
if(waiting == 0)
|
||||
{
|
||||
std::cout << "Test failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bs0.publish(msg);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
|
||||
bs0.disconnect();
|
||||
bs1.disconnect();
|
||||
|
||||
if (r.succeed == 0)
|
||||
{
|
||||
std::cout << "Test failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Test succeeded" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user