Squashed 'libs/CommService/' content from commit 7ccc0fc
git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
This commit is contained in:
107
libs/libbattle-com/tests/test_basic_message_queue.cpp
Normal file
107
libs/libbattle-com/tests/test_basic_message_queue.cpp
Normal 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user