diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd748e91..b3885f74 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,12 +39,12 @@ jobs: - name: Build up-core-api conan package shell: bash run: | - conan create --version 1.6.0-alpha2 up-conan-recipes/up-core-api/release + conan create --version 1.6.0-alpha4 up-conan-recipes/up-core-api/release - name: Build up-cpp conan package shell: bash run: | - conan create --version 1.0.1 --build=missing up-conan-recipes/up-cpp/release + conan create --version 1.0.1-dev --build=missing up-conan-recipes/up-cpp/developer - name: Build zenohcpp conan package shell: bash @@ -163,7 +163,7 @@ jobs: style: 'file' # read .clang-format for configuration tidy-checks: '' # Read .clang-tidy for configuration database: compile_commands.json - version: 12 + version: 13 - name: Run linters on tests id: test-linter @@ -176,7 +176,7 @@ jobs: style: 'file' # read .clang-format for configuration tidy-checks: '' # Read .clang-tidy for configuration database: compile_commands.json - version: 12 + version: 13 - name: Report lint failure if: diff --git a/README.md b/README.md index 95fd9d2e..6f2e8c55 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ up-transport-zenoh-cpp, follow the steps in the ### With Conan for dependencies ``` -cd up-client-zenoh-cpp +cd up-transport-zenoh-cpp conan install . --build=missing cmake --preset conan-release cd build/Release diff --git a/conanfile.txt b/conanfile.txt index 042c163e..ce2b80fb 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,9 +1,9 @@ [requires] -up-cpp/[^1.0.1] +up-cpp/[^1.0.1, include_prerelease] zenohcpp/1.2.1 zenohc/1.2.1 spdlog/[~1.13] -up-core-api/[~1.6, include_prerelease] +up-core-api/1.6.0-alpha4 protobuf/[~3.21] [test_requires] diff --git a/include/up-transport-zenoh-cpp/ThreadSafeMap.h b/include/up-transport-zenoh-cpp/ThreadSafeMap.h index 8bd8e743..2711a763 100644 --- a/include/up-transport-zenoh-cpp/ThreadSafeMap.h +++ b/include/up-transport-zenoh-cpp/ThreadSafeMap.h @@ -1,3 +1,17 @@ +// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0 +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H +#define UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H + #include #include #include @@ -25,9 +39,8 @@ class ThreadSafeMap { Iterator it = map_.find(key); if (it != map_.end()) { return it->second; - } else { - return std::nullopt; } + return std::nullopt; } template @@ -36,12 +49,13 @@ class ThreadSafeMap { Iterator it = std::find_if(map_.begin(), map_.end(), pred); if (it != map_.end()) { return it->second; - } else { - return std::nullopt; } + return std::nullopt; } private: MapType map_; mutable std::mutex mutex_; }; + +#endif // UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H \ No newline at end of file diff --git a/include/up-transport-zenoh-cpp/ZenohUTransport.h b/include/up-transport-zenoh-cpp/ZenohUTransport.h index 3b3c38ef..2688d13f 100644 --- a/include/up-transport-zenoh-cpp/ZenohUTransport.h +++ b/include/up-transport-zenoh-cpp/ZenohUTransport.h @@ -15,9 +15,7 @@ #include #include -#include #include -#include #define ZENOHCXX_ZENOHC #include @@ -46,14 +44,14 @@ namespace uprotocol::transport { struct ZenohUTransport : public UTransport { /// @brief Constructor /// - /// @param defaultUri Default Authority and Entity (as a UUri) for + /// @param default_uri Default Authority and Entity (as a UUri) for /// clients using this transport instance. - /// @param configFile Path to a configuration file containing the Zenoh + /// @param config_file Path to a configuration file containing the Zenoh /// transport configuration. - ZenohUTransport(const v1::UUri& defaultUri, - const std::filesystem::path& configFile); + ZenohUTransport(const v1::UUri& default_uri, + const std::filesystem::path& config_file); - virtual ~ZenohUTransport() = default; + ~ZenohUTransport() override = default; protected: /// @brief Send a message. @@ -63,8 +61,7 @@ struct ZenohUTransport : public UTransport { /// @returns * OKSTATUS if the payload has been successfully /// sent (ACK'ed) /// * FAILSTATUS with the appropriate failure otherwise. - [[nodiscard]] virtual v1::UStatus sendImpl( - const v1::UMessage& message) override; + [[nodiscard]] v1::UStatus sendImpl(const v1::UMessage& message) override; /// @brief Represents the callable end of a callback connection. using CallableConn = typename UTransport::CallableConn; @@ -81,7 +78,7 @@ struct ZenohUTransport : public UTransport { /// /// @returns * OKSTATUS if the listener was registered successfully. /// * FAILSTATUS with the appropriate failure otherwise. - [[nodiscard]] virtual v1::UStatus registerListenerImpl( + [[nodiscard]] v1::UStatus registerListenerImpl( CallableConn&& listener, const v1::UUri& source_filter, std::optional&& sink_filter) override; @@ -93,7 +90,7 @@ struct ZenohUTransport : public UTransport { /// @note The default implementation does nothing. /// /// @param listener shared_ptr of the Connection that has been broken. - virtual void cleanupListener(CallableConn listener) override; + void cleanupListener(const CallableConn& listener) override; static std::string toZenohKeyString( const std::string& default_authority_name, const v1::UUri& source, diff --git a/lint/clang-format.sh b/lint/clang-format.sh index c4d7c0be..c01258cd 100755 --- a/lint/clang-format.sh +++ b/lint/clang-format.sh @@ -9,7 +9,7 @@ pushd -n "$PROJECT_ROOT" for f in **/*.h **/*.cpp; do echo echo "Checking file '$f'" - # NOTE: Using clang-format-12 in CI system, too - clang-format-12 -i "$f" + # NOTE: Using clang-format-13 in CI system, too + clang-format-13 -i "$f" done popd -n diff --git a/src/ZenohUTransport.cpp b/src/ZenohUTransport.cpp index de5d042b..c249631a 100644 --- a/src/ZenohUTransport.cpp +++ b/src/ZenohUTransport.cpp @@ -37,7 +37,7 @@ std::string ZenohUTransport::toZenohKeyString( const std::optional& sink) { std::ostringstream zenoh_key; - auto writeUUri = [&](const v1::UUri& uuri) { + auto write_u_uri = [&](const v1::UUri& uuri) { zenoh_key << "/"; // authority_name @@ -74,10 +74,10 @@ std::string ZenohUTransport::toZenohKeyString( zenoh_key << "up"; - writeUUri(source); + write_u_uri(source); if (sink.has_value()) { - writeUUri(*sink); + write_u_uri(*sink); } else { zenoh_key << "/{}/{}/{}/{}"; } @@ -105,13 +105,13 @@ v1::UAttributes ZenohUTransport::attachmentToUAttributes( if (attachment_vec.size() != 2) { spdlog::error("attachmentToUAttributes: attachment size != 2"); - // TODO: error report, exception? + // TODO(unknown) error report, exception? } if (attachment_vec[0].second.size() == 1) { if (attachment_vec[0].second[0] != UATTRIBUTE_VERSION) { spdlog::error("attachmentToUAttributes: incorrect version"); - // TODO: error report, exception? + // TODO(unknown) error report, exception? } }; v1::UAttributes res; @@ -166,8 +166,7 @@ std::optional ZenohUTransport::sampleToUMessage( "sampleToUMessage: empty attachment, cannot read uAttributes"); return std::nullopt; } - std::string payload( - zenoh::ext::deserialize(sample.get_payload())); + auto payload(zenoh::ext::deserialize(sample.get_payload())); message.set_payload(payload); return message; @@ -186,7 +185,7 @@ std::optional ZenohUTransport::queryToUMessage( return std::nullopt; } if (query.get_payload().has_value()) { - std::string payload(zenoh::ext::deserialize( + auto payload(zenoh::ext::deserialize( query.get_payload().value().get())); message.set_payload(payload); } @@ -194,12 +193,12 @@ std::optional ZenohUTransport::queryToUMessage( return message; } -ZenohUTransport::ZenohUTransport(const v1::UUri& defaultUri, - const std::filesystem::path& configFile) - : UTransport(defaultUri), +ZenohUTransport::ZenohUTransport(const v1::UUri& default_uri, + const std::filesystem::path& config_file) + : UTransport(default_uri), session_(zenoh::Session::open( - std::move(zenoh::Config::from_file(configFile.string().c_str())))) { - // TODO: add to setup or remove + zenoh::Config::from_file(config_file.string()))) { + // TODO(unknown) add to setup or remove spdlog::set_level(spdlog::level::debug); spdlog::info("ZenohUTransport init"); @@ -211,10 +210,10 @@ v1::UStatus ZenohUTransport::registerPublishNotificationListener_( // NOTE: listener is captured by copy here so that it does not go out // of scope when this function returns. - auto on_sample = [this, listener](const zenoh::Sample& sample) mutable { - auto maybeMessage = sampleToUMessage(sample); - if (maybeMessage.has_value()) { - listener(maybeMessage.value()); + auto on_sample = [listener](const zenoh::Sample& sample) mutable { + auto maybe_message = sampleToUMessage(sample); + if (maybe_message.has_value()) { + listener(maybe_message.value()); } else { spdlog::error("on_sample: failed to retrieve uMessage"); } @@ -225,7 +224,7 @@ v1::UStatus ZenohUTransport::registerPublishNotificationListener_( auto subscriber = session_.declare_subscriber( zenoh_key, std::move(on_sample), std::move(on_drop)); subscriber_map_.emplace(listener, std::move(subscriber)); - return v1::UStatus(); + return {}; } v1::UStatus ZenohUTransport::sendPublishNotification_( @@ -250,7 +249,7 @@ v1::UStatus ZenohUTransport::sendPublishNotification_( return uError(v1::UCode::INTERNAL, e.what()); } - return v1::UStatus(); + return {}; } // NOTE: Messages have already been validated by the base class. It does not @@ -278,10 +277,10 @@ v1::UStatus ZenohUTransport::registerListenerImpl( std::string zenoh_key = toZenohKeyString(getEntityUri().authority_name(), source_filter, sink_filter); - return registerPublishNotificationListener_(zenoh_key, listener); + return registerPublishNotificationListener_(zenoh_key, std::move(listener)); } -void ZenohUTransport::cleanupListener(CallableConn listener) { +void ZenohUTransport::cleanupListener(const CallableConn& listener) { subscriber_map_.erase(listener); } diff --git a/test/coverage/ZenohUTransportTest.cpp b/test/coverage/ZenohUTransportTest.cpp index 29a5397b..df72b2ea 100644 --- a/test/coverage/ZenohUTransportTest.cpp +++ b/test/coverage/ZenohUTransportTest.cpp @@ -18,14 +18,11 @@ #include "up-transport-zenoh-cpp/ZenohUTransport.h" -namespace { - -using namespace uprotocol; +namespace uprotocol { constexpr std::string_view ZENOH_CONFIG_FILE = BUILD_REALPATH_ZENOH_CONF; constexpr std::string_view ENTITY_URI_STR = "//test0/10001/1/0"; -constexpr std::string_view TOPIC_URI_STR = "//test0/10001/1/8000"; class TestZenohUTransport : public testing::Test { protected: @@ -37,21 +34,27 @@ class TestZenohUTransport : public testing::Test { // Run once per execution of the test application. // Used for setup of all tests. Has access to this instance. TestZenohUTransport() = default; - ~TestZenohUTransport() = default; // Run once per execution of the test application. // Used only for global setup outside of tests. static void SetUpTestSuite() {} static void TearDownTestSuite() {} + +public: + ~TestZenohUTransport() override = default; }; -uprotocol::v1::UUri create_uuri(const std::string& authority, uint32_t ue_id, - uint32_t ue_version_major, - uint32_t resource_id) { +struct UeDetails { + uint32_t ue_id; + uint32_t ue_version_major; +}; + +uprotocol::v1::UUri create_uuri(const std::string& authority, + UeDetails ue_details, uint32_t resource_id) { uprotocol::v1::UUri uuri; uuri.set_authority_name(authority); - uuri.set_ue_id(ue_id); - uuri.set_ue_version_major(ue_version_major); + uuri.set_ue_id(ue_details.ue_id); + uuri.set_ue_version_major(ue_details.ue_version_major); uuri.set_resource_id(resource_id); return uuri; @@ -63,7 +66,7 @@ v1::UUri create_uuri(std::string_view serialized) { } // TODO(sashacmc): config generation -TEST_F(TestZenohUTransport, ConstructDestroy) { +TEST_F(TestZenohUTransport, ConstructDestroy) { // NOLINT std::cout << ZENOH_CONFIG_FILE << std::endl; zenoh::init_log_from_env_or("error"); @@ -74,45 +77,45 @@ TEST_F(TestZenohUTransport, ConstructDestroy) { struct ExposeKeyString : public transport::ZenohUTransport { template - static auto toZenohKeyString(Args&&... args) { + static auto toZenohKeyString(const std::string& prefix, Args&&... args) { return transport::ZenohUTransport::toZenohKeyString( - std::forward(args)...); + prefix, std::forward(args)...); } }; -TEST_F(TestZenohUTransport, toZenohKeyString) { +TEST_F(TestZenohUTransport, toZenohKeyString) { // NOLINT EXPECT_TRUE( (std::is_base_of_v)); - EXPECT_EQ( - ExposeKeyString::toZenohKeyString( - "", create_uuri("192.168.1.100", 0x10AB, 3, 0x80CD), std::nullopt), - "up/192.168.1.100/10AB/3/80CD/{}/{}/{}/{}"); + EXPECT_EQ(ExposeKeyString::toZenohKeyString( + "", create_uuri("192.168.1.100", {0x10AB, 3}, 0x80CD), + std::nullopt), + "up/192.168.1.100/10AB/3/80CD/{}/{}/{}/{}"); EXPECT_EQ(ExposeKeyString::toZenohKeyString( - "", create_uuri("192.168.1.100", 0x10AB, 3, 0x80CD), - create_uuri("192.168.1.101", 0x20EF, 4, 0)), + "", create_uuri("192.168.1.100", {0x10AB, 3}, 0x80CD), + create_uuri("192.168.1.101", {0x20EF, 4}, 0)), "up/192.168.1.100/10AB/3/80CD/192.168.1.101/20EF/4/0"); EXPECT_EQ(ExposeKeyString::toZenohKeyString( - "", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF), - create_uuri("192.168.1.101", 0x20EF, 4, 0)), + "", create_uuri("*", {0xFFFF, 0xFF}, 0xFFFF), + create_uuri("192.168.1.101", {0x20EF, 4}, 0)), "up/*/*/*/*/192.168.1.101/20EF/4/0"); EXPECT_EQ(ExposeKeyString::toZenohKeyString( - "", create_uuri("my-host1", 0x10AB, 3, 0), - create_uuri("my-host2", 0x20EF, 4, 0xB)), + "", create_uuri("my-host1", {0x10AB, 3}, 0), + create_uuri("my-host2", {0x20EF, 4}, 0xB)), "up/my-host1/10AB/3/0/my-host2/20EF/4/B"); EXPECT_EQ(ExposeKeyString::toZenohKeyString( - "", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF), - create_uuri("my-host2", 0x20EF, 4, 0xB)), + "", create_uuri("*", {0xFFFF, 0xFF}, 0xFFFF), + create_uuri("my-host2", {0x20EF, 4}, 0xB)), "up/*/*/*/*/my-host2/20EF/4/B"); EXPECT_EQ(ExposeKeyString::toZenohKeyString( - "", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF), - create_uuri("[::1]", 0xFFFF, 0xFF, 0xFFFF)), + "", create_uuri("*", {0xFFFF, 0xFF}, 0xFFFF), + create_uuri("[::1]", {0xFFFF, 0xFF}, 0xFFFF)), "up/*/*/*/*/[::1]/*/*/*"); } -} // namespace +} // namespace uprotocol diff --git a/test/extra/NotificationTest.cpp b/test/extra/NotificationTest.cpp index 6a204766..4a2bcee6 100644 --- a/test/extra/NotificationTest.cpp +++ b/test/extra/NotificationTest.cpp @@ -17,9 +17,7 @@ #include "up-transport-zenoh-cpp/ZenohUTransport.h" -namespace { - -using namespace uprotocol; +namespace uprotocol { constexpr std::string_view ZENOH_CONFIG_FILE = BUILD_REALPATH_ZENOH_CONF; @@ -33,18 +31,21 @@ class NotificationTest : public testing::Test { // Run once per execution of the test application. // Used for setup of all tests. Has access to this instance. NotificationTest() = default; - ~NotificationTest() = default; // Run once per execution of the test application. // Used only for global setup outside of tests. static void SetUpTestSuite() {} static void TearDownTestSuite() {} + +public: + ~NotificationTest() override = default; }; v1::UUri getUUri(uint16_t resource) { + constexpr uint32_t DEFAULT_UE_ID = 0x10001; v1::UUri uuri; uuri.set_authority_name(static_cast("test0")); - uuri.set_ue_id(0x10001); + uuri.set_ue_id(DEFAULT_UE_ID); uuri.set_ue_version_major(1); uuri.set_resource_id(resource); return uuri; @@ -56,16 +57,18 @@ std::shared_ptr getTransport( ZENOH_CONFIG_FILE); } -TEST_F(NotificationTest, BasicNotificationTestWithPayload) { +TEST_F(NotificationTest, BasicNotificationTestWithPayload) { // NOLINT + constexpr uint16_t RESOURCE1_ID = 0x8000; + constexpr uint16_t RESOURCE2_ID = 0x8001; zenoh::init_log_from_env_or("error"); auto transport = getTransport(); - auto source = getUUri(0x8000); + auto source = getUUri(RESOURCE1_ID); auto sink = getUUri(0); auto source_filter = source; - constexpr int num_messages = 25; + constexpr int NUM_MESSAGES = 25; - auto notificationSource = communication::NotificationSource( + auto notification_source = communication::NotificationSource( transport, std::move(source), std::move(sink), v1::UPAYLOAD_FORMAT_TEXT); @@ -74,44 +77,46 @@ TEST_F(NotificationTest, BasicNotificationTestWithPayload) { std::queue rx_queue; auto on_rx = [&rx_queue_mtx, &rx_queue](const v1::UMessage& message) { std::lock_guard lock(rx_queue_mtx); - rx_queue.push(std::move(message)); + rx_queue.push(message); }; auto maybe_sink = communication::NotificationSink::create( - transport, std::move(on_rx), std::move(source_filter)); + transport, std::move(on_rx), source_filter); EXPECT_TRUE(maybe_sink.has_value()); // Create a second sink with a different source filter to verify messages // arrive at the right sink - auto source_filter2 = getUUri(0x8001); - auto on_rx2 = [](const v1::UMessage& message) { FAIL(); }; + auto source_filter2 = getUUri(RESOURCE2_ID); + auto on_rx2 = [](const v1::UMessage& /*message*/) { FAIL(); }; auto maybe_sink2 = communication::NotificationSink::create( - transport, std::move(on_rx2), std::move(source_filter2)); + transport, std::move(on_rx2), source_filter2); EXPECT_TRUE(maybe_sink2.has_value()); // Send the notification messages if (maybe_sink.has_value()) { - for (auto remaining = num_messages; remaining > 0; --remaining) { + for (auto remaining = NUM_MESSAGES; remaining > 0; --remaining) { std::string message = "Hello, world!"; datamodel::builder::Payload payload(message, v1::UPAYLOAD_FORMAT_TEXT); - auto status = notificationSource.notify(std::move(payload)); + auto status = notification_source.notify(std::move(payload)); EXPECT_EQ(status.code(), v1::UCode::OK); } } - EXPECT_EQ(rx_queue.size(), num_messages); + EXPECT_EQ(rx_queue.size(), NUM_MESSAGES); } -TEST_F(NotificationTest, BasicNotificationTestWithoutPayload) { +TEST_F(NotificationTest, BasicNotificationTestWithoutPayload) { // NOLINT + constexpr uint16_t RESOURCE1_ID = 0x8000; + constexpr uint16_t RESOURCE2_ID = 0x8001; zenoh::init_log_from_env_or("error"); auto transport = getTransport(); - auto source = getUUri(0x8000); + auto source = getUUri(RESOURCE1_ID); auto sink = getUUri(0); auto source_filter = source; - constexpr int num_messages = 25; + constexpr int NUM_MESSAGES = 25; - auto notificationSource = communication::NotificationSource( + auto notification_source = communication::NotificationSource( transport, std::move(source), std::move(sink)); // Create the intended sink for the notifications @@ -119,29 +124,29 @@ TEST_F(NotificationTest, BasicNotificationTestWithoutPayload) { std::queue rx_queue; auto on_rx = [&rx_queue_mtx, &rx_queue](const v1::UMessage& message) { std::lock_guard lock(rx_queue_mtx); - rx_queue.push(std::move(message)); + rx_queue.push(message); }; auto maybe_sink = communication::NotificationSink::create( - transport, std::move(on_rx), std::move(source_filter)); + transport, std::move(on_rx), source_filter); EXPECT_TRUE(maybe_sink.has_value()); // Create a second sink with a different source filter to verify messages // arrive at the right sink - auto source_filter2 = getUUri(0x8001); - auto on_rx2 = [](const v1::UMessage& message) { FAIL(); }; + auto source_filter2 = getUUri(RESOURCE2_ID); + auto on_rx2 = [](const v1::UMessage& /*message*/) { FAIL(); }; auto maybe_sink2 = communication::NotificationSink::create( - transport, std::move(on_rx2), std::move(source_filter2)); + transport, std::move(on_rx2), source_filter2); EXPECT_TRUE(maybe_sink2.has_value()); // Send the notification messages if (maybe_sink.has_value()) { - for (auto remaining = num_messages; remaining > 0; --remaining) { - auto status = notificationSource.notify(); + for (auto remaining = NUM_MESSAGES; remaining > 0; --remaining) { + auto status = notification_source.notify(); EXPECT_EQ(status.code(), v1::UCode::OK); } } - EXPECT_EQ(rx_queue.size(), num_messages); + EXPECT_EQ(rx_queue.size(), NUM_MESSAGES); } -} // namespace +} // namespace uprotocol diff --git a/test/extra/PublisherSubscriberTest.cpp b/test/extra/PublisherSubscriberTest.cpp index 8dc9308c..4baad174 100644 --- a/test/extra/PublisherSubscriberTest.cpp +++ b/test/extra/PublisherSubscriberTest.cpp @@ -17,10 +17,9 @@ #include "up-transport-zenoh-cpp/ZenohUTransport.h" -namespace { -constexpr size_t num_publish_messages = 25; +constexpr size_t NUM_PUBLISH_MESSAGES = 25; -using namespace uprotocol; +namespace uprotocol { constexpr std::string_view ZENOH_CONFIG_FILE = BUILD_REALPATH_ZENOH_CONF; @@ -38,18 +37,21 @@ class PublisherSubscriberTest : public testing::Test { // Run once per execution of the test application. // Used for setup of all tests. Has access to this instance. PublisherSubscriberTest() { zenoh::init_log_from_env_or("error"); } - ~PublisherSubscriberTest() = default; // Run once per execution of the test application. // Used only for global setup outside of tests. static void SetUpTestSuite() {} static void TearDownTestSuite() {} + +public: + ~PublisherSubscriberTest() override = default; }; v1::UUri makeUUri(uint16_t resource_id) { + constexpr uint32_t DEFAULT_UE_ID = 0x10001; v1::UUri uuri; uuri.set_authority_name(static_cast("test0")); - uuri.set_ue_id((0x10001)); + uuri.set_ue_id((DEFAULT_UE_ID)); uuri.set_ue_version_major(1); uuri.set_resource_id(resource_id); return uuri; @@ -85,7 +87,7 @@ void ValidateMessages(std::queue& rx_queue, size_t num_messages, // TODO(sashacmc): config generation -TEST_F(PublisherSubscriberTest, SinglePubSingleSub) { +TEST_F(PublisherSubscriberTest, SinglePubSingleSub) { // NOLINT auto transport = getTransport(); communication::Publisher pub(transport, makeUUri(TOPIC_URI), @@ -103,7 +105,7 @@ TEST_F(PublisherSubscriberTest, SinglePubSingleSub) { EXPECT_TRUE(maybe_sub); if (maybe_sub) { - for (auto remaining = num_publish_messages; remaining > 0; + for (auto remaining = NUM_PUBLISH_MESSAGES; remaining > 0; --remaining) { std::ostringstream message; message << "Message number: " << remaining; @@ -115,11 +117,11 @@ TEST_F(PublisherSubscriberTest, SinglePubSingleSub) { } } - ValidateMessages(rx_queue, num_publish_messages, "Message number: "); + ValidateMessages(rx_queue, NUM_PUBLISH_MESSAGES, "Message number: "); } // Single publisher, multiple subscribers (2) on the same topic. -TEST_F(PublisherSubscriberTest, SinglePubMultipleSub) { +TEST_F(PublisherSubscriberTest, SinglePubMultipleSub) { // NOLINT auto transport = getTransport(); communication::Publisher pub(transport, makeUUri(TOPIC_URI), @@ -148,7 +150,7 @@ TEST_F(PublisherSubscriberTest, SinglePubMultipleSub) { EXPECT_TRUE(maybe_sub2); if (maybe_sub && maybe_sub2) { - for (auto remaining = num_publish_messages; remaining > 0; + for (auto remaining = NUM_PUBLISH_MESSAGES; remaining > 0; --remaining) { std::ostringstream message; message << "Message number: " << remaining; @@ -160,12 +162,13 @@ TEST_F(PublisherSubscriberTest, SinglePubMultipleSub) { } } - ValidateMessages(rx_queue, num_publish_messages, "Message number: "); - ValidateMessages(rx_queue2, num_publish_messages, "Message number: "); + ValidateMessages(rx_queue, NUM_PUBLISH_MESSAGES, "Message number: "); + ValidateMessages(rx_queue2, NUM_PUBLISH_MESSAGES, "Message number: "); } // Single publisher, two subscribers on different topics -TEST_F(PublisherSubscriberTest, SinglePubMultipleSubDifferentTopics) { +TEST_F(PublisherSubscriberTest, // NOLINT + SinglePubMultipleSubDifferentTopics) { auto transport = getTransport(); communication::Publisher pub(transport, makeUUri(TOPIC_URI), @@ -183,13 +186,13 @@ TEST_F(PublisherSubscriberTest, SinglePubMultipleSubDifferentTopics) { EXPECT_TRUE(maybe_sub); // subscribe to a different topic (non-existent topic) - auto on_rx2 = [](const v1::UMessage& message) { FAIL(); }; + auto on_rx2 = [](const v1::UMessage& /*message*/) { FAIL(); }; auto maybe_sub2 = communication::Subscriber::subscribe( transport, makeUUri(TOPIC_URI2), std::move(on_rx2)); EXPECT_TRUE(maybe_sub2); if (maybe_sub && maybe_sub2) { - for (auto remaining = num_publish_messages; remaining > 0; + for (auto remaining = NUM_PUBLISH_MESSAGES; remaining > 0; --remaining) { std::ostringstream message; message << "Message number: " << remaining; @@ -201,11 +204,12 @@ TEST_F(PublisherSubscriberTest, SinglePubMultipleSubDifferentTopics) { } } - ValidateMessages(rx_queue, num_publish_messages, "Message number: "); + ValidateMessages(rx_queue, NUM_PUBLISH_MESSAGES, "Message number: "); } // Two publishers, two subscribers, two topics -TEST_F(PublisherSubscriberTest, MultiplePubMultipleSubDifferentTopics) { +TEST_F(PublisherSubscriberTest, // NOLINT + MultiplePubMultipleSubDifferentTopics) { auto transport = getTransport(); communication::Publisher pub(transport, makeUUri(TOPIC_URI), @@ -236,7 +240,7 @@ TEST_F(PublisherSubscriberTest, MultiplePubMultipleSubDifferentTopics) { EXPECT_TRUE(maybe_sub2); if (maybe_sub && maybe_sub2) { - for (auto remaining = num_publish_messages; remaining > 0; + for (auto remaining = NUM_PUBLISH_MESSAGES; remaining > 0; --remaining) { std::ostringstream message; message << "Pub 1 - Message number: " << remaining; @@ -254,10 +258,10 @@ TEST_F(PublisherSubscriberTest, MultiplePubMultipleSubDifferentTopics) { } } - ValidateMessages(rx_queue, num_publish_messages, + ValidateMessages(rx_queue, NUM_PUBLISH_MESSAGES, "Pub 1 - Message number: "); - ValidateMessages(rx_queue2, num_publish_messages, + ValidateMessages(rx_queue2, NUM_PUBLISH_MESSAGES, "Pub 2 - Message number: "); } -} // namespace +} // namespace uprotocol diff --git a/test/extra/RpcClientServerTest.cpp b/test/extra/RpcClientServerTest.cpp index b7188a6d..ab9e438e 100644 --- a/test/extra/RpcClientServerTest.cpp +++ b/test/extra/RpcClientServerTest.cpp @@ -17,26 +17,50 @@ #include #include #include +#include -#include +#include using namespace std::chrono_literals; -namespace { - -using namespace uprotocol::v1; +namespace uprotocol::v1 { using uprotocol::communication::RpcClient; using uprotocol::communication::RpcServer; constexpr std::string_view ZENOH_CONFIG_FILE = BUILD_REALPATH_ZENOH_CONF; +struct UeDetails { + uint32_t ue_id; + uint32_t ue_version_major; +}; + struct MyUUri { - std::string auth = ""; - uint32_t ue_id = 0x8000; - uint32_t ue_version_major = 1; - uint32_t resource_id = 1; + static constexpr uint32_t DEFAULT_UE_ID = 0x8000; + +public: + MyUUri(std::string auth_val, UeDetails ue_details, uint32_t resource_id_val) + : auth(std::move(auth_val)), + ue_id(ue_details.ue_id), + ue_version_major(ue_details.ue_version_major), + resource_id(resource_id_val) {} + + void set_auth(const std::string& auth_val) { auth = auth_val; } + [[nodiscard]] const std::string& get_auth() const { return auth; } + + void set_ue_details(UeDetails ue_details) { + ue_id = ue_details.ue_id; + ue_version_major = ue_details.ue_version_major; + } - operator uprotocol::v1::UUri() const { + [[nodiscard]] uint32_t get_ue_id() const { return ue_id; } + [[nodiscard]] uint32_t get_ue_version_major() const { + return ue_version_major; + } + + void set_resource_id(uint32_t resource) { resource_id = resource; } + [[nodiscard]] uint32_t get_resource_id() const { return resource_id; } + + explicit operator uprotocol::v1::UUri() const { UUri ret; ret.set_authority_name(auth); ret.set_ue_id(ue_id); @@ -45,13 +69,16 @@ struct MyUUri { return ret; } - std::string to_string() const { + [[nodiscard]] std::string to_string() const { return std::string("<< ") + UUri(*this).ShortDebugString() + " >>"; } -}; -const MyUUri rpc_service_uuri{"me_authority", 65538, 1, 32600}; -const MyUUri ident{"me_authority", 65538, 1, 0}; +private: + std::string auth; + uint32_t ue_id = DEFAULT_UE_ID; + uint32_t ue_version_major = 1; + uint32_t resource_id = 1; +}; class RpcClientServerTest : public testing::Test { protected: @@ -61,7 +88,9 @@ class RpcClientServerTest : public testing::Test { // Run once per TEST_F. // Used to set up clean environments per test. void SetUp() override { - transport_ = std::make_shared(ident, ZENOH_CONFIG_FILE); + const MyUUri ident{"me_authority", {65538, 1}, 0}; + transport_ = std::make_shared(static_cast(ident), + ZENOH_CONFIG_FILE); EXPECT_NE(nullptr, transport_); } @@ -70,15 +99,18 @@ class RpcClientServerTest : public testing::Test { // Run once per execution of the test application. // Used for setup of all tests. Has access to this instance. RpcClientServerTest() = default; - ~RpcClientServerTest() = default; // Run once per execution of the test application. // Used only for global setup outside of tests. static void SetUpTestSuite() {} static void TearDownTestSuite() {} + +public: + ~RpcClientServerTest() override = default; }; TEST_F(RpcClientServerTest, SimpleRoundTrip) { // NOLINT + const MyUUri rpc_service_uuri{"me_authority", {65538, 1}, 32600}; std::string client_request{"RPC Request"}; // NOLINT uprotocol::datamodel::builder::Payload client_request_payload( client_request, UPayloadFormat::UPAYLOAD_FORMAT_TEXT); @@ -92,8 +124,8 @@ TEST_F(RpcClientServerTest, SimpleRoundTrip) { // NOLINT server_response, UPayloadFormat::UPAYLOAD_FORMAT_TEXT); auto server_or_status = RpcServer::create( - transport_, rpc_service_uuri, - [this, &server_called, &server_capture, + transport_, v1::UUri(rpc_service_uuri), + [&server_called, &server_capture, &server_response_payload](const UMessage& message) { server_called = true; server_capture = message; @@ -103,14 +135,14 @@ TEST_F(RpcClientServerTest, SimpleRoundTrip) { // NOLINT ASSERT_TRUE(server_or_status.has_value()); ASSERT_NE(server_or_status.value(), nullptr); - auto client = RpcClient(transport_, rpc_service_uuri, + auto client = RpcClient(transport_, v1::UUri(rpc_service_uuri), UPriority::UPRIORITY_CS4, 1000ms); uprotocol::communication::RpcClient::InvokeHandle client_handle; // NOLINT - EXPECT_NO_THROW( + EXPECT_NO_THROW( // NOLINT client_handle = client.invokeMethod( std::move(client_request_payload), - [this, &client_called, &client_capture](auto maybe_response) { + [&client_called, &client_capture](const auto& maybe_response) { client_called = true; if (maybe_response.has_value()) { client_capture = maybe_response.value(); @@ -123,4 +155,4 @@ TEST_F(RpcClientServerTest, SimpleRoundTrip) { // NOLINT EXPECT_EQ(server_response, client_capture.payload()); } -} // namespace +} // namespace uprotocol::v1