|
| 1 | +// Copyright 2026 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <atomic> |
| 16 | +#include <cstdint> |
| 17 | + |
| 18 | +#include <cpp_utils/testing/gtest_aux.hpp> |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <cpp_utils/exception/InconsistencyException.hpp> |
| 22 | +#include <cpp_utils/time/time_utils.hpp> |
| 23 | + |
| 24 | +#include <ddspipe_core/dynamic/DiscoveryDatabase.hpp> |
| 25 | +#include <ddspipe_core/testing/random_values.hpp> |
| 26 | + |
| 27 | +using namespace eprosima::ddspipe::core; |
| 28 | +using namespace eprosima::ddspipe::core::testing; |
| 29 | + |
| 30 | +namespace test { |
| 31 | + |
| 32 | +constexpr uint32_t WAIT_TIME_MS = 50; |
| 33 | + |
| 34 | +std::map<types::Guid, types::Endpoint> get_all_endpoints( |
| 35 | + const DiscoveryDatabase& discovery_database) |
| 36 | +{ |
| 37 | + return discovery_database.get_endpoints( |
| 38 | + []( |
| 39 | + const types::Endpoint&) |
| 40 | + { |
| 41 | + return true; |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace test |
| 46 | + |
| 47 | +/** |
| 48 | + * Test that updating an endpoint as inactive removes it from the discovery database |
| 49 | + * |
| 50 | + * STEPS: |
| 51 | + * - add one active endpoint |
| 52 | + * - verify it is stored in the database |
| 53 | + * - update the same endpoint as inactive |
| 54 | + * - verify it is erased, the erase callback is triggered, and further access fails |
| 55 | + */ |
| 56 | +TEST(DiscoveryDatabaseTest, inactive_update_erases_endpoint) |
| 57 | +{ |
| 58 | + DiscoveryDatabase discovery_database; |
| 59 | + discovery_database.start(); |
| 60 | + |
| 61 | + auto endpoint = random_endpoint(5); |
| 62 | + endpoint.active = true; |
| 63 | + |
| 64 | + std::atomic<uint32_t> updated_callbacks{0}; |
| 65 | + std::atomic<uint32_t> erased_callbacks{0}; |
| 66 | + std::atomic<bool> erased_same_guid{false}; |
| 67 | + |
| 68 | + discovery_database.add_endpoint_updated_callback( |
| 69 | + [&]( |
| 70 | + types::Endpoint) |
| 71 | + { |
| 72 | + ++updated_callbacks; |
| 73 | + }); |
| 74 | + |
| 75 | + discovery_database.add_endpoint_erased_callback( |
| 76 | + [&]( |
| 77 | + types::Endpoint erased_endpoint) |
| 78 | + { |
| 79 | + erased_same_guid.store(erased_endpoint.guid == endpoint.guid); |
| 80 | + ++erased_callbacks; |
| 81 | + }); |
| 82 | + |
| 83 | + discovery_database.add_endpoint(endpoint); |
| 84 | + eprosima::utils::sleep_for(test::WAIT_TIME_MS); |
| 85 | + |
| 86 | + ASSERT_TRUE(discovery_database.endpoint_exists(endpoint.guid)); |
| 87 | + ASSERT_EQ(test::get_all_endpoints(discovery_database).size(), 1u); |
| 88 | + |
| 89 | + endpoint.active = false; |
| 90 | + discovery_database.update_endpoint(endpoint); |
| 91 | + eprosima::utils::sleep_for(test::WAIT_TIME_MS); |
| 92 | + |
| 93 | + EXPECT_FALSE(discovery_database.endpoint_exists(endpoint.guid)); |
| 94 | + EXPECT_TRUE(test::get_all_endpoints(discovery_database).empty()); |
| 95 | + EXPECT_EQ(updated_callbacks.load(), 0u); |
| 96 | + EXPECT_EQ(erased_callbacks.load(), 1u); |
| 97 | + EXPECT_TRUE(erased_same_guid.load()); |
| 98 | + EXPECT_THROW(discovery_database.get_endpoint(endpoint.guid), eprosima::utils::InconsistencyException); |
| 99 | + |
| 100 | + discovery_database.stop(); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Test that stopping the discovery database clears stored endpoints and filtered endpoints |
| 105 | + * |
| 106 | + * STEPS: |
| 107 | + * - add one active endpoint and one filtered endpoint |
| 108 | + * - verify both are stored |
| 109 | + * - stop the discovery database |
| 110 | + * - verify both entries are removed from the internal state |
| 111 | + */ |
| 112 | +TEST(DiscoveryDatabaseTest, stop_clears_stored_endpoints) |
| 113 | +{ |
| 114 | + DiscoveryDatabase discovery_database; |
| 115 | + discovery_database.start(); |
| 116 | + |
| 117 | + auto endpoint = random_endpoint(7); |
| 118 | + endpoint.active = true; |
| 119 | + auto filtered_guid = random_guid(11); |
| 120 | + |
| 121 | + discovery_database.add_endpoint(endpoint); |
| 122 | + discovery_database.add_filtered_endpoint(filtered_guid); |
| 123 | + eprosima::utils::sleep_for(test::WAIT_TIME_MS); |
| 124 | + |
| 125 | + ASSERT_TRUE(discovery_database.endpoint_exists(endpoint.guid)); |
| 126 | + ASSERT_TRUE(discovery_database.exists_filtered_endpoint(filtered_guid)); |
| 127 | + |
| 128 | + discovery_database.stop(); |
| 129 | + |
| 130 | + EXPECT_FALSE(discovery_database.endpoint_exists(endpoint.guid)); |
| 131 | + EXPECT_FALSE(discovery_database.exists_filtered_endpoint(filtered_guid)); |
| 132 | + EXPECT_TRUE(test::get_all_endpoints(discovery_database).empty()); |
| 133 | +} |
| 134 | + |
| 135 | +int main( |
| 136 | + int argc, |
| 137 | + char** argv) |
| 138 | +{ |
| 139 | + ::testing::InitGoogleTest(&argc, argv); |
| 140 | + return RUN_ALL_TESTS(); |
| 141 | +} |
0 commit comments