global_config_test.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "src/core/lib/gprpp/global_config.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <gtest/gtest.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/lib/gpr/env.h"
  25. #include "src/core/lib/gprpp/memory.h"
  26. GPR_GLOBAL_CONFIG_DECLARE_BOOL(bool_var);
  27. GPR_GLOBAL_CONFIG_DEFINE_BOOL(bool_var, false, "");
  28. GPR_GLOBAL_CONFIG_DEFINE_INT32(int32_var, 0, "");
  29. GPR_GLOBAL_CONFIG_DEFINE_STRING(string_var, "", "");
  30. TEST(GlobalConfigTest, BoolTest) {
  31. EXPECT_FALSE(GPR_GLOBAL_CONFIG_GET(bool_var));
  32. GPR_GLOBAL_CONFIG_SET(bool_var, true);
  33. EXPECT_TRUE(GPR_GLOBAL_CONFIG_GET(bool_var));
  34. }
  35. TEST(GlobalConfigTest, Int32Test) {
  36. EXPECT_EQ(0, GPR_GLOBAL_CONFIG_GET(int32_var));
  37. GPR_GLOBAL_CONFIG_SET(int32_var, 1024);
  38. EXPECT_EQ(1024, GPR_GLOBAL_CONFIG_GET(int32_var));
  39. }
  40. TEST(GlobalConfigTest, StringTest) {
  41. grpc_core::UniquePtr<char> value;
  42. value = GPR_GLOBAL_CONFIG_GET(string_var);
  43. EXPECT_EQ(0, strcmp(value.get(), ""));
  44. GPR_GLOBAL_CONFIG_SET(string_var, "Test");
  45. value = GPR_GLOBAL_CONFIG_GET(string_var);
  46. EXPECT_EQ(0, strcmp(value.get(), "Test"));
  47. }
  48. int main(int argc, char** argv) {
  49. ::testing::InitGoogleTest(&argc, argv);
  50. int ret = RUN_ALL_TESTS();
  51. return ret;
  52. }