diff options
author | Vojislav Stojkovic <vojislav.stojkovic@mongodb.com> | 2021-12-21 21:16:37 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-21 21:58:02 +0000 |
commit | ff375f0ae47b960f46a8606d0f5fd6558230709b (patch) | |
tree | fee171d12bb09246f7d61b17fb6971c3e7f27f30 | |
parent | 68b1c9ad4fe1c4015c587968bc23b7ec45c5dd17 (diff) | |
download | mongo-ff375f0ae47b960f46a8606d0f5fd6558230709b.tar.gz |
SERVER-61732 Prevent threads spawned outside ThreadContextTest from updating test counters
-rw-r--r-- | src/mongo/util/thread_context_test.cpp | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/mongo/util/thread_context_test.cpp b/src/mongo/util/thread_context_test.cpp index 99e94fb7837..a7f17ce2393 100644 --- a/src/mongo/util/thread_context_test.cpp +++ b/src/mongo/util/thread_context_test.cpp @@ -27,8 +27,11 @@ * it in the license file. */ +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest + #include "mongo/platform/basic.h" +#include "mongo/logv2/log.h" #include "mongo/util/thread_context.h" #include <boost/optional.hpp> @@ -73,23 +76,37 @@ synchronized_value gCounters{Counters{0, 0, 0}}; class TestDecoration { public: TestDecoration() { - ASSERT(!ThreadContext::get()) - << "ThreadContext decorations should be created before the ThreadContext is set"; - - ++gCounters->created; - }; + invariant(!ThreadContext::get()); + + if (newInstancesEnabled) { + // Thread name is not available here, so log the ID for diagnostic purposes. + LOGV2_INFO(6173200, + "Incrementing creation counter", + "threadId"_attr = ProcessId::getCurrentThreadId()); + ++gCounters->created; + } + } ~TestDecoration() { - ++gCounters->destroyed; - - if (ThreadContext::get()) { - // We should only be able to reference a ThreadContext in our destructor if our - // lifetime was extended to be off thread. - ++gCounters->destroyedOffThread; + if (newInstancesEnabled) { + // Thread name might not be available here, so log the ID for diagnostic purposes. + LOGV2_INFO(6173201, + "Incrementing destruction counter(s)", + "threadId"_attr = ProcessId::getCurrentThreadId()); + ++gCounters->destroyed; + + if (ThreadContext::get()) { + // We should only be able to reference a ThreadContext in our destructor if our + // lifetime was extended to be off thread. + ++gCounters->destroyedOffThread; + } } } + + static thread_local bool newInstancesEnabled; }; +thread_local bool TestDecoration::newInstancesEnabled = false; const auto getThreadTestDecoration = ThreadContext::declareDecoration<TestDecoration>(); class ThreadContextTest : public unittest::Test { @@ -98,11 +115,13 @@ public: ThreadContext::get(); // Ensure a ThreadContext for the main thread. _monitor.emplace(); *gCounters = {0, 0, 0}; + TestDecoration::newInstancesEnabled = true; } void tearDown() override { _monitor->notifyDone(); _monitor.reset(); + TestDecoration::newInstancesEnabled = false; auto endCount = gCounters.get(); ASSERT_EQ(endCount.created, endCount.destroyed); ASSERT_GTE(endCount.destroyed, endCount.destroyedOffThread); @@ -117,6 +136,12 @@ public: ASSERT(context); ASSERT(context->isAlive()); + // Log the thread ID for diagnostic purposes, so it can be associated with + // the thread name and cross-referenced with other logging statements here. + LOGV2_INFO(6173202, + "Retrieving thread context", + "threadId"_attr = ProcessId::getCurrentThreadId()); + return context; } @@ -133,7 +158,12 @@ public: */ template <typename F> void launchAndJoinThread(F&& f) { - _monitor->spawn(std::forward<F>(f)).join(); + _monitor + ->spawn([&] { + TestDecoration::newInstancesEnabled = true; + std::forward<F>(f)(); + }) + .join(); } boost::optional<unittest::ThreadAssertionMonitor> _monitor; |