Added flag for BLE v2 in nearby connections

PiperOrigin-RevId: 516585139
This commit is contained in:
Guogang Li
2023-03-14 11:35:49 -07:00
committed by Copybara-Service
parent ecac59ef2b
commit e539d021a3
15 changed files with 484 additions and 22 deletions
+55
View File
@@ -0,0 +1,55 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
licenses(["notice"])
cc_library(
name = "connections_flags",
srcs = [
"connections_flags.cc",
],
hdrs = [
"connections_flags.h",
"nearby_connections_feature_flags.h",
],
copts = [
"-Ithird_party",
],
visibility = [
"//connections:__subpackages__",
"//internal/platform:__subpackages__",
"//location/nearby/cpp:__subpackages__",
],
deps = [
"//internal/flags:flag_reader",
"//internal/platform:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
],
)
cc_test(
name = "connections_flags_test",
srcs = ["connections_flags_test.cc"],
deps = [
":connections_flags",
"//internal/flags:flag_reader",
"//internal/platform/implementation/g3",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
@@ -0,0 +1,125 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "connections/implementation/flags/connections_flags.h"
#include <string>
#include "internal/platform/mutex.h"
#include "internal/platform/mutex_lock.h"
namespace nearby {
namespace connections {
ConnectionsFlags& ConnectionsFlags::GetInstance() {
static ConnectionsFlags* sharing_flags = new ConnectionsFlags();
return *sharing_flags;
}
bool ConnectionsFlags::GetBoolFlag(const flags::Flag<bool>& flag) {
MutexLock lock(&mutex_);
const auto& it = overrided_bool_flag_values_.find(flag.name());
if (it != overrided_bool_flag_values_.end()) {
return it->second;
}
if (flag_reader_ != nullptr) {
return flag_reader_->GetBoolFlag(flag);
}
return default_flag_reader_.GetBoolFlag(flag);
}
int64_t ConnectionsFlags::GetInt64Flag(const flags::Flag<int64_t>& flag) {
MutexLock lock(&mutex_);
const auto& it = overrided_int64_flag_values_.find(flag.name());
if (it != overrided_int64_flag_values_.end()) {
return it->second;
}
if (flag_reader_ != nullptr) {
return flag_reader_->GetInt64Flag(flag);
}
return default_flag_reader_.GetInt64Flag(flag);
}
double ConnectionsFlags::GetDoubleFlag(const flags::Flag<double>& flag) {
MutexLock lock(&mutex_);
const auto& it = overrided_double_flag_values_.find(flag.name());
if (it != overrided_double_flag_values_.end()) {
return it->second;
}
if (flag_reader_ != nullptr) {
return flag_reader_->GetDoubleFlag(flag);
}
return default_flag_reader_.GetDoubleFlag(flag);
}
std::string ConnectionsFlags::GetStringFlag(
const flags::Flag<absl::string_view>& flag) {
MutexLock lock(&mutex_);
const auto& it = overrided_string_flag_values_.find(flag.name());
if (it != overrided_string_flag_values_.end()) {
return it->second;
}
if (flag_reader_ != nullptr) {
return flag_reader_->GetStringFlag(flag);
}
return default_flag_reader_.GetStringFlag(flag);
}
void ConnectionsFlags::SetFlagReader(flags::FlagReader& flag_reader) {
MutexLock lock(&mutex_);
flag_reader_ = &flag_reader;
}
void ConnectionsFlags::OverrideBoolFlagValue(const flags::Flag<bool>& flag,
bool new_value) {
MutexLock lock(&mutex_);
overrided_bool_flag_values_[flag.name()] = new_value;
}
void ConnectionsFlags::OverrideInt64FlagValue(const flags::Flag<int64_t>& flag,
int64_t new_value) {
MutexLock lock(&mutex_);
overrided_int64_flag_values_[flag.name()] = new_value;
}
void ConnectionsFlags::OverrideDoubleFlagValue(const flags::Flag<double>& flag,
double new_value) {
MutexLock lock(&mutex_);
overrided_double_flag_values_[flag.name()] = new_value;
}
void ConnectionsFlags::OverrideStringFlagValue(
const flags::Flag<absl::string_view>& flag, absl::string_view new_value) {
MutexLock lock(&mutex_);
overrided_string_flag_values_[flag.name()] = std::string(new_value);
}
void ConnectionsFlags::ResetOverridedValues() {
MutexLock lock(&mutex_);
overrided_bool_flag_values_.clear();
overrided_int64_flag_values_.clear();
overrided_double_flag_values_.clear();
overrided_string_flag_values_.clear();
}
} // namespace connections
} // namespace nearby
@@ -0,0 +1,96 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_
#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "internal/flags/default_flag_reader.h"
#include "internal/flags/flag_reader.h"
#include "internal/platform/mutex.h"
namespace nearby {
namespace connections {
class ConnectionsFlags : public nearby::flags::FlagReader {
public:
~ConnectionsFlags() override = default;
static ConnectionsFlags& GetInstance();
// Reads flag with boolean value.
bool GetBoolFlag(const flags::Flag<bool>& flag) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Reads flag with int64_t value.
int64_t GetInt64Flag(const flags::Flag<int64_t>& flag) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Reads flag with double value.
double GetDoubleFlag(const flags::Flag<double>& flag) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Reads flag with string value.
std::string GetStringFlag(const flags::Flag<absl::string_view>& flag) override
ABSL_LOCKS_EXCLUDED(mutex_);
void SetFlagReader(flags::FlagReader& flag_reader)
ABSL_LOCKS_EXCLUDED(mutex_);
// Override the default value of the flags. The major purpose of the method is
// for test.
void OverrideBoolFlagValue(const flags::Flag<bool>& flag, bool new_value)
ABSL_LOCKS_EXCLUDED(mutex_);
void OverrideInt64FlagValue(const flags::Flag<int64_t>& flag,
int64_t new_value) ABSL_LOCKS_EXCLUDED(mutex_);
void OverrideDoubleFlagValue(const flags::Flag<double>& flag,
double new_value) ABSL_LOCKS_EXCLUDED(mutex_);
void OverrideStringFlagValue(const flags::Flag<absl::string_view>& flag,
absl::string_view new_value)
ABSL_LOCKS_EXCLUDED(mutex_);
// Reset all overrided values.
void ResetOverridedValues() ABSL_LOCKS_EXCLUDED(mutex_);
private:
ConnectionsFlags() = default;
flags::FlagReader* flag_reader_ = nullptr;
flags::DefaultFlagReader default_flag_reader_;
mutable Mutex mutex_;
absl::flat_hash_map<std::string, bool> overrided_bool_flag_values_
ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<std::string, int64_t> overrided_int64_flag_values_
ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<std::string, double> overrided_double_flag_values_
ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<std::string, std::string> overrided_string_flag_values_
ABSL_GUARDED_BY(mutex_);
};
} // namespace connections
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_CONNECTIONS_FLAGS_H_
@@ -0,0 +1,121 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "connections/implementation/flags/connections_flags.h"
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/flags/flag.h"
#include "internal/flags/flag_reader.h"
namespace nearby {
namespace connections {
namespace {
constexpr auto kTestBoolFlag =
flags::Flag<bool>("test_package", "45401515", false);
constexpr auto kTestInt64Flag =
flags::Flag<int64_t>("test_package", "45401516", 666);
constexpr auto kTestDoubleFlag =
flags::Flag<double>("test_package", "45401517", 66.66);
constexpr auto kTestStringFlag =
flags::Flag<absl::string_view>("test_package", "45401518", "string");
constexpr bool kTestBoolFlagTestValue = true;
constexpr int64_t kTestInt64FlagTestValue = 666;
constexpr double kTestDoubleFlagTestValue = 88.8;
constexpr std::string_view kTestStringFlagTestValue = "hello";
class MockFlagReader : public flags::FlagReader {
public:
MOCK_METHOD(bool, GetBoolFlag, (const flags::Flag<bool>& flag), (override));
MOCK_METHOD(int64_t, GetInt64Flag, (const flags::Flag<int64_t>& flag),
(override));
MOCK_METHOD(double, GetDoubleFlag, (const flags::Flag<double>& flag),
(override));
MOCK_METHOD(std::string, GetStringFlag,
(const flags::Flag<absl::string_view>& flag), (override));
};
TEST(ConnectionsFlags, GetDefaultValues) {
EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag),
kTestBoolFlag.default_value());
EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag),
kTestInt64Flag.default_value());
EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag),
kTestDoubleFlag.default_value());
EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag),
kTestStringFlag.default_value());
}
TEST(ConnectionsFlags, OverrideDefaultValues) {
ConnectionsFlags::GetInstance().OverrideBoolFlagValue(kTestBoolFlag,
kTestBoolFlagTestValue);
EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag),
kTestBoolFlagTestValue);
ConnectionsFlags::GetInstance().OverrideInt64FlagValue(
kTestInt64Flag, kTestInt64FlagTestValue);
EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag),
kTestInt64FlagTestValue);
ConnectionsFlags::GetInstance().OverrideDoubleFlagValue(
kTestDoubleFlag, kTestDoubleFlagTestValue);
EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag),
kTestDoubleFlagTestValue);
ConnectionsFlags::GetInstance().OverrideStringFlagValue(
kTestStringFlag, kTestStringFlagTestValue);
EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag),
kTestStringFlagTestValue);
ConnectionsFlags::GetInstance().ResetOverridedValues();
}
TEST(ConnectionsFlags, SetFlagReader) {
auto flag_reader = std::make_unique<::testing::NiceMock<MockFlagReader>>();
ConnectionsFlags::GetInstance().SetFlagReader(*flag_reader.get());
EXPECT_CALL(*flag_reader, GetBoolFlag(::testing::_))
.WillOnce(::testing::Invoke([=](const flags::Flag<bool>& flag) {
return kTestBoolFlagTestValue;
}));
EXPECT_EQ(ConnectionsFlags::GetInstance().GetBoolFlag(kTestBoolFlag),
kTestBoolFlagTestValue);
EXPECT_CALL(*flag_reader, GetInt64Flag(::testing::_))
.WillOnce(::testing::Invoke([=](const flags::Flag<int64_t>& flag) {
return kTestInt64FlagTestValue;
}));
EXPECT_EQ(ConnectionsFlags::GetInstance().GetInt64Flag(kTestInt64Flag),
kTestInt64FlagTestValue);
EXPECT_CALL(*flag_reader, GetDoubleFlag(::testing::_))
.WillOnce(::testing::Invoke([=](const flags::Flag<double>& flag) {
return kTestDoubleFlagTestValue;
}));
EXPECT_EQ(ConnectionsFlags::GetInstance().GetDoubleFlag(kTestDoubleFlag),
kTestDoubleFlagTestValue);
EXPECT_CALL(*flag_reader, GetStringFlag(::testing::_))
.WillOnce(
::testing::Invoke([=](const flags::Flag<absl::string_view>& flag) {
return std::string(kTestStringFlagTestValue);
}));
EXPECT_EQ(ConnectionsFlags::GetInstance().GetStringFlag(kTestStringFlag),
kTestStringFlagTestValue);
}
} // namespace
} // namespace connections
} // namespace nearby
@@ -0,0 +1,42 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_
#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_
#include "absl/strings/string_view.h"
#include "internal/flags/flag.h"
namespace nearby {
namespace connections {
namespace config_package_nearby {
#ifndef PHENOTYPE_CONFIG_PACKAGE_NEARBY
#define PHENOTYPE_CONFIG_PACKAGE_NEARBY
constexpr absl::string_view kConfigPackage = "nearby";
#endif // PHENOTYPE_CONFIG_PACKAGE_NEARBY
// The Nearby Connections features.
namespace nearby_connections_feature {
// Disable/Enable BLE v2 in Nearby Connections SDK.
constexpr auto kEnableBleV2 =
flags::Flag<bool>(kConfigPackage, "45401515", false);
} // namespace nearby_connections_feature
} // namespace config_package_nearby
} // namespace connections
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_FLAGS_NEARBY_CONNECTIONS_FEATURE_FLAGS_H_