Files
SimControl/libs/libbattle-com/tests/test_basic_queue_receiver.cpp
Henry Winkel cc67e4840f Squashed 'libs/CommService/' content from commit 7ccc0fc
git-subtree-dir: libs/CommService
git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
2022-09-15 09:53:53 +02:00

80 lines
2.1 KiB
C++

/*
* 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