Made kPeripheralLostTimeout as flag

PiperOrigin-RevId: 530408275
This commit is contained in:
Guogang Li
2023-05-08 14:29:02 -07:00
committed by Copybara-Service
parent 4eaaeb254d
commit 072d24238b
5 changed files with 29 additions and 8 deletions
@@ -32,6 +32,10 @@ namespace nearby_connections_feature {
constexpr auto kEnableBleV2 =
flags::Flag<bool>(kConfigPackage, "45401515", false);
// The timeout in millis to report peripheral device lost.
constexpr auto kBlePeripheralLostTimeoutMillis =
flags::Flag<int64_t>(kConfigPackage, "45411439", 12000);
// LINT.ThenChange(
// //depot/google3/location/nearby/cpp/sharing/clients/windows/nearby_sharing_service_adapter_dart.h,
// //depot/google3/location/nearby/cpp/sharing/clients/windows/nearby_sharing_service_adapter_dart.cc,
+4
View File
@@ -47,9 +47,11 @@ cc_library(
deps = [
":utils",
"//connections:core_types",
"//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums/ble_v2",
"//connections/implementation/mediums/webrtc",
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//internal/flags:nearby_flags",
"//internal/platform:base",
"//internal/platform:cancellation_flag",
"//internal/platform:comm",
@@ -118,7 +120,9 @@ cc_test(
deps = [
":mediums",
":utils",
"//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums/ble_v2",
"//internal/flags:nearby_flags",
"//internal/platform:base",
"//internal/platform:comm",
"//internal/platform:test_util",
+8 -4
View File
@@ -21,7 +21,9 @@
#include <vector>
#include "absl/strings/escaping.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
@@ -29,6 +31,7 @@
#include "connections/implementation/mediums/bluetooth_radio.h"
#include "connections/implementation/mediums/utils.h"
#include "connections/power_level.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancelable_alarm.h"
#include "internal/platform/logging.h"
@@ -52,9 +55,6 @@ void AssumeHeld(Mutex& m) ABSL_ASSERT_EXCLUSIVE_LOCK(m) {}
} // namespace
// These definitions are necessary before C++17.
constexpr absl::Duration BleV2::kPeripheralLostTimeout;
BleV2::BleV2(BluetoothRadio& radio)
: radio_(radio), adapter_(radio_.GetBluetoothAdapter()) {}
@@ -303,6 +303,10 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
return false;
}
absl::Duration peripheral_lost_timeout =
absl::Milliseconds(NearbyFlags::GetInstance().GetInt64Flag(
config_package_nearby::nearby_connections_feature::
kBlePeripheralLostTimeoutMillis));
// Set up lost alarm.
lost_alarm_ = std::make_unique<CancelableAlarm>(
"BLE.StartScanning() onLost",
@@ -310,7 +314,7 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
MutexLock lock(&mutex_);
discovered_peripheral_tracker_.ProcessLostGattAdvertisements();
},
kPeripheralLostTimeout, &alarm_executor_, /*is_recurring=*/true);
peripheral_lost_timeout, &alarm_executor_, /*is_recurring=*/true);
NEARBY_LOGS(INFO) << "Turned on BLE scanning with service id=" << service_id;
return true;
@@ -55,8 +55,6 @@ class BleV2 final {
accepted_cb = DefaultCallback<BleV2Socket, const std::string&>();
};
static constexpr absl::Duration kPeripheralLostTimeout = absl::Seconds(3);
explicit BleV2(BluetoothRadio& bluetooth_radio);
~BleV2();
@@ -17,8 +17,10 @@
#include <string>
#include "gtest/gtest.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
#include "connections/implementation/mediums/bluetooth_radio.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/medium_environment.h"
@@ -38,6 +40,7 @@ constexpr FeatureFlags kTestCases[] = {
};
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
constexpr int64_t kPeripheralLostTimeoutInMillis = 1000;
constexpr absl::string_view kServiceIDA =
"com.google.location.nearby.apps.test.a";
constexpr absl::string_view kServiceIDB =
@@ -45,6 +48,14 @@ constexpr absl::string_view kServiceIDB =
constexpr absl::string_view kAdvertisementString = "\x0a\x0b\x0c\x0d";
class BleV2Test : public testing::TestWithParam<FeatureFlags> {
public:
void SetUp() override {
NearbyFlags::GetInstance().OverrideInt64FlagValue(
config_package_nearby::nearby_connections_feature::
kBlePeripheralLostTimeoutMillis,
/*ms=*/kPeripheralLostTimeoutInMillis);
}
protected:
MediumEnvironment& env_{MediumEnvironment::Instance()};
};
@@ -427,7 +438,7 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) {
// Wait for a while (2 times delay) to let the alarm occur twice and
// `ProcessLostGattAdvertisements` twice to lost periperal.
SystemClock::Sleep(BleV2::kPeripheralLostTimeout * 2);
SystemClock::Sleep(absl::Milliseconds(kPeripheralLostTimeoutInMillis) * 2);
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
@@ -522,7 +533,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
// Wait for a while (2 times delay) to let the alarm occur twice and
// `ProcessLostGattAdvertisements` twice to lost periperal.
SystemClock::Sleep(BleV2::kPeripheralLostTimeout * 2);
SystemClock::Sleep(absl::Milliseconds(kPeripheralLostTimeoutInMillis) * 2);
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());