/* * 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 #include #include #include #include #include #include #include /** * \brief Verify functional correctness of class BasicMessageQueue. * \ingroup class_test * */ SCENARIO("Testing class BasicMessageQueue ","[BasicMessageQueue]") { GIVEN("A BasicMessageQueue object") { std::shared_ptr testQueue; REQUIRE_NOTHROW( testQueue = std::make_shared() ); WHEN("appending a message") { auto msg = std::make_unique(); 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(); msg->topic = "test topic"; testQueue->appendMessage( std::move(msg) ); } THEN("the message contents should not be changed") { CHECK( 10 == testQueue->size() ); std::unique_ptr 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(); 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