Make sure that current_test_suite and current_test_info are mutex-protected while writing for thread-safety.

PiperOrigin-RevId: 610810340
Change-Id: I37f33510373dff04b8e9c9e8a9f32d30fcce46ff
This commit is contained in:
Abseil Team 2024-02-27 11:03:43 -08:00 committed by Copybara-Service
parent 9d43b27f7a
commit 814ba36338
3 changed files with 59 additions and 30 deletions

View File

@ -1262,6 +1262,20 @@ class GTEST_API_ UnitTest {
// total_test_suite_count() - 1. If i is not in that range, returns NULL. // total_test_suite_count() - 1. If i is not in that range, returns NULL.
TestSuite* GetMutableTestSuite(int i); TestSuite* GetMutableTestSuite(int i);
// Invokes OsStackTrackGetterInterface::UponLeavingGTest. UponLeavingGTest()
// should be called immediately before Google Test calls user code. It saves
// some information about the current stack that CurrentStackTrace() will use
// to find and hide Google Test stack frames.
void UponLeavingGTest();
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite)
GTEST_LOCK_EXCLUDED_(mutex_);
// Sets the TestInfo object for the test that's currently running.
void set_current_test_info(TestInfo* a_current_test_info)
GTEST_LOCK_EXCLUDED_(mutex_);
// Accessors for the implementation object. // Accessors for the implementation object.
internal::UnitTestImpl* impl() { return impl_; } internal::UnitTestImpl* impl() { return impl_; }
const internal::UnitTestImpl* impl() const { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; }
@ -1270,6 +1284,8 @@ class GTEST_API_ UnitTest {
// members of UnitTest. // members of UnitTest.
friend class ScopedTrace; friend class ScopedTrace;
friend class Test; friend class Test;
friend class TestInfo;
friend class TestSuite;
friend class internal::AssertHelper; friend class internal::AssertHelper;
friend class internal::StreamingListenerTest; friend class internal::StreamingListenerTest;
friend class internal::UnitTestRecordPropertyTestHelper; friend class internal::UnitTestRecordPropertyTestHelper;

View File

@ -709,18 +709,6 @@ class GTEST_API_ UnitTestImpl {
return type_parameterized_test_registry_; return type_parameterized_test_registry_;
} }
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite) {
current_test_suite_ = a_current_test_suite;
}
// Sets the TestInfo object for the test that's currently running. If
// current_test_info is NULL, the assertion results will be stored in
// ad_hoc_test_result_.
void set_current_test_info(TestInfo* a_current_test_info) {
current_test_info_ = a_current_test_info;
}
// Registers all parameterized tests defined using TEST_P and // Registers all parameterized tests defined using TEST_P and
// INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter // INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter
// combination. This method can be called more then once; it has guards // combination. This method can be called more then once; it has guards
@ -841,6 +829,18 @@ class GTEST_API_ UnitTestImpl {
// GTEST_FLAG(catch_exceptions) at the moment it starts. // GTEST_FLAG(catch_exceptions) at the moment it starts.
void set_catch_exceptions(bool value) { catch_exceptions_ = value; } void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite) {
current_test_suite_ = a_current_test_suite;
}
// Sets the TestInfo object for the test that's currently running. If
// current_test_info is NULL, the assertion results will be stored in
// ad_hoc_test_result_.
void set_current_test_info(TestInfo* a_current_test_info) {
current_test_info_ = a_current_test_info;
}
// The UnitTest object that owns this implementation object. // The UnitTest object that owns this implementation object.
UnitTest* const parent_; UnitTest* const parent_;

View File

