global_config_env_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 
29 #include "src/core/lib/gpr/env.h"
31 
32 namespace {
33 
34 bool g_config_error_function_called;
35 
36 void ClearConfigErrorCalled() { g_config_error_function_called = false; }
37 
38 bool IsConfigErrorCalled() { return g_config_error_function_called; }
39 
40 // This function is for preventing the program from invoking
41 // an error handler due to configuration error and
42 // make test routines know whether there is error.
43 void FakeConfigErrorFunction(const char* /*error_message*/) {
44  g_config_error_function_called = true;
45 }
46 
47 class GlobalConfigEnvTest : public ::testing::Test {
48  protected:
49  void SetUp() override { ClearConfigErrorCalled(); }
50  void TearDown() override { EXPECT_FALSE(IsConfigErrorCalled()); }
51 };
52 
53 } // namespace
54 
55 GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, true, "");
56 GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 1234, "");
57 GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "Apple", "");
58 
59 TEST_F(GlobalConfigEnvTest, BoolWithEnvTest) {
60  const char* bool_var_name = "BOOL_VAR";
61 
62  gpr_unsetenv(bool_var_name);
64 
65  gpr_setenv(bool_var_name, "true");
67 
68  gpr_setenv(bool_var_name, "false");
70 
71  EXPECT_FALSE(IsConfigErrorCalled());
72 
73  gpr_setenv(bool_var_name, "");
74  GPR_GLOBAL_CONFIG_GET(bool_var);
75  EXPECT_TRUE(IsConfigErrorCalled());
76  ClearConfigErrorCalled();
77 
78  gpr_setenv(bool_var_name, "!");
79  GPR_GLOBAL_CONFIG_GET(bool_var);
80  EXPECT_TRUE(IsConfigErrorCalled());
81  ClearConfigErrorCalled();
82 }
83 
84 TEST_F(GlobalConfigEnvTest, Int32WithEnvTest) {
85  const char* int32_var_name = "INT32_VAR";
86 
87  gpr_unsetenv(int32_var_name);
88  EXPECT_EQ(1234, GPR_GLOBAL_CONFIG_GET(int32_var));
89 
90  gpr_setenv(int32_var_name, "0");
91  EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
92 
93  gpr_setenv(int32_var_name, "-123456789");
94  EXPECT_EQ(-123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
95 
96  gpr_setenv(int32_var_name, "123456789");
97  EXPECT_EQ(123456789, GPR_GLOBAL_CONFIG_GET(int32_var));
98 
99  EXPECT_FALSE(IsConfigErrorCalled());
100 
101  gpr_setenv(int32_var_name, "-1AB");
102  GPR_GLOBAL_CONFIG_GET(int32_var);
103  EXPECT_TRUE(IsConfigErrorCalled());
104  ClearConfigErrorCalled();
105 }
106 
107 TEST_F(GlobalConfigEnvTest, StringWithEnvTest) {
108  const char* string_var_name = "STRING_VAR";
110 
111  gpr_unsetenv(string_var_name);
112  value = GPR_GLOBAL_CONFIG_GET(string_var);
113  EXPECT_EQ(0, strcmp(value.get(), "Apple"));
114 
115  gpr_setenv(string_var_name, "Banana");
116  value = GPR_GLOBAL_CONFIG_GET(string_var);
117  EXPECT_EQ(0, strcmp(value.get(), "Banana"));
118 
119  gpr_setenv(string_var_name, "");
120  value = GPR_GLOBAL_CONFIG_GET(string_var);
121  EXPECT_EQ(0, strcmp(value.get(), ""));
122 }
123 
124 int main(int argc, char** argv) {
125  // Not to abort the test when parsing error happens.
126  grpc_core::SetGlobalConfigEnvErrorFunction(&FakeConfigErrorFunction);
127 
128  ::testing::InitGoogleTest(&argc, argv);
129  int ret = RUN_ALL_TESTS();
130  return ret;
131 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
global_config_env.h
log.h
GPR_GLOBAL_CONFIG_GET
#define GPR_GLOBAL_CONFIG_GET(name)
Definition: global_config_generic.h:24
GPR_GLOBAL_CONFIG_DEFINE_BOOL
GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, true, "")
string.h
grpc_core::SetGlobalConfigEnvErrorFunction
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func)
Definition: global_config_env.cc:58
env.h
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::Test::TearDown
virtual void TearDown()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:2270
memory.h
main
int main(int argc, char **argv)
Definition: global_config_env_test.cc:124
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
GPR_GLOBAL_CONFIG_DEFINE_INT32
GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 1234, "")
value
const char * value
Definition: hpack_parser_table.cc:165
testing::Test::SetUp
virtual void SetUp()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:2264
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
TEST_F
TEST_F(GlobalConfigEnvTest, BoolWithEnvTest)
Definition: global_config_env_test.cc:59
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
gpr_unsetenv
void gpr_unsetenv(const char *name)
gpr_setenv
void gpr_setenv(const char *name, const char *value)
GPR_GLOBAL_CONFIG_DEFINE_STRING
GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "Apple", "")


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:28