Skip to content

Commit 814ba36

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Make sure that current_test_suite and current_test_info are mutex-protected while writing for thread-safety.
PiperOrigin-RevId: 610810340 Change-Id: I37f33510373dff04b8e9c9e8a9f32d30fcce46ff
1 parent 9d43b27 commit 814ba36

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

googletest/include/gtest/gtest.h

+16
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,20 @@ class GTEST_API_ UnitTest {
12621262
// total_test_suite_count() - 1. If i is not in that range, returns NULL.
12631263
TestSuite* GetMutableTestSuite(int i);
12641264

1265+
// Invokes OsStackTrackGetterInterface::UponLeavingGTest. UponLeavingGTest()
1266+
// should be called immediately before Google Test calls user code. It saves
1267+
// some information about the current stack that CurrentStackTrace() will use
1268+
// to find and hide Google Test stack frames.
1269+
void UponLeavingGTest();
1270+
1271+
// Sets the TestSuite object for the test that's currently running.
1272+
void set_current_test_suite(TestSuite* a_current_test_suite)
1273+
GTEST_LOCK_EXCLUDED_(mutex_);
1274+
1275+
// Sets the TestInfo object for the test that's currently running.
1276+
void set_current_test_info(TestInfo* a_current_test_info)
1277+
GTEST_LOCK_EXCLUDED_(mutex_);
1278+
12651279
// Accessors for the implementation object.
12661280
internal::UnitTestImpl* impl() { return impl_; }
12671281
const internal::UnitTestImpl* impl() const { return impl_; }
@@ -1270,6 +1284,8 @@ class GTEST_API_ UnitTest {
12701284
// members of UnitTest.
12711285
friend class ScopedTrace;
12721286
friend class Test;
1287+
friend class TestInfo;
1288+
friend class TestSuite;
12731289
friend class internal::AssertHelper;
12741290
friend class internal::StreamingListenerTest;
12751291
friend class internal::UnitTestRecordPropertyTestHelper;

googletest/src/gtest-internal-inl.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -709,18 +709,6 @@ class GTEST_API_ UnitTestImpl {
709709
return type_parameterized_test_registry_;
710710
}
711711

712-
// Sets the TestSuite object for the test that's currently running.
713-
void set_current_test_suite(TestSuite* a_current_test_suite) {
714-
current_test_suite_ = a_current_test_suite;
715-
}
716-
717-
// Sets the TestInfo object for the test that's currently running. If
718-
// current_test_info is NULL, the assertion results will be stored in
719-
// ad_hoc_test_result_.
720-
void set_current_test_info(TestInfo* a_current_test_info) {
721-
current_test_info_ = a_current_test_info;
722-
}
723-
724712
// Registers all parameterized tests defined using TEST_P and
725713
// INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter
726714
// combination. This method can be called more then once; it has guards
@@ -841,6 +829,18 @@ class GTEST_API_ UnitTestImpl {
841829
// GTEST_FLAG(catch_exceptions) at the moment it starts.
842830
void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
843831

832+
// Sets the TestSuite object for the test that's currently running.
833+
void set_current_test_suite(TestSuite* a_current_test_suite) {
834+
current_test_suite_ = a_current_test_suite;
835+
}
836+
837+
// Sets the TestInfo object for the test that's currently running. If
838+
// current_test_info is NULL, the assertion results will be stored in
839+
// ad_hoc_test_result_.
840+
void set_current_test_info(TestInfo* a_current_test_info) {
841+
current_test_info_ = a_current_test_info;
842+
}
843+
844844
// The UnitTest object that owns this implementation object.
845845
UnitTest* const parent_;
846846

googletest/src/gtest.cc

+31-18
Original file line numberDiff line numberDiff line change
@@ -2836,14 +2836,13 @@ void TestInfo::Run() {
28362836
}
28372837

28382838
// Tells UnitTest where to store test result.
2839-
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
2840-
impl->set_current_test_info(this);
2839+
UnitTest::GetInstance()->set_current_test_info(this);
28412840

28422841
// Notifies the unit test event listeners that a test is about to start.
28432842
repeater->OnTestStart(*this);
28442843
result_.set_start_timestamp(internal::GetTimeInMillis());
28452844
internal::Timer timer;
2846-
impl->os_stack_trace_getter()->UponLeavingGTest();
2845+
UnitTest::GetInstance()->UponLeavingGTest();
28472846

28482847
// Creates the test object.
28492848
Test* const test = internal::HandleExceptionsInMethodIfSupported(
@@ -2861,7 +2860,7 @@ void TestInfo::Run() {
28612860

28622861
if (test != nullptr) {
28632862
// Deletes the test object.
2864-
impl->os_stack_trace_getter()->UponLeavingGTest();
2863+
UnitTest::GetInstance()->UponLeavingGTest();
28652864
internal::HandleExceptionsInMethodIfSupported(
28662865
test, &Test::DeleteSelf_, "the test fixture's destructor");
28672866
}
@@ -2873,15 +2872,14 @@ void TestInfo::Run() {
28732872

28742873
// Tells UnitTest to stop associating assertion results to this
28752874
// test.
2876-
impl->set_current_test_info(nullptr);
2875+
UnitTest::GetInstance()->set_current_test_info(nullptr);
28772876
}
28782877

28792878
// Skip and records a skipped test result for this object.
28802879
void TestInfo::Skip() {
28812880
if (!should_run_) return;
28822881

2883-
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
2884-
impl->set_current_test_info(this);
2882+
UnitTest::GetInstance()->set_current_test_info(this);
28852883

28862884
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
28872885

@@ -2890,12 +2888,13 @@ void TestInfo::Skip() {
28902888

28912889
const TestPartResult test_part_result =
28922890
TestPartResult(TestPartResult::kSkip, this->file(), this->line(), "");
2893-
impl->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult(
2894-
test_part_result);
2891+
internal::GetUnitTestImpl()
2892+
->GetTestPartResultReporterForCurrentThread()
2893+
->ReportTestPartResult(test_part_result);
28952894

28962895
// Notifies the unit test event listener that a test has just finished.
28972896
repeater->OnTestEnd(*this);
2898-
impl->set_current_test_info(nullptr);
2897+
UnitTest::GetInstance()->set_current_test_info(nullptr);
28992898
}
29002899

29012900
// class TestSuite
@@ -2991,8 +2990,7 @@ void TestSuite::AddTestInfo(TestInfo* test_info) {
29912990
void TestSuite::Run() {
29922991
if (!should_run_) return;
29932992

2994-
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
2995-
impl->set_current_test_suite(this);
2993+
UnitTest::GetInstance()->set_current_test_suite(this);
29962994

29972995
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
29982996

@@ -3022,7 +3020,7 @@ void TestSuite::Run() {
30223020
repeater->OnTestCaseStart(*this);
30233021
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
30243022

3025-
impl->os_stack_trace_getter()->UponLeavingGTest();
3023+
UnitTest::GetInstance()->UponLeavingGTest();
30263024
internal::HandleExceptionsInMethodIfSupported(
30273025
this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()");
30283026

@@ -3047,7 +3045,7 @@ void TestSuite::Run() {
30473045
}
30483046
elapsed_time_ = timer.Elapsed();
30493047

3050-
impl->os_stack_trace_getter()->UponLeavingGTest();
3048+
UnitTest::GetInstance()->UponLeavingGTest();
30513049
internal::HandleExceptionsInMethodIfSupported(
30523050
this, &TestSuite::RunTearDownTestSuite, "TearDownTestSuite()");
30533051

@@ -3058,15 +3056,14 @@ void TestSuite::Run() {
30583056
repeater->OnTestCaseEnd(*this);
30593057
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
30603058

3061-
impl->set_current_test_suite(nullptr);
3059+
UnitTest::GetInstance()->set_current_test_suite(nullptr);
30623060
}
30633061

30643062
// Skips all tests under this TestSuite.
30653063
void TestSuite::Skip() {
30663064
if (!should_run_) return;
30673065

3068-
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
3069-
impl->set_current_test_suite(this);
3066+
UnitTest::GetInstance()->set_current_test_suite(this);
30703067

30713068
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
30723069

@@ -3088,7 +3085,7 @@ void TestSuite::Skip() {
30883085
repeater->OnTestCaseEnd(*this);
30893086
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
30903087

3091-
impl->set_current_test_suite(nullptr);
3088+
UnitTest::GetInstance()->set_current_test_suite(nullptr);
30923089
}
30933090

30943091
// Clears the results of all tests in this test suite.
@@ -5304,6 +5301,22 @@ TestSuite* UnitTest::GetMutableTestSuite(int i) {
53045301
return impl()->GetMutableSuiteCase(i);
53055302
}
53065303

5304+
void UnitTest::UponLeavingGTest() {
5305+
impl()->os_stack_trace_getter()->UponLeavingGTest();
5306+
}
5307+
5308+
// Sets the TestSuite object for the test that's currently running.
5309+
void UnitTest::set_current_test_suite(TestSuite* a_current_test_suite) {
5310+
internal::MutexLock lock(&mutex_);
5311+
impl_->set_current_test_suite(a_current_test_suite);
5312+
}
5313+
5314+
// Sets the TestInfo object for the test that's currently running.
5315+
void UnitTest::set_current_test_info(TestInfo* a_current_test_info) {
5316+
internal::MutexLock lock(&mutex_);
5317+
impl_->set_current_test_info(a_current_test_info);
5318+
}
5319+
53075320
// Returns the list of event listeners that can be used to track events
53085321
// inside Google Test.
53095322
TestEventListeners& UnitTest::listeners() { return *impl()->listeners(); }

0 commit comments

Comments
 (0)