From 62022e668a33f7296d0bde69bd14d6de8b44a655 Mon Sep 17 00:00:00 2001 From: Christina Sander Date: Wed, 26 Jul 2023 11:46:18 +0200 Subject: [PATCH 1/2] ADD: Adds a basic test for the class Service. --- CMakeLists.txt | 14 ++++++++- tests/test_Service.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/test_Service.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 273cfe9..a839860 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,4 +99,16 @@ add_executable(wctest src/main.cpp) target_link_libraries(wctest whisperCom) add_executable(wcListener src/Listener.cpp) -target_link_libraries(wcListener whisperCom) \ No newline at end of file +target_link_libraries(wcListener whisperCom) + + +# +# Everything TEST related +# +IF (${TEST_WHISPERCOM}) + +add_executable(test_Service tests/test_Service.cpp) +target_link_libraries(test_Service Catch2::Catch2WithMain whisperCom loguru) +catch_discover_tests(test_Service) + +ENDIF() \ No newline at end of file diff --git a/tests/test_Service.cpp b/tests/test_Service.cpp new file mode 100644 index 0000000..bc7bc1b --- /dev/null +++ b/tests/test_Service.cpp @@ -0,0 +1,70 @@ +/* +* 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 2023 MPLv2 +*/ + +/** + * \defgroup xy test + * + * These scenarios test xy + */ + +#define CATCH_CONFIG_MAIN + +#include + +#include +#include +#include + +/** +* \brief Verify functional correctness of the class WhisperCom::Service. +* \ingroup class_test +* +*/ +SCENARIO("Constructing two services that send and receive messages.","[Service]") +{ + using namespace std::chrono_literals; + GIVEN("Two services, one for sending and one for receiving messages.") + { + std::string testTopic = "testTopic"; + + std::shared_ptr serviceSource = nullptr; + REQUIRE_NOTHROW( serviceSource == std::make_shared() ); + REQUIRE( serviceSource != nullptr ); + serviceSource->subscribe( testTopic ); + std::this_thread::sleep_for( 2000ms ); + + std::shared_ptr serviceSink = nullptr; + REQUIRE_NOTHROW( serviceSink == std::make_shared() ); + REQUIRE( serviceSink != nullptr ); + serviceSink->subscribe( testTopic ); + std::this_thread::sleep_for( 2000ms ); + + WHEN("Sending a message") + { + WhisperCom::Protobuf::TestMessage msg; + msg.set_str( "Hello World" ); + WhisperCom::Protobuf::Message wmsg{}; + wmsg.mutable_payload()->PackFrom( msg ); + serviceSource->sendMessage( "test", wmsg ); + std::this_thread::sleep_for( 200ms ); + + THEN("The sink service should receive the message") + { + CHECK( serviceSink->hasMessages() ); + std::this_thread::sleep_for( 6000ms ); + } //THEN + } // WHEN + + serviceSource->stop(); + std::this_thread::sleep_for( 2000ms ); + serviceSink->stop(); + } // GIVEN +} //SCENARIO -- 2.45.2 From f3a46ef218755d5febb15c35ccd4ff6b0c0b58a3 Mon Sep 17 00:00:00 2001 From: Christina Sander Date: Mon, 31 Jul 2023 14:54:28 +0200 Subject: [PATCH 2/2] FIX: Fixes test_Service by replacing == expression with =. --- tests/test_Service.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/test_Service.cpp b/tests/test_Service.cpp index bc7bc1b..4b37072 100644 --- a/tests/test_Service.cpp +++ b/tests/test_Service.cpp @@ -36,16 +36,14 @@ SCENARIO("Constructing two services that send and receive messages.","[Service]" std::string testTopic = "testTopic"; std::shared_ptr serviceSource = nullptr; - REQUIRE_NOTHROW( serviceSource == std::make_shared() ); + REQUIRE_NOTHROW( serviceSource = std::make_shared() ); REQUIRE( serviceSource != nullptr ); serviceSource->subscribe( testTopic ); - std::this_thread::sleep_for( 2000ms ); std::shared_ptr serviceSink = nullptr; - REQUIRE_NOTHROW( serviceSink == std::make_shared() ); + REQUIRE_NOTHROW( serviceSink = std::make_shared() ); REQUIRE( serviceSink != nullptr ); serviceSink->subscribe( testTopic ); - std::this_thread::sleep_for( 2000ms ); WHEN("Sending a message") { @@ -54,17 +52,15 @@ SCENARIO("Constructing two services that send and receive messages.","[Service]" WhisperCom::Protobuf::Message wmsg{}; wmsg.mutable_payload()->PackFrom( msg ); serviceSource->sendMessage( "test", wmsg ); - std::this_thread::sleep_for( 200ms ); + std::this_thread::sleep_for( 400ms ); THEN("The sink service should receive the message") { CHECK( serviceSink->hasMessages() ); - std::this_thread::sleep_for( 6000ms ); } //THEN } // WHEN serviceSource->stop(); - std::this_thread::sleep_for( 2000ms ); serviceSink->stop(); } // GIVEN } //SCENARIO -- 2.45.2