diff options
Diffstat (limited to 'src/mongo/unittest/unittest.cpp')
-rw-r--r-- | src/mongo/unittest/unittest.cpp | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp index ac8c1947f7d..1eef67c81d7 100644 --- a/src/mongo/unittest/unittest.cpp +++ b/src/mongo/unittest/unittest.cpp @@ -1,4 +1,3 @@ - /** * Copyright (C) 2018-present MongoDB, Inc. * @@ -166,6 +165,38 @@ UnsafeScopeGuard<F> MakeUnsafeScopeGuard(F fun) { return UnsafeScopeGuard<F>(std::move(fun)); } +// Attempting to read the featureCompatibilityVersion parameter before it is explicitly initialized +// with a meaningful value will trigger failures as of SERVER-32630. +void setUpFCV() { + serverGlobalParams.featureCompatibility.setVersion( + ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42); +} +void tearDownFCV() { + serverGlobalParams.featureCompatibility.reset(); +} + +struct TestSuiteEnvironment { + explicit TestSuiteEnvironment() { + setUpFCV(); + } + + ~TestSuiteEnvironment() noexcept(false) { + tearDownFCV(); + } +}; + +struct UnitTestEnvironment { + explicit UnitTestEnvironment(Test* const t) : test(t) { + test->setUp(); + } + + ~UnitTestEnvironment() noexcept(false) { + test->tearDown(); + } + + Test* const test; +}; + } // namespace Test::Test() : _isCapturingLogMessages(false) {} @@ -177,8 +208,7 @@ Test::~Test() { } void Test::run() { - setUp(); - auto guard = MakeUnsafeScopeGuard([this] { tearDown(); }); + UnitTestEnvironment environment(this); // An uncaught exception does not prevent the tear down from running. But // such an event still constitutes an error. To test this behavior we use a @@ -186,21 +216,11 @@ void Test::run() { // not considered an error. try { _doTest(); - } catch (FixtureExceptionForTesting&) { + } catch (const FixtureExceptionForTesting&) { return; } } -// Attempting to read the featureCompatibilityVersion parameter before it is explicitly initialized -// with a meaningful value will trigger failures as of SERVER-32630. -void Test::setUp() { - serverGlobalParams.featureCompatibility.setVersion( - ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42); -} -void Test::tearDown() { - serverGlobalParams.featureCompatibility.reset(); -} - namespace { class StringVectorAppender : public logger::MessageLogDomain::EventAppender { public: @@ -309,7 +329,9 @@ Result* Suite::run(const std::string& filter, int runsPerTest) { if (runsPerTest > 1) { runTimes << " (" << x + 1 << "/" << runsPerTest << ")"; } + log() << "\t going to run test: " << tc->getName() << runTimes.str(); + TestSuiteEnvironment environment; tc->run(); } passes = true; |