mirror of https://github.com/grpc/grpc.git
More generic configuration system is introduced in order to i) unify the way how modules access the configurations instead of using low-level get/setenv functions and ii) enable the customization for where configuration is stored. This could be extended to support flag, file, etc. Default configuration system uses environment variables as before so basically this is expected to work just as it did. This behavior can change by redefining GPR_GLOBAL_CONFIG_DEFINE_*type* macros. * Migrated configuration GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS GRPC_EXPERIMENTAL_DISABLE_FLOW_CONTROL GRPC_ABORT_ON_LEAKS GRPC_NOT_USE_SYSTEM_SSL_ROOTSpull/18799/head
parent
50b5240d0a
commit
1518ecbd76
44 changed files with 1096 additions and 63 deletions
@ -0,0 +1,87 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stdint.h> |
||||
|
||||
// --------------------------------------------------------------------
|
||||
// How to use global configuration variables:
|
||||
//
|
||||
// Defining config variables of a specified type:
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_*TYPE*(name, default_value, help);
|
||||
//
|
||||
// Supported TYPEs: BOOL, INT32, STRING
|
||||
//
|
||||
// It's recommended to use lowercase letters for 'name' like
|
||||
// regular variables. The builtin configuration system uses
|
||||
// environment variable and the name is converted to uppercase
|
||||
// when looking up the value. For example,
|
||||
// GPR_GLOBAL_CONFIG_DEFINE(grpc_latency) looks up the value with the
|
||||
// name, "GRPC_LATENCY".
|
||||
//
|
||||
// The variable initially has the specified 'default_value'
|
||||
// which must be an expression convertible to 'Type'.
|
||||
// 'default_value' may be evaluated 0 or more times,
|
||||
// and at an unspecified time; keep it
|
||||
// simple and usually free of side-effects.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_*TYPE* should not be called in a C++ header.
|
||||
// It should be called at the top-level (outside any namespaces)
|
||||
// in a .cc file.
|
||||
//
|
||||
// Getting the variables:
|
||||
// GPR_GLOBAL_CONFIG_GET(name)
|
||||
//
|
||||
// If error happens during getting variables, error messages will
|
||||
// be logged and default value will be returned.
|
||||
//
|
||||
// Setting the variables with new value:
|
||||
// GPR_GLOBAL_CONFIG_SET(name, new_value)
|
||||
//
|
||||
// Declaring config variables for other modules to access:
|
||||
// GPR_GLOBAL_CONFIG_DECLARE_*TYPE*(name)
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// How to customize the global configuration system:
|
||||
//
|
||||
// How to read and write configuration value can be customized.
|
||||
// Builtin system uses environment variables but it can be extended to
|
||||
// support command-line flag, file, etc.
|
||||
//
|
||||
// To customize it, following macros should be redefined.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_BOOL
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_INT32
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_STRING
|
||||
//
|
||||
// These macros should define functions for getting and setting variable.
|
||||
// For example, GPR_GLOBAL_CONFIG_DEFINE_BOOL(test, ...) would define two
|
||||
// functions.
|
||||
//
|
||||
// bool gpr_global_config_get_test();
|
||||
// void gpr_global_config_set_test(bool value);
|
||||
|
||||
#include "src/core/lib/gprpp/global_config_env.h" |
||||
|
||||
#include "src/core/lib/gprpp/global_config_custom.h" |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_H */ |
@ -0,0 +1,29 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H |
||||
|
||||
// This is a placeholder for custom global configuration implementaion.
|
||||
// To use the custom one, please define following macros here.
|
||||
//
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_BOOL
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_INT32
|
||||
// GPR_GLOBAL_CONFIG_DEFINE_STRING
|
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_CUSTOM_H */ |
@ -0,0 +1,135 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/global_config_env.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/env.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
#include <ctype.h> |
||||
#include <string.h> |
||||
|
||||
namespace grpc_core { |
||||
|
||||
namespace { |
||||
|
||||
void DefaultGlobalConfigEnvErrorFunction(const char* error_message) { |
||||
gpr_log(GPR_ERROR, "%s", error_message); |
||||
} |
||||
|
||||
GlobalConfigEnvErrorFunctionType g_global_config_env_error_func = |
||||
DefaultGlobalConfigEnvErrorFunction; |
||||
|
||||
void LogParsingError(const char* name, const char* value) { |
||||
char* error_message; |
||||
gpr_asprintf(&error_message, |
||||
"Illegal value '%s' specified for environment variable '%s'", |
||||
value, name); |
||||
(*g_global_config_env_error_func)(error_message); |
||||
gpr_free(error_message); |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func) { |
||||
g_global_config_env_error_func = func; |
||||
} |
||||
|
||||
UniquePtr<char> GlobalConfigEnv::GetValue() { |
||||
return UniquePtr<char>(gpr_getenv(GetName())); |
||||
} |
||||
|
||||
void GlobalConfigEnv::SetValue(const char* value) { |
||||
gpr_setenv(GetName(), value); |
||||
} |
||||
|
||||
void GlobalConfigEnv::Unset() { gpr_unsetenv(GetName()); } |
||||
|
||||
char* GlobalConfigEnv::GetName() { |
||||
// This makes sure that name_ is in a canonical form having uppercase
|
||||
// letters. This is okay to be called serveral times.
|
||||
for (char* c = name_; *c != 0; ++c) { |
||||
*c = toupper(*c); |
||||
} |
||||
return name_; |
||||
} |
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvBool>::value, |
||||
"GlobalConfigEnvBool needs to be trivially destructible."); |
||||
|
||||
bool GlobalConfigEnvBool::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return default_value_; |
||||
} |
||||
// parsing given value string.
|
||||
bool result = false; |
||||
if (!gpr_parse_bool_value(str.get(), &result)) { |
||||
LogParsingError(GetName(), str.get()); |
||||
result = default_value_; |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
void GlobalConfigEnvBool::Set(bool value) { |
||||
SetValue(value ? "true" : "false"); |
||||
} |
||||
|
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvInt32>::value, |
||||
"GlobalConfigEnvInt32 needs to be trivially destructible."); |
||||
|
||||
int32_t GlobalConfigEnvInt32::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return default_value_; |
||||
} |
||||
// parsing given value string.
|
||||
char* end = str.get(); |
||||
long result = strtol(str.get(), &end, 10); |
||||
if (*end != 0) { |
||||
LogParsingError(GetName(), str.get()); |
||||
result = default_value_; |
||||
} |
||||
return static_cast<int32_t>(result); |
||||
} |
||||
|
||||
void GlobalConfigEnvInt32::Set(int32_t value) { |
||||
char buffer[GPR_LTOA_MIN_BUFSIZE]; |
||||
gpr_ltoa(value, buffer); |
||||
SetValue(buffer); |
||||
} |
||||
|
||||
static_assert(std::is_trivially_destructible<GlobalConfigEnvString>::value, |
||||
"GlobalConfigEnvString needs to be trivially destructible."); |
||||
|
||||
UniquePtr<char> GlobalConfigEnvString::Get() { |
||||
UniquePtr<char> str = GetValue(); |
||||
if (str == nullptr) { |
||||
return UniquePtr<char>(gpr_strdup(default_value_)); |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
void GlobalConfigEnvString::Set(const char* value) { SetValue(value); } |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,131 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/global_config_generic.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message); |
||||
|
||||
/*
|
||||
* Set global_config_env_error_function which is called when config system |
||||
* encounters errors such as parsing error. What the default function does |
||||
* is logging error message. |
||||
*/ |
||||
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func); |
||||
|
||||
// Base class for all classes to access environment variables.
|
||||
class GlobalConfigEnv { |
||||
protected: |
||||
// `name` should be writable and alive after constructor is called.
|
||||
constexpr explicit GlobalConfigEnv(char* name) : name_(name) {} |
||||
|
||||
public: |
||||
// Returns the value of `name` variable.
|
||||
UniquePtr<char> GetValue(); |
||||
|
||||
// Sets the value of `name` variable.
|
||||
void SetValue(const char* value); |
||||
|
||||
// Unsets `name` variable.
|
||||
void Unset(); |
||||
|
||||
protected: |
||||
char* GetName(); |
||||
|
||||
private: |
||||
char* name_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvBool : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvBool(char* name, bool default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
bool Get(); |
||||
void Set(bool value); |
||||
|
||||
private: |
||||
bool default_value_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvInt32 : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvInt32(char* name, int32_t default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
int32_t Get(); |
||||
void Set(int32_t value); |
||||
|
||||
private: |
||||
int32_t default_value_; |
||||
}; |
||||
|
||||
class GlobalConfigEnvString : public GlobalConfigEnv { |
||||
public: |
||||
constexpr GlobalConfigEnvString(char* name, const char* default_value) |
||||
: GlobalConfigEnv(name), default_value_(default_value) {} |
||||
|
||||
UniquePtr<char> Get(); |
||||
void Set(const char* value); |
||||
|
||||
private: |
||||
const char* default_value_; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
// Macros for defining global config instances using environment variables.
|
||||
// This defines a GlobalConfig*Type* instance with arguments for
|
||||
// mutable variable name and default value.
|
||||
// Mutable name (g_env_str_##name) is here for having an array
|
||||
// for the canonical name without dynamic allocation.
|
||||
// `help` argument is ignored for this implementation.
|
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
bool gpr_global_config_get_##name() { return g_env_##name.Get(); } \
|
||||
void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); } |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
|
||||
void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); } |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help) \ |
||||
static char g_env_str_##name[] = #name; \
|
||||
static ::grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
|
||||
default_value); \
|
||||
::grpc_core::UniquePtr<char> gpr_global_config_get_##name() { \
|
||||
return g_env_##name.Get(); \
|
||||
} \
|
||||
void gpr_global_config_set_##name(const char* value) { \
|
||||
g_env_##name.Set(value); \
|
||||
} |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */ |
@ -0,0 +1,44 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H |
||||
#define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
#include <stdint.h> |
||||
|
||||
#define GPR_GLOBAL_CONFIG_GET(name) gpr_global_config_get_##name() |
||||
|
||||
#define GPR_GLOBAL_CONFIG_SET(name, value) gpr_global_config_set_##name(value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_BOOL(name) \ |
||||
extern bool gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(bool value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_INT32(name) \ |
||||
extern int32_t gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(int32_t value) |
||||
|
||||
#define GPR_GLOBAL_CONFIG_DECLARE_STRING(name) \ |
||||
extern grpc_core::UniquePtr<char> gpr_global_config_get_##name(); \
|
||||
extern void gpr_global_config_set_##name(const char* value) |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_GENERIC_H */ |
@ -0,0 +1,130 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gpr/env.h" |
||||
#include "src/core/lib/gprpp/global_config_env.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
namespace { |
||||
|
||||
bool g_config_error_function_called; |
||||
|
||||
void ClearConfigErrorCalled() { g_config_error_function_called = false; } |
||||
|
||||
bool IsConfigErrorCalled() { return g_config_error_function_called; } |
||||
|
||||
// This function is for preventing the program from invoking
|
||||
// an error handler due to configuration error and
|
||||
// make test routines know whether there is error.
|
||||
void FakeConfigErrorFunction(const char* error_message) { |
||||
g_config_error_function_called = true; |
||||
} |
||||
|
||||
class GlobalConfigEnvTest : public ::testing::Test { |
||||
protected: |
||||
void SetUp() override { ClearConfigErrorCalled(); } |
||||
void TearDown() override { EXPECT_FALSE(IsConfigErrorCalled()); } |
||||
}; |
||||
|
||||
} // namespace
|
||||
|
||||
GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, true, ""); |
||||
GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 1234, ""); |
||||
GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "Apple", ""); |
||||
|
||||
TEST_F(GlobalConfigEnvTest, BoolWithEnvTest) { |
||||
const char* bool_var_name = "BOOL_VAR"; |
||||
|
||||
gpr_unsetenv(bool_var_name); |
||||
EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var)); |
||||
|
||||
gpr_setenv(bool_var_name, "true"); |
||||
EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var)); |
||||
|
||||
gpr_setenv(bool_var_name, "false"); |
||||
EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var)); |
||||
|
||||
EXPECT_FALSE(IsConfigErrorCalled()); |
||||
|
||||
gpr_setenv(bool_var_name, ""); |
||||
GPR_GLOBAL_CONFIG_GET(bool_var); |
||||
EXPECT_TRUE(IsConfigErrorCalled()); |
||||
ClearConfigErrorCalled(); |
||||
|
||||
gpr_setenv(bool_var_name, "!"); |
||||
GPR_GLOBAL_CONFIG_GET(bool_var); |
||||
EXPECT_TRUE(IsConfigErrorCalled()); |
||||
ClearConfigErrorCalled(); |
||||
} |
||||
|
||||
TEST_F(GlobalConfigEnvTest, Int32WithEnvTest) { |
||||
const char* int32_var_name = "INT32_VAR"; |
||||
|
||||
gpr_unsetenv(int32_var_name); |
||||
EXPECT_EQ(1234, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
|
||||
gpr_setenv(int32_var_name, "0"); |
||||
EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
|
||||
gpr_setenv(int32_var_name, "-123456789"); |
||||
EXPECT_EQ(-123456789, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
|
||||
gpr_setenv(int32_var_name, "123456789"); |
||||
EXPECT_EQ(123456789, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
|
||||
EXPECT_FALSE(IsConfigErrorCalled()); |
||||
|
||||
gpr_setenv(int32_var_name, "-1AB"); |
||||
GPR_GLOBAL_CONFIG_GET(int32_var); |
||||
EXPECT_TRUE(IsConfigErrorCalled()); |
||||
ClearConfigErrorCalled(); |
||||
} |
||||
|
||||
TEST_F(GlobalConfigEnvTest, StringWithEnvTest) { |
||||
const char* string_var_name = "STRING_VAR"; |
||||
grpc_core::UniquePtr<char> value; |
||||
|
||||
gpr_unsetenv(string_var_name); |
||||
value = GPR_GLOBAL_CONFIG_GET(string_var); |
||||
EXPECT_EQ(0, strcmp(value.get(), "Apple")); |
||||
|
||||
gpr_setenv(string_var_name, "Banana"); |
||||
value = GPR_GLOBAL_CONFIG_GET(string_var); |
||||
EXPECT_EQ(0, strcmp(value.get(), "Banana")); |
||||
|
||||
gpr_setenv(string_var_name, ""); |
||||
value = GPR_GLOBAL_CONFIG_GET(string_var); |
||||
EXPECT_EQ(0, strcmp(value.get(), "")); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
// Not to abort the test when parsing error happens.
|
||||
grpc_core::SetGlobalConfigEnvErrorFunction(&FakeConfigErrorFunction); |
||||
|
||||
::testing::InitGoogleTest(&argc, argv); |
||||
int ret = RUN_ALL_TESTS(); |
||||
return ret; |
||||
} |
@ -0,0 +1,65 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* http://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 <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/gpr/env.h" |
||||
#include "src/core/lib/gprpp/global_config.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
GPR_GLOBAL_CONFIG_DECLARE_BOOL(bool_var); |
||||
|
||||
GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, false, ""); |
||||
GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 0, ""); |
||||
GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "", ""); |
||||
|
||||
TEST(GlobalConfigTest, BoolTest) { |
||||
EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var)); |
||||
GPR_GLOBAL_CONFIG_SET(bool_var, true); |
||||
EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var)); |
||||
} |
||||
|
||||
TEST(GlobalConfigTest, Int32Test) { |
||||
EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
GPR_GLOBAL_CONFIG_SET(int32_var, 1024); |
||||
EXPECT_EQ(1024, GPR_GLOBAL_CONFIG_GET(int32_var)); |
||||
} |
||||
|
||||
TEST(GlobalConfigTest, StringTest) { |
||||
grpc_core::UniquePtr<char> value; |
||||
|
||||
value = GPR_GLOBAL_CONFIG_GET(string_var); |
||||
EXPECT_EQ(0, strcmp(value.get(), "")); |
||||
|
||||
GPR_GLOBAL_CONFIG_SET(string_var, "Test"); |
||||
|
||||
value = GPR_GLOBAL_CONFIG_GET(string_var); |
||||
EXPECT_EQ(0, strcmp(value.get(), "Test")); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
int ret = RUN_ALL_TESTS(); |
||||
return ret; |
||||
} |
Loading…
Reference in new issue