Squashed 'libs/CommService/' content from commit 7ccc0fc

git-subtree-dir: libs/CommService
git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
This commit is contained in:
Henry Winkel
2022-09-15 09:53:53 +02:00
commit cc67e4840f
799 changed files with 179487 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
/*
* 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
* @copyright 2022 MPLv2
*/
/**
* \defgroup class test
*
* These scenarios test the class BC::BasicMessageQueue
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <BC/BC.hpp>
#include <BC/BasicMessageQueue.hpp>
#include <exception>
#include <mutex>
#include <thread>
#include <memory>
#include <chrono>
/**
* \brief Verify functional correctness of class BasicMessageQueue.
* \ingroup class_test
*
*/
SCENARIO("Testing class BasicMessageQueue ","[BasicMessageQueue]")
{
GIVEN("A BasicMessageQueue object")
{
std::shared_ptr<BC::BasicMessageQueue> testQueue;
REQUIRE_NOTHROW( testQueue = std::make_shared<BC::BasicMessageQueue>() );
WHEN("appending a message")
{
auto msg = std::make_unique<BC::Message>();
testQueue->appendMessage( std::move(msg) );
THEN("the message queue should not be empty")
{
CHECK( 0 != testQueue->size() );
} //THEN
} // WHEN
WHEN("appending several messages and getting them from the queue")
{
for( int i = 0; i < 10; i++)
{
auto msg = std::make_unique<BC::Message>();
msg->topic = "test topic";
testQueue->appendMessage( std::move(msg) );
}
THEN("the message contents should not be changed")
{
CHECK( 10 == testQueue->size() );
std::unique_ptr<BC::Message> m;
for( int i = 0; i < testQueue->size(); i++)
{
REQUIRE_NOTHROW( m = testQueue->getMessage() );
CHECK( m->topic == "test topic" );
}
} //THEN
} // WHEN
WHEN("waiting for new message and appending messages")
{
REQUIRE( 0 == testQueue->size() );
std::thread myTestThread( &BC::BasicMessageQueue::waitForNewMessage, testQueue);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
THEN("the function should wait for the new message to be appended")
{
CHECK( true == myTestThread.joinable() );
std::this_thread::sleep_for(std::chrono::milliseconds(100));
CHECK( true == myTestThread.joinable() );
auto msg = std::make_unique<BC::Message>();
testQueue->appendMessage( std::move(msg) );
std::this_thread::sleep_for(std::chrono::milliseconds(100));
CHECK( 0 == testQueue->size() );
} //THEN
myTestThread.join();
} // WHEN
WHEN("trying to get a message from an empty message queue")
{
REQUIRE( 0 == testQueue->size() );
THEN("the message contents should not be changed")
{
REQUIRE_THROWS( testQueue->getMessage() );
} //THEN
} // WHEN
} // GIVEN
} //SCENARIO

View File

@@ -0,0 +1,79 @@
/*
* 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
* @copyright 2022 MPLv2
*/
/**
* \defgroup class test
*
* These scenarios test the class BC::BasicMessageQueue
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <BC/BC.hpp>
#include <BC/BasicQueueReceiver.hpp>
#include <exception>
#include <mutex>
#include <thread>
#include <memory>
#include <chrono>
/**
* \brief Verify functional correctness of class BasicQueueReceiver.
* \ingroup class_test
*
*/
SCENARIO("Testing class BasicQueueReceiver ","[BasicQueueReceiver]")
{
GIVEN("A BasicQueueReceiver object")
{
std::shared_ptr<BC::BasicQueueReceiver> testReceiver = nullptr;
REQUIRE_NOTHROW( testReceiver = std::make_shared<BC::BasicQueueReceiver>() );
REQUIRE( 0 == testReceiver->getQueueSize() );
WHEN("receiving a new message")
{
testReceiver->receive( new BC::Message{} );
THEN("the message queue should not be empty")
{
REQUIRE( 1 == testReceiver->getQueueSize() );
} //THEN
} // WHEN
WHEN("receiving a new message")
{
testReceiver->receive( new BC::Message{} );
THEN("the message queue should not be empty")
{
REQUIRE( 1 == testReceiver->getQueueSize() );
} //THEN
} // WHEN
WHEN("waiting for new message and appending messages")
{
REQUIRE( 0 == testReceiver->getQueueSize() );
std::thread myTestThread( &BC::BasicQueueReceiver::waitForNewMessage, testReceiver);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
THEN("the function should wait for a new message to be received")
{
CHECK( true == myTestThread.joinable() );
auto msg = std::make_unique<BC::Message>();
testReceiver->receive( new BC::Message{} );
std::this_thread::sleep_for(std::chrono::milliseconds(100));
CHECK( 0 == testReceiver->getQueueSize() );
} //THEN
myTestThread.join();
} // WHEN
} // GIVEN
} //SCENARIO

View File

@@ -0,0 +1,97 @@
/*
* 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 test file for the BaseTrack class
* @author Dominik Meyer <dmeyer@hsu-hh.de>
* @date 2018-11-21
* @copyright 2018 no yet defined
* @test testing conversion fromSimpleToByteArray and fromByteArrayToSimple
*/
#include <BC/BC.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdint>
#include <algorithm>
int main()
{
std::vector<unsigned char> testVector;
// initialize random number generator
std::srand(std::time(nullptr));
int numberRuns=100000;
// start with BC::DataTypes::deviceIdType
BC::DataTypes::deviceIdType dit = std::rand() % UINT32_MAX;
BC::DataTypes::deviceIdType dit2;
for(int i=0; i<numberRuns; i++)
{
dit = std::rand() % UINT32_MAX;
testVector = BC::DataTypes::Convert::fromSimpleToByteArray(dit);
dit2=BC::DataTypes::Convert::fromByteArrayToSimple<BC::DataTypes::deviceIdType>(testVector);
if ( dit != dit2 )
{
std::cout << "Test failed" << std::endl;
return -1;
}
}
// test fromSimpleVectorToByteArray and back
std::vector<uint32_t> data;
std::vector<uint32_t> data2;
std::vector<unsigned char> v;
uint32_t nrElements = std::rand() % 100000 +1000;
for(uint32_t i=0; i<nrElements; i++)
{
data.push_back(std::rand() % UINT32_MAX);
}
v=BC::DataTypes::Convert::fromSimpleContainerToByteArray(data);
data2=BC::DataTypes::Convert::fromByteArrayToSimpleContainer<std::vector<uint32_t>,uint32_t>(v);
if (data.size() != data2.size())
{
std::cout << "Convert of uint32_t vector failed. Sizes do not match" << std::endl;
return -1;
}
// test if elements are the same
if (!std::equal(data.begin(), data.end(), data2.begin()))
{
std::cout << "Convert of uint32_t vector failed. elements do not match" << std::endl;
return -1;
}
//test vector of string conversion
std::vector<std::string> strings;
std::vector<std::string> strings2;
for(uint32_t i=0; i<20; i++)
{
strings.push_back("hallo");
}
v.clear();
v=BC::DataTypes::Convert::fromStringContainerToByteArray(strings);
strings2=BC::DataTypes::Convert::fromByteArrayToStringContainer<std::vector<std::string>>(v);
// test if elements are the same
if (!std::equal(strings.begin(), strings.end(), strings2.begin()))
{
std::cout << "Convert of string vector failed. elements do not match" << std::endl;
return -1;
}
return 0;
}

View 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;
}