Enable flag to disable instant on lost

PiperOrigin-RevId: 740501388
This commit is contained in:
Guogang Li
2025-03-25 15:15:37 -07:00
committed by Copybara-Service
parent d9a54115ee
commit df2ddd0f20
2 changed files with 80 additions and 72 deletions
@@ -85,7 +85,7 @@ constexpr auto kUseStableEndpointId =
flags::Flag<bool>(kConfigPackage, "45639298", false);
// When true, disable instant on lost on BLE without extended feature.
constexpr auto kDisableInstantOnLostOnBleWithoutExtended =
flags::Flag<bool>(kConfigPackage, "45687098", false);
flags::Flag<bool>(kConfigPackage, "45687098", true);
} // namespace nearby_connections_feature
} // namespace config_package_nearby
@@ -14,6 +14,7 @@
#include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h"
#include <memory>
#include <string>
#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<InstantOnLostManager>();
}
void TearDown() override { instant_on_lost_manager_->Shutdown(); }
InstantOnLostManager& instant_on_lost_manager() {
return *instant_on_lost_manager_;
}
private:
std::unique_ptr<InstantOnLostManager> 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