Skip to content

Commit f46f3ea

Browse files
author
vladlosev
committed
Adds support for detection of running in death test child processes.
git-svn-id: http://googletest.googlecode.com/svn/trunk@606 861a406c-534a-0410-8894-cb66d6ee9925
1 parent 9781f54 commit f46f3ea

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

include/gtest/gtest-death-test.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ GTEST_DECLARE_string_(death_test_style);
5151

5252
#if GTEST_HAS_DEATH_TEST
5353

54+
namespace internal {
55+
56+
// Returns a Boolean value indicating whether the caller is currently
57+
// executing in the context of the death test child process. Tools such as
58+
// Valgrind heap checkers may need this to modify their behavior in death
59+
// tests. IMPORTANT: This is an internal utility. Using it may break the
60+
// implementation of death tests. User code MUST NOT use it.
61+
GTEST_API_ bool InDeathTestChild();
62+
63+
} // namespace internal
64+
5465
// The following macros are useful for writing death tests.
5566

5667
// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is

src/gtest-death-test.cc

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,42 @@ GTEST_DEFINE_string_(
109109
"Indicates the file, line number, temporal index of "
110110
"the single death test to run, and a file descriptor to "
111111
"which a success code may be sent, all separated by "
112-
"colons. This flag is specified if and only if the current "
112+
"the '|' characters. This flag is specified if and only if the current "
113113
"process is a sub-process launched for running a thread-safe "
114114
"death test. FOR INTERNAL USE ONLY.");
115115
} // namespace internal
116116

117117
#if GTEST_HAS_DEATH_TEST
118118

119+
namespace internal {
120+
121+
// Valid only for fast death tests. Indicates the code is running in the
122+
// child process of a fast style death test.
123+
static bool g_in_fast_death_test_child = false;
124+
125+
// Returns a Boolean value indicating whether the caller is currently
126+
// executing in the context of the death test child process. Tools such as
127+
// Valgrind heap checkers may need this to modify their behavior in death
128+
// tests. IMPORTANT: This is an internal utility. Using it may break the
129+
// implementation of death tests. User code MUST NOT use it.
130+
bool InDeathTestChild() {
131+
# if GTEST_OS_WINDOWS
132+
133+
// On Windows, death tests are thread-safe regardless of the value of the
134+
// death_test_style flag.
135+
return !GTEST_FLAG(internal_run_death_test).empty();
136+
137+
# else
138+
139+
if (GTEST_FLAG(death_test_style) == "threadsafe")
140+
return !GTEST_FLAG(internal_run_death_test).empty();
141+
else
142+
return g_in_fast_death_test_child;
143+
#endif
144+
}
145+
146+
} // namespace internal
147+
119148
// ExitedWithCode constructor.
120149
ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {
121150
}
@@ -825,6 +854,7 @@ DeathTest::TestRole NoExecDeathTest::AssumeRole() {
825854
// Event forwarding to the listeners of event listener API mush be shut
826855
// down in death test subprocesses.
827856
GetUnitTestImpl()->listeners()->SuppressEventForwarding();
857+
g_in_fast_death_test_child = true;
828858
return EXECUTE_TEST;
829859
} else {
830860
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));

test/gtest-death-test_test.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ using testing::internal::DeathTestFactory;
7575
using testing::internal::FilePath;
7676
using testing::internal::GetLastErrnoDescription;
7777
using testing::internal::GetUnitTestImpl;
78+
using testing::internal::InDeathTestChild;
7879
using testing::internal::ParseNaturalNumber;
7980
using testing::internal::String;
8081

@@ -1345,6 +1346,26 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
13451346
#endif // _MSC_VER
13461347
}
13471348

1349+
TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
1350+
testing::GTEST_FLAG(death_test_style) = "fast";
1351+
EXPECT_FALSE(InDeathTestChild());
1352+
EXPECT_DEATH({
1353+
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1354+
fflush(stderr);
1355+
_exit(1);
1356+
}, "Inside");
1357+
}
1358+
1359+
TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
1360+
testing::GTEST_FLAG(death_test_style) = "threadsafe";
1361+
EXPECT_FALSE(InDeathTestChild());
1362+
EXPECT_DEATH({
1363+
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
1364+
fflush(stderr);
1365+
_exit(1);
1366+
}, "Inside");
1367+
}
1368+
13481369
// Tests that a test case whose name ends with "DeathTest" works fine
13491370
// on Windows.
13501371
TEST(NotADeathTest, Test) {

0 commit comments

Comments
 (0)