core_configuration_test.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "src/core/lib/config/core_configuration.h"
  15. #include <chrono>
  16. #include <thread>
  17. #include <gtest/gtest.h>
  18. namespace grpc_core {
  19. // Allow substitution of config builder - in real code this would iterate
  20. // through all plugins
  21. namespace {
  22. using ConfigBuilderFunction = std::function<void(CoreConfiguration::Builder*)>;
  23. ConfigBuilderFunction g_mock_builder;
  24. } // namespace
  25. void BuildCoreConfiguration(CoreConfiguration::Builder* builder) {
  26. g_mock_builder(builder);
  27. }
  28. namespace {
  29. // Helper for testing - clear out any state, rebuild configuration with fn being
  30. // the initializer
  31. void InitConfigWithBuilder(ConfigBuilderFunction fn) {
  32. CoreConfiguration::Reset();
  33. g_mock_builder = fn;
  34. CoreConfiguration::Get();
  35. g_mock_builder = nullptr;
  36. }
  37. TEST(ConfigTest, NoopConfig) {
  38. InitConfigWithBuilder([](CoreConfiguration::Builder*) {});
  39. CoreConfiguration::Get();
  40. }
  41. TEST(ConfigTest, ThreadedInit) {
  42. CoreConfiguration::Reset();
  43. g_mock_builder = [](CoreConfiguration::Builder*) {
  44. std::this_thread::sleep_for(std::chrono::seconds(1));
  45. };
  46. std::vector<std::thread> threads;
  47. threads.reserve(64);
  48. for (int i = 0; i < 64; i++) {
  49. threads.push_back(std::thread([]() { CoreConfiguration::Get(); }));
  50. }
  51. for (auto& t : threads) {
  52. t.join();
  53. }
  54. g_mock_builder = nullptr;
  55. CoreConfiguration::Get();
  56. }
  57. } // namespace
  58. } // namespace grpc_core
  59. int main(int argc, char** argv) {
  60. ::testing::InitGoogleTest(&argc, argv);
  61. return RUN_ALL_TESTS();
  62. }