diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2018-06-22 12:00:21 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2018-06-22 12:32:29 -0400 |
commit | d520be0814492c262515cf0a5d62a127ace70dce (patch) | |
tree | e754e3cce243a7b51922b6d2a179a3d355ccefb7 /src/mongo/db/ftdc | |
parent | c0b942e3a80b9ccd8434ab0927d97cbd1862d19a (diff) | |
download | mongo-d520be0814492c262515cf0a5d62a127ace70dce.tar.gz |
SERVER-34798 Remove ServiceContext subclasses and use new ServiceContext in every unit test.
This patch does several loosely related and surprisingly hard to separate things.
1.) Make the ServiceContext class final
2.) Create a mechanism, called ConstructorActions, for running methods on
ServiceContexts immediately after they're built and immediately before they're
destroyed.
3.) Introduce / improve test fixture base classes for tests, giving them fresh
ServiceContext instances for each test case. There is one fixture for tests that
need a storage engine and another for those that do not.
4.) Make several remaining global variables SC decorations in support of (3)
5.) Replace many MONGO_INITIALIZERS that access getGlobalServiceContext with the
new constructor-actions system, which is needed for (3.)
6.) Fix up tests to use the fixtures from (3) and fix tests that silently used
different service contexts in together in a technically illegal fashion that now
breaks.
7.) Utilize (2) as necessary to simplify initialization of new ServiceContexts,
simplifying the fixtures in (3).
Diffstat (limited to 'src/mongo/db/ftdc')
-rw-r--r-- | src/mongo/db/ftdc/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/ftdc/compressor_test.cpp | 18 | ||||
-rw-r--r-- | src/mongo/db/ftdc/controller_test.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/ftdc/file_manager_test.cpp | 10 | ||||
-rw-r--r-- | src/mongo/db/ftdc/file_writer_test.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_test.cpp | 17 | ||||
-rw-r--r-- | src/mongo/db/ftdc/ftdc_test.h | 6 |
7 files changed, 41 insertions, 33 deletions
diff --git a/src/mongo/db/ftdc/SConscript b/src/mongo/db/ftdc/SConscript index 4822907a540..6415ae347dd 100644 --- a/src/mongo/db/ftdc/SConscript +++ b/src/mongo/db/ftdc/SConscript @@ -96,6 +96,7 @@ env.CppUnitTest( 'varint_test.cpp', ], LIBDEPS=[ + '$BUILD_DIR/mongo/db/service_context_test_fixture', '$BUILD_DIR/mongo/util/clock_source_mock', 'ftdc', ], diff --git a/src/mongo/db/ftdc/compressor_test.cpp b/src/mongo/db/ftdc/compressor_test.cpp index 0c01bd58040..38ae3958a7b 100644 --- a/src/mongo/db/ftdc/compressor_test.cpp +++ b/src/mongo/db/ftdc/compressor_test.cpp @@ -60,8 +60,10 @@ namespace mongo { FTDCCompressor::CompressorState::kCompressorFull); \ ASSERT_TRUE(st.getValue().is_initialized()); +class FTDCCompressorTest : public FTDCTest {}; + // Sanity check -TEST(FTDCCompressor, TestBasic) { +TEST_F(FTDCCompressorTest, TestBasic) { FTDCConfig config; FTDCCompressor c(&config); @@ -91,7 +93,7 @@ TEST(FTDCCompressor, TestBasic) { } // Test strings only -TEST(FTDCCompressor, TestStrings) { +TEST_F(FTDCCompressorTest, TestStrings) { FTDCConfig config; FTDCCompressor c(&config); @@ -180,7 +182,7 @@ private: }; // Test various schema changes -TEST(FTDCCompressor, TestSchemaChanges) { +TEST_F(FTDCCompressorTest, TestSchemaChanges) { TestTie c; auto st = c.addSample(BSON("name" @@ -341,7 +343,7 @@ TEST(FTDCCompressor, TestSchemaChanges) { } // Ensure changing between the various number formats is considered compatible -TEST(FTDCCompressor, TestNumbersCompat) { +TEST_F(FTDCCompressorTest, TestNumbersCompat) { TestTie c; auto st = c.addSample(BSON("name" @@ -368,7 +370,7 @@ TEST(FTDCCompressor, TestNumbersCompat) { } // Test various date time types -TEST(FTDCCompressor, TestDateTimeTypes) { +TEST_F(FTDCCompressorTest, TestDateTimeTypes) { TestTie c; for (int i = 0; i < 10; i++) { BSONObjBuilder builder1; @@ -382,7 +384,7 @@ TEST(FTDCCompressor, TestDateTimeTypes) { } // Test all types -TEST(FTDCCompressor, Types) { +TEST_F(FTDCCompressorTest, Types) { TestTie c; auto st = c.addSample(BSON("name" @@ -455,7 +457,7 @@ TEST(FTDCCompressor, Types) { } // Test a full buffer -TEST(FTDCCompressor, TestFull) { +TEST_F(FTDCCompressorTest, TestFull) { // Test a large numbers of zeros, and incremental numbers in a full buffer for (int j = 0; j < 2; j++) { TestTie c; @@ -509,7 +511,7 @@ BSONObj generateSample(std::random_device& rd, T generator, size_t count) { } // Test many metrics -TEST(ZFTDCCompressor, TestManyMetrics) { +TEST_F(FTDCCompressorTest, TestManyMetrics) { std::random_device rd; std::mt19937 gen(rd()); diff --git a/src/mongo/db/ftdc/controller_test.cpp b/src/mongo/db/ftdc/controller_test.cpp index c307d02cca8..de88054aec9 100644 --- a/src/mongo/db/ftdc/controller_test.cpp +++ b/src/mongo/db/ftdc/controller_test.cpp @@ -50,6 +50,8 @@ namespace mongo { +class FTDCControllerTest : public FTDCTest {}; + class FTDCMetricsCollectorMockTee : public FTDCCollectorInterface { public: ~FTDCMetricsCollectorMockTee() { @@ -154,7 +156,7 @@ public: }; // Test a run of the controller and the data it logs to log file -TEST(FTDCControllerTest, TestFull) { +TEST_F(FTDCControllerTest, TestFull) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path dir(tempdir.path()); @@ -207,7 +209,7 @@ TEST(FTDCControllerTest, TestFull) { // Test we can start and stop the controller in quick succession, make sure it succeeds without // assert or fault -TEST(FTDCControllerTest, TestStartStop) { +TEST_F(FTDCControllerTest, TestStartStop) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path dir(tempdir.path()); @@ -228,7 +230,7 @@ TEST(FTDCControllerTest, TestStartStop) { // Test we can start the controller as disabled, the directory is empty, and then we can succesfully // enable it -TEST(FTDCControllerTest, TestStartAsDisabled) { +TEST_F(FTDCControllerTest, TestStartAsDisabled) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path dir(tempdir.path()); diff --git a/src/mongo/db/ftdc/file_manager_test.cpp b/src/mongo/db/ftdc/file_manager_test.cpp index 027ae88af62..0a0b0367221 100644 --- a/src/mongo/db/ftdc/file_manager_test.cpp +++ b/src/mongo/db/ftdc/file_manager_test.cpp @@ -50,8 +50,10 @@ namespace mongo { +class FTDCFileManagerTest : public ServiceContextTest {}; + // Test a full buffer -TEST(FTDCFileManagerTest, TestFull) { +TEST_F(FTDCFileManagerTest, TestFull) { Client* client = &cc(); FTDCConfig c; c.maxFileSizeBytes = 300; @@ -149,7 +151,7 @@ void ValidateInterimFileHasData(const boost::filesystem::path& dir, bool hasData } // Test a normal restart -TEST(FTDCFileManagerTest, TestNormalRestart) { +TEST_F(FTDCFileManagerTest, TestNormalRestart) { Client* client = &cc(); FTDCConfig c; c.maxFileSizeBytes = 1000; @@ -219,7 +221,7 @@ TEST(FTDCFileManagerTest, TestNormalRestart) { } // Test a restart after a crash with a corrupt archive file -TEST(FTDCFileManagerTest, TestCorruptCrashRestart) { +TEST_F(FTDCFileManagerTest, TestCorruptCrashRestart) { Client* client = &cc(); FTDCConfig c; c.maxFileSizeBytes = 1000; @@ -296,7 +298,7 @@ TEST(FTDCFileManagerTest, TestCorruptCrashRestart) { } // Test a restart with a good interim file, and validate we have all the data -TEST(FTDCFileManagerTest, TestNormalCrashInterim) { +TEST_F(FTDCFileManagerTest, TestNormalCrashInterim) { Client* client = &cc(); FTDCConfig c; c.maxSamplesPerInterimMetricChunk = 3; diff --git a/src/mongo/db/ftdc/file_writer_test.cpp b/src/mongo/db/ftdc/file_writer_test.cpp index f7977e2b8b3..81e3a741d24 100644 --- a/src/mongo/db/ftdc/file_writer_test.cpp +++ b/src/mongo/db/ftdc/file_writer_test.cpp @@ -47,8 +47,10 @@ namespace mongo { const char* kTestFile = "metrics.test"; const char* kTestFileCopy = "metrics.test.copy"; +class FTDCFileTest : public ServiceContextTest {}; + // File Sanity check -TEST(FTDCFileTest, TestFileBasicMetadata) { +TEST_F(FTDCFileTest, TestFileBasicMetadata) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path p(tempdir.path()); p /= kTestFile; @@ -99,7 +101,7 @@ TEST(FTDCFileTest, TestFileBasicMetadata) { } // File Sanity check -TEST(FTDCFileTest, TestFileBasicCompress) { +TEST_F(FTDCFileTest, TestFileBasicCompress) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path p(tempdir.path()); p /= kTestFile; @@ -208,7 +210,7 @@ private: }; // Test various schema changes -TEST(FTDCFileTest, TestSchemaChanges) { +TEST_F(FTDCFileTest, TestSchemaChanges) { FileTestTie c; c.addSample(BSON("name" @@ -279,7 +281,7 @@ TEST(FTDCFileTest, TestSchemaChanges) { } // Test a full buffer -TEST(FTDCFileTest, TestFull) { +TEST_F(FTDCFileTest, TestFull) { // Test a large numbers of zeros, and incremental numbers in a full buffer for (int j = 0; j < 2; j++) { FileTestTie c; @@ -318,7 +320,7 @@ TEST(FTDCFileTest, TestFull) { } // Test a large documents so that we cause multiple 4kb buffers to flush on Windows. -TEST(FTDCFileTest, TestLargeDocuments) { +TEST_F(FTDCFileTest, TestLargeDocuments) { FileTestTie c; for (int j = 0; j < 5; j++) { @@ -337,7 +339,7 @@ TEST(FTDCFileTest, TestLargeDocuments) { } // Test a bad file -TEST(FTDCFileTest, TestBadFile) { +TEST_F(FTDCFileTest, TestBadFile) { unittest::TempDir tempdir("metrics_testpath"); boost::filesystem::path p(tempdir.path()); p /= kTestFile; diff --git a/src/mongo/db/ftdc/ftdc_test.cpp b/src/mongo/db/ftdc/ftdc_test.cpp index 7e4020b3979..11674ea2cad 100644 --- a/src/mongo/db/ftdc/ftdc_test.cpp +++ b/src/mongo/db/ftdc/ftdc_test.cpp @@ -39,7 +39,6 @@ #include "mongo/db/ftdc/file_reader.h" #include "mongo/db/jsobj.h" #include "mongo/db/service_context.h" -#include "mongo/db/service_context_noop.h" #include "mongo/stdx/memory.h" #include "mongo/unittest/unittest.h" #include "mongo/util/clock_source.h" @@ -111,17 +110,11 @@ void createDirectoryClean(const boost::filesystem::path& dir) { boost::filesystem::create_directory(dir); } -MONGO_INITIALIZER_WITH_PREREQUISITES(FTDCTestInit, ("ThreadNameInitializer")) -(InitializerContext* context) { - setGlobalServiceContext(stdx::make_unique<ServiceContextNoop>()); - - getGlobalServiceContext()->setFastClockSource(stdx::make_unique<ClockSourceMock>()); - getGlobalServiceContext()->setPreciseClockSource(stdx::make_unique<ClockSourceMock>()); - getGlobalServiceContext()->setTickSource(stdx::make_unique<TickSourceMock>()); - - Client::initThreadIfNotAlready("UnitTest"); - - return Status::OK(); +FTDCTest::FTDCTest() { + auto service = getServiceContext(); + service->setFastClockSource(stdx::make_unique<ClockSourceMock>()); + service->setPreciseClockSource(stdx::make_unique<ClockSourceMock>()); + service->setTickSource(stdx::make_unique<TickSourceMock>()); } } // namespace mongo diff --git a/src/mongo/db/ftdc/ftdc_test.h b/src/mongo/db/ftdc/ftdc_test.h index afbca103b4f..fb9ada44f2f 100644 --- a/src/mongo/db/ftdc/ftdc_test.h +++ b/src/mongo/db/ftdc/ftdc_test.h @@ -30,9 +30,15 @@ #include <vector> #include "mongo/db/jsobj.h" +#include "mongo/db/service_context_test_fixture.h" namespace mongo { +class FTDCTest : public ServiceContextTest { +public: + FTDCTest(); +}; + /** * Validate the documents in a file match the specified vector. * |