diff options
author | ADAM David Alan Martin <adam.martin@10gen.com> | 2019-01-24 15:53:23 -0500 |
---|---|---|
committer | ADAM David Alan Martin <adam.martin@10gen.com> | 2019-01-25 11:55:34 -0500 |
commit | 677a65ba43fecf7cf20b7a2c34b5ec2bed413edd (patch) | |
tree | 8d9506823c2f003a718b3afc41cdefe7f1cfde71 /src/mongo/unittest | |
parent | bbe09aa5e0966ada5232ffbcb098efdd78b4f24e (diff) | |
download | mongo-677a65ba43fecf7cf20b7a2c34b5ec2bed413edd.tar.gz |
SERVER-39000 Fix Unittest Framework initialization.
Some tests in `dbtest` do not use the `mongo::unittest::Test` class
as a basis for their implementation and thus need some way to benefit
from global DB Environment initialization functions. Specifically
the changes in SERVER-32630 required `serverCompatibilityVersion`
to be set sensibly. Some tests in `dbtest` were not correctly
getting this benefit; instead only incidentally getting a correct
setting by accident, as the results of an unintended residue of
an earlier operation. This can lead to inconsistentcies in which
tests pass, as link order changes -- the tests are registered using
static initialization, whose instability of order can cause
mysterious failures in `dbtest`.
Diffstat (limited to 'src/mongo/unittest')
-rw-r--r-- | src/mongo/unittest/unittest.cpp | 50 | ||||
-rw-r--r-- | src/mongo/unittest/unittest.h | 20 |
2 files changed, 46 insertions, 24 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; diff --git a/src/mongo/unittest/unittest.h b/src/mongo/unittest/unittest.h index 1a6f82a9a36..4614b7a5abd 100644 --- a/src/mongo/unittest/unittest.h +++ b/src/mongo/unittest/unittest.h @@ -306,6 +306,16 @@ public: void run(); + /** + * Called on the test object before running the test. + */ + virtual void setUp() {} + + /** + * Called on the test object after running the test. + */ + virtual void tearDown() {} + protected: /** * Registration agent for adding tests to suites, used by TEST macro. @@ -363,16 +373,6 @@ protected: */ void printCapturedLogLines() const; - /** - * Called on the test object before running the test. - */ - virtual void setUp(); - - /** - * Called on the test object after running the test. - */ - virtual void tearDown(); - private: /** * The test itself. |