From df2ddd0f202ed5aa0839b93881dfc8fd96a1b7da Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Tue, 25 Mar 2025 15:14:28 -0700 Subject: [PATCH] Enable flag to disable instant on lost PiperOrigin-RevId: 740501388 --- .../flags/nearby_connections_feature_flags.h | 2 +- .../ble_v2/instant_on_lost_manager_test.cc | 150 +++++++++--------- 2 files changed, 80 insertions(+), 72 deletions(-) diff --git a/connections/implementation/flags/nearby_connections_feature_flags.h b/connections/implementation/flags/nearby_connections_feature_flags.h index 73af1c2e..8d47327f 100644 --- a/connections/implementation/flags/nearby_connections_feature_flags.h +++ b/connections/implementation/flags/nearby_connections_feature_flags.h @@ -85,7 +85,7 @@ constexpr auto kUseStableEndpointId = flags::Flag(kConfigPackage, "45639298", false); // When true, disable instant on lost on BLE without extended feature. constexpr auto kDisableInstantOnLostOnBleWithoutExtended = - flags::Flag(kConfigPackage, "45687098", false); + flags::Flag(kConfigPackage, "45687098", true); } // namespace nearby_connections_feature } // namespace config_package_nearby diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc b/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc index cc8c4a29..8ce7775b 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc +++ b/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc @@ -14,6 +14,7 @@ #include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h" +#include #include #include "gtest/gtest.h" @@ -21,6 +22,7 @@ #include "absl/time/clock.h" #include "absl/time/time.h" #include "internal/platform/byte_array.h" +#include "internal/platform/medium_environment.h" namespace nearby { namespace connections { @@ -39,109 +41,115 @@ constexpr absl::string_view kData6 = "\x16\x17\x18"; constexpr absl::Duration kOnLostAdvertisingDuration = absl::Milliseconds(2100); -TEST(InstantOnLostManager, StartOnLostAdvertisingAfterStopAdvertising) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +class InstantOnLostManagerTest : public ::testing::Test { + protected: + void SetUp() override { + MediumEnvironment::Instance().SetBleExtendedAdvertisementsAvailable(true); + instant_on_lost_manager_ = std::make_unique(); + } + + void TearDown() override { instant_on_lost_manager_->Shutdown(); } + + InstantOnLostManager& instant_on_lost_manager() { + return *instant_on_lost_manager_; + } + + private: + std::unique_ptr instant_on_lost_manager_; +}; + +TEST_F(InstantOnLostManagerTest, StartOnLostAdvertisingAfterStopAdvertising) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); absl::SleepFor(kOnLostAdvertisingDuration); - EXPECT_FALSE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); + EXPECT_FALSE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); } -TEST(InstantOnLostManager, NoOnLostAdvertisingWhenAdvertiseAgain) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, NoOnLostAdvertisingWhenAdvertiseAgain) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - EXPECT_FALSE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - instant_on_lost_manager.Shutdown(); + EXPECT_FALSE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); } -TEST(InstantOnLostManager, MultipleAdvertisingOnSameServiceId) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, MultipleAdvertisingOnSameServiceId) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 1); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 1); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData2.data(), kData2.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 2); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 2); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData3.data(), kData3.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 3); - instant_on_lost_manager.Shutdown(); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 3); } -TEST(InstantOnLostManager, MaximumOnLostHashesInOnLostAdvertising) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, MaximumOnLostHashesInOnLostAdvertising) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData2.data(), kData2.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData3.data(), kData3.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdB), ByteArray(kData4.data(), kData4.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdB)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdB)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdB), ByteArray(kData5.data(), kData5.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdB)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdB)); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdC), ByteArray(kData6.data(), kData6.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdC)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 5); - instant_on_lost_manager.Shutdown(); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdC)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 5); } -TEST(InstantOnLostManager, ShutdownMultipleTimes) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, ShutdownMultipleTimes) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - EXPECT_TRUE(instant_on_lost_manager.Shutdown()); - EXPECT_FALSE(instant_on_lost_manager.Shutdown()); + EXPECT_TRUE(instant_on_lost_manager().Shutdown()); + EXPECT_FALSE(instant_on_lost_manager().Shutdown()); } -TEST(InstantOnLostManager, AdvertisingOnShutdownManager) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.Shutdown(); - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, AdvertisingOnShutdownManager) { + instant_on_lost_manager().Shutdown(); + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_FALSE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_FALSE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); } -TEST(InstantOnLostManager, RemoveExpiredOnLostAdvertisement) { - InstantOnLostManager instant_on_lost_manager; - instant_on_lost_manager.OnAdvertisingStarted( +TEST_F(InstantOnLostManagerTest, RemoveExpiredOnLostAdvertisement) { + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData1.data(), kData1.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 1); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 1); absl::SleepFor(absl::Milliseconds(1050)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData2.data(), kData2.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 2); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 2); absl::SleepFor(absl::Milliseconds(1050)); - instant_on_lost_manager.OnAdvertisingStarted( + instant_on_lost_manager().OnAdvertisingStarted( std::string(kServiceIdA), ByteArray(kData3.data(), kData3.size())); - instant_on_lost_manager.OnAdvertisingStopped(std::string(kServiceIdA)); - EXPECT_TRUE(instant_on_lost_manager.IsOnLostAdvertisingForTesting()); - EXPECT_EQ(instant_on_lost_manager.GetOnLostHashesForTesting().size(), 2); - instant_on_lost_manager.Shutdown(); + instant_on_lost_manager().OnAdvertisingStopped(std::string(kServiceIdA)); + EXPECT_TRUE(instant_on_lost_manager().IsOnLostAdvertisingForTesting()); + EXPECT_EQ(instant_on_lost_manager().GetOnLostHashesForTesting().size(), 2); } } // namespace