diff options
Diffstat (limited to 'src/mongo/idl/server_parameter_test_util.h')
| -rw-r--r-- | src/mongo/idl/server_parameter_test_util.h | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/src/mongo/idl/server_parameter_test_util.h b/src/mongo/idl/server_parameter_test_util.h index 566489dd1a9..f7e2ae31834 100644 --- a/src/mongo/idl/server_parameter_test_util.h +++ b/src/mongo/idl/server_parameter_test_util.h @@ -36,31 +36,28 @@ namespace mongo { /** - * Test-only RAII type that allows to set a server parameter value during the execution of a - * unit test, or part of a unit test, and resets it to the original value on destruction. + * Test-only class that sets a server parameter to the specified value and allows + * resetting after the test completes. */ -class RAIIServerParameterControllerForTest { +class ServerParameterControllerForTest { public: /** * Constructor setting the server parameter to the specified value. */ template <typename T> - RAIIServerParameterControllerForTest(const std::string& name, T value) + ServerParameterControllerForTest(const std::string& name, T value) : _serverParam(ServerParameterSet::getNodeParameterSet()->get(name)) { - // Save the old value + // Save the old value. BSONObjBuilder bob; _serverParam->appendSupportingRoundtrip(nullptr, bob, name); _oldValue = bob.obj(); - // Set to the new value + // Set server param to the new value. uassertStatusOK(_serverParam->set(BSON(name << value).firstElement())); } - /** - * Destructor resetting the server parameter to the original value. - */ - ~RAIIServerParameterControllerForTest() { - // Reset to the old value + void reset() { + // Reset to the old value. auto elem = _oldValue.firstElement(); uassertStatusOK(_serverParam->set(elem)); } @@ -70,4 +67,28 @@ private: BSONObj _oldValue; }; +/** + * Test-only RAII type that wraps ServerParameterControllerForTest. Upon destruction, the server + * parameter will be set to its original value. + */ +class RAIIServerParameterControllerForTest { +public: + /** + * Constructor setting the server parameter to the specified value. + */ + template <typename T> + RAIIServerParameterControllerForTest(const std::string& name, T value) + : _serverParamController(ServerParameterControllerForTest(name, value)) {} + + /** + * Destructor resetting the server parameter to the original value. + */ + ~RAIIServerParameterControllerForTest() { + _serverParamController.reset(); + } + +private: + ServerParameterControllerForTest _serverParamController; +}; + } // namespace mongo |
