| 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 |
|
| 15 |
#include "src/core/lib/config/core_configuration.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <chrono> |
| 19 |
#include <thread> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include <gtest/gtest.h> |
| 23 |
|
| 24 |
namespace grpc_core { |
| 25 |
|
| 26 |
// Allow substitution of config builder - in real code this would iterate |
| 27 |
// through all plugins |
| 28 |
namespace { |
| 29 |
using ConfigBuilderFunction = std::function<void(CoreConfiguration::Builder*)>; |
| 30 |
ConfigBuilderFunction g_mock_builder; |
| 31 |
} // namespace |
| 32 |
|
| 33 |
void BuildCoreConfiguration(CoreConfiguration::Builder* builder) { |
| 34 |
g_mock_builder(builder); |
| 35 |
} |
| 36 |
|
| 37 |
namespace { |
| 38 |
// Helper for testing - clear out any state, rebuild configuration with fn being |
| 39 |
// the initializer |
| 40 |
void InitConfigWithBuilder(ConfigBuilderFunction fn) { |
| 41 |
CoreConfiguration::Reset(); |
| 42 |
g_mock_builder = fn; |
| 43 |
CoreConfiguration::Get(); |
| 44 |
g_mock_builder = nullptr; |
| 45 |
} |
| 46 |
|
| 47 |
TEST(ConfigTest, NoopConfig) { |
| 48 |
InitConfigWithBuilder([](CoreConfiguration::Builder*) {}); |
| 49 |
CoreConfiguration::Get(); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(ConfigTest, ThreadedInit) { |
| 53 |
CoreConfiguration::Reset(); |
| 54 |
g_mock_builder = [](CoreConfiguration::Builder*) { |
| 55 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 56 |
}; |
| 57 |
std::vector<std::thread> threads; |
| 58 |
threads.reserve(10); |
| 59 |
for (int i = 0; i < 10; i++) { |
| 60 |
threads.push_back(std::thread([]() { CoreConfiguration::Get(); })); |
| 61 |
} |
| 62 |
for (auto& t : threads) { |
| 63 |
t.join(); |
| 64 |
} |
| 65 |
g_mock_builder = nullptr; |
| 66 |
CoreConfiguration::Get(); |
| 67 |
} |
| 68 |
} // namespace |
| 69 |
|
| 70 |
} // namespace grpc_core |
| 71 |
|
| 72 |
int main(int argc, char** argv) { |
| 73 |
::testing::InitGoogleTest(&argc, argv); |
| 74 |
return RUN_ALL_TESTS(); |
| 75 |
} |