@ -2836,14 +2836,13 @@ void TestInfo::Run() {
} }
// Tells UnitTest where to store test result. // Tells UnitTest where to store test result.
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); UnitTest::GetInstance()->set_current_test_info(this);
impl->set_current_test_info(this);
// Notifies the unit test event listeners that a test is about to start. // Notifies the unit test event listeners that a test is about to start.
repeater->OnTestStart(*this); repeater->OnTestStart(*this);
result_.set_start_timestamp(internal::GetTimeInMillis()); result_.set_start_timestamp(internal::GetTimeInMillis());
internal::Timer timer; internal::Timer timer;
impl->os_stack_trace_getter()->UponLeavingGTest(); UnitTest::GetInstance()->UponLeavingGTest();
// Creates the test object. // Creates the test object.
Test* const test = internal::HandleExceptionsInMethodIfSupported( Test* const test = internal::HandleExceptionsInMethodIfSupported(
@ -2861,7 +2860,7 @@ void TestInfo::Run() {
if (test != nullptr) { if (test != nullptr) {
// Deletes the test object. // Deletes the test object.
impl->os_stack_trace_getter()->UponLeavingGTest(); UnitTest::GetInstance()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(
test, &Test::DeleteSelf_, "the test fixture's destructor"); test, &Test::DeleteSelf_, "the test fixture's destructor");
} }
@ -2873,15 +2872,14 @@ void TestInfo::Run() {
// Tells UnitTest to stop associating assertion results to this // Tells UnitTest to stop associating assertion results to this
// test. // test.
impl->set_current_test_info(nullptr); UnitTest::GetInstance()->set_current_test_info(nullptr);
} }
// Skip and records a skipped test result for this object. // Skip and records a skipped test result for this object.
void TestInfo::Skip() { void TestInfo::Skip() {
if (!should_run_) return; if (!should_run_) return;
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); UnitTest::GetInstance()->set_current_test_info(this);
impl->set_current_test_info(this);
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
@ -2890,12 +2888,13 @@ void TestInfo::Skip() {
const TestPartResult test_part_result = const TestPartResult test_part_result =
TestPartResult(TestPartResult::kSkip, this->file(), this->line(), ""); TestPartResult(TestPartResult::kSkip, this->file(), this->line(), "");
impl->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult( internal::GetUnitTestImpl()
test_part_result); ->GetTestPartResultReporterForCurrentThread()
->ReportTestPartResult(test_part_result);
// Notifies the unit test event listener that a test has just finished. // Notifies the unit test event listener that a test has just finished.
repeater->OnTestEnd(*this); repeater->OnTestEnd(*this);
impl->set_current_test_info(nullptr); UnitTest::GetInstance()->set_current_test_info(nullptr);
} }
// class TestSuite // class TestSuite
@ -2991,8 +2990,7 @@ void TestSuite::AddTestInfo(TestInfo* test_info) {
void TestSuite::Run() { void TestSuite::Run() {
if (!should_run_) return; if (!should_run_) return;
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); UnitTest::GetInstance()->set_current_test_suite(this);
impl->set_current_test_suite(this);
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
@ -3022,7 +3020,7 @@ void TestSuite::Run() {
repeater->OnTestCaseStart(*this); repeater->OnTestCaseStart(*this);
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
impl->os_stack_trace_getter()->UponLeavingGTest(); UnitTest::GetInstance()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(
this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()"); this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()");
@ -3047,7 +3045,7 @@ void TestSuite::Run() {
} }
elapsed_time_ = timer.Elapsed(); elapsed_time_ = timer.Elapsed();
impl->os_stack_trace_getter()->UponLeavingGTest(); UnitTest::GetInstance()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(
this, &TestSuite::RunTearDownTestSuite, "TearDownTestSuite()"); this, &TestSuite::RunTearDownTestSuite, "TearDownTestSuite()");
@ -3058,15 +3056,14 @@ void TestSuite::Run() {
repeater->OnTestCaseEnd(*this); repeater->OnTestCaseEnd(*this);
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
impl->set_current_test_suite(nullptr); UnitTest::GetInstance()->set_current_test_suite(nullptr);
} }
// Skips all tests under this TestSuite. // Skips all tests under this TestSuite.
void TestSuite::Skip() { void TestSuite::Skip() {
if (!should_run_) return; if (!should_run_) return;
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); UnitTest::GetInstance()->set_current_test_suite(this);
impl->set_current_test_suite(this);
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
@ -3088,7 +3085,7 @@ void TestSuite::Skip() {
repeater->OnTestCaseEnd(*this); repeater->OnTestCaseEnd(*this);
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
impl->set_current_test_suite(nullptr); UnitTest::GetInstance()->set_current_test_suite(nullptr);
} }
// Clears the results of all tests in this test suite. // Clears the results of all tests in this test suite.
@ -5304,6 +5301,22 @@ TestSuite* UnitTest::GetMutableTestSuite(int i) {
return impl()->GetMutableSuiteCase(i); return impl()->GetMutableSuiteCase(i);
} }
void UnitTest::UponLeavingGTest() {
impl()->os_stack_trace_getter()->UponLeavingGTest();
}
// Sets the TestSuite object for the test that's currently running.
void UnitTest::set_current_test_suite(TestSuite* a_current_test_suite) {
internal::MutexLock lock(&mutex_);
impl_->set_current_test_suite(a_current_test_suite);
}
// Sets the TestInfo object for the test that's currently running.
void UnitTest::set_current_test_info(TestInfo* a_current_test_info) {
internal::MutexLock lock(&mutex_);
impl_->set_current_test_info(a_current_test_info);
}
// Returns the list of event listeners that can be used to track events // Returns the list of event listeners that can be used to track events
// inside Google Test. // inside Google Test.
TestEventListeners& UnitTest::listeners() { return *impl()->listeners(); } TestEventListeners& UnitTest::listeners() { return *impl()->listeners(); }