Merge branch 'google3' to master, roll forward up to cl/355302206.

This commit is contained in:
hai007
2021-02-04 13:34:05 -08:00
41 changed files with 1194 additions and 472 deletions
+3
View File
@@ -176,7 +176,10 @@ cc_test(
"cancellation_flag_test.cc",
],
deps = [
":base",
":cancellation_flag",
":test_util",
"//platform/impl/g3", # build_cleaner: keep
"//testing/base/public:gunit_main",
],
)
+12
View File
@@ -1,5 +1,7 @@
#include "platform/base/cancellation_flag.h"
#include "platform/base/feature_flags.h"
namespace location {
namespace nearby {
@@ -15,6 +17,11 @@ CancellationFlag::CancellationFlag(bool cancelled) {
void CancellationFlag::Cancel() {
absl::MutexLock lock(mutex_.get());
// Return immediately as no-op if feature flag is not enabled.
if (!FeatureFlags::GetInstance().GetFlags().enable_cancellation_flag) {
return;
}
if (cancelled_) {
// Someone already cancelled. Return immediately.
return;
@@ -25,6 +32,11 @@ void CancellationFlag::Cancel() {
bool CancellationFlag::Cancelled() const {
absl::MutexLock lock(mutex_.get());
// Return falsea as no-op if feature flag is not enabled.
if (!FeatureFlags::GetInstance().GetFlags().enable_cancellation_flag) {
return false;
}
return cancelled_;
}
+55 -4
View File
@@ -1,20 +1,71 @@
#include "platform/base/cancellation_flag.h"
#include "platform/base/feature_flags.h"
#include "platform/base/medium_environment.h"
#include "gtest/gtest.h"
namespace location {
namespace nearby {
namespace {
TEST(CancellationFlagTest, InitialValueIsFalse) {
using FeatureFlags = FeatureFlags::Flags;
constexpr FeatureFlags kTestCases[] = {
FeatureFlags{
.enable_cancellation_flag = true,
},
FeatureFlags{
.enable_cancellation_flag = false,
},
};
class CancellationFlagTest : public ::testing::TestWithParam<FeatureFlags> {
protected:
void SetUp() override {
feature_flags_ = GetParam();
env_.SetFeatureFlags(feature_flags_);
}
FeatureFlags feature_flags_;
MediumEnvironment& env_{MediumEnvironment::Instance()};
};
TEST_P(CancellationFlagTest, InitialValueIsFalse) {
CancellationFlag flag;
// No matter FeatureFlag is enabled or not, Cancelled is always false.
EXPECT_FALSE(flag.Cancelled());
}
TEST(CancellationFlagTest, CanCancel) {
CancellationFlag flag;
flag.Cancel();
TEST_P(CancellationFlagTest, InitialValueAsTrue) {
CancellationFlag flag{true};
// If FeatureFlag is disabled, Cancelled is false as no-op.
if (!feature_flags_.enable_cancellation_flag) {
EXPECT_FALSE(flag.Cancelled());
return;
}
EXPECT_TRUE(flag.Cancelled());
}
TEST_P(CancellationFlagTest, CanCancel) {
CancellationFlag flag;
flag.Cancel();
// If FeatureFlag is disabled, return as no-op immediately and
// Cancelled is always false.
if (!feature_flags_.enable_cancellation_flag) {
EXPECT_FALSE(flag.Cancelled());
return;
}
EXPECT_TRUE(flag.Cancelled());
}
INSTANTIATE_TEST_SUITE_P(ParametrisedCancellationFlagTest, CancellationFlagTest,
::testing::ValuesIn(kTestCases));
} // namespace
} // namespace nearby
} // namespace location
+1 -1
View File
@@ -12,7 +12,7 @@ class FeatureFlags {
public:
// Holds for all the feature flags.
struct Flags {
bool enable_cancellation_flags = false;
bool enable_cancellation_flag = false;
bool resume_before_disconnect = true;
};
+6 -5
View File
@@ -7,18 +7,19 @@ namespace location {
namespace nearby {
namespace {
FeatureFlags::Flags kTestFeatureFlags{.enable_cancellation_flags = true};
constexpr FeatureFlags::Flags kTestFeatureFlags{.enable_cancellation_flag =
true};
TEST(FeatureFlagsTest, ToStringWorks) {
TEST(FeatureFlagsTest, ToSetFeatureWorks) {
const FeatureFlags& features = FeatureFlags::GetInstance();
EXPECT_FALSE(features.GetFlags().enable_cancellation_flags);
EXPECT_FALSE(features.GetFlags().enable_cancellation_flag);
MediumEnvironment& medium_environment = MediumEnvironment::Instance();
medium_environment.SetFeatureFlags(kTestFeatureFlags);
EXPECT_TRUE(features.GetFlags().enable_cancellation_flags);
EXPECT_TRUE(features.GetFlags().enable_cancellation_flag);
const FeatureFlags& another_features_ref = FeatureFlags::GetInstance();
EXPECT_TRUE(another_features_ref.GetFlags().enable_cancellation_flags);
EXPECT_TRUE(another_features_ref.GetFlags().enable_cancellation_flag);
}
} // namespace