-
Notifications
You must be signed in to change notification settings - Fork 8
/
DeathTest.cpp
232 lines (192 loc) · 7.19 KB
/
DeathTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <thread>
#include <future>
#include "DeathTest.h"
#include <Death.h>
#include <FileIO.h>
#include <cassert>
bool DeathTest::ranEcho(false);
std::vector<Death::DeathCallbackArg> DeathTest::stringsEchoed;
std::atomic<int> DeathTest::ranTimes(0);
TEST(DeathTest, DeleteIpcFilesRemovesIPCFiles) {
DeathTest::ranEcho = false;
DeathTest::ranTimes.store(0);
DeathTest::stringsEchoed.clear();
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
Death::Instance().RegisterDeathEvent(&Death::DeleteIpcFiles, "ipc:///tmp/test.ipc");
unlink("/tmp/test.ipc");
ASSERT_FALSE(FileIO::DoesFileExist("/tmp/test.ipc"));
ASSERT_FALSE(FileIO::WriteAsciiFileContent("/tmp/test.ipc", "test").HasFailed());
ASSERT_TRUE(FileIO::DoesFileExist("/tmp/test.ipc"));
CHECK(false);
ASSERT_FALSE(FileIO::DoesFileExist("/tmp/test.ipc"));
}
TEST(DeathTest, DeleteIpcFilesDoesntRemoveNonIPCFiles) {
DeathTest::ranEcho = false;
DeathTest::ranTimes.store(0);
DeathTest::stringsEchoed.clear();
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
Death::Instance().RegisterDeathEvent(&Death::DeleteIpcFiles, "tcp:///tmp/test.deathtest");
unlink("/tmp/test.deathtest");
ASSERT_FALSE(FileIO::DoesFileExist("/tmp/test.deathtest"));
ASSERT_FALSE(FileIO::WriteAsciiFileContent("/tmp/test.deathtest", "test").HasFailed());
ASSERT_TRUE(FileIO::DoesFileExist("/tmp/test.deathtest"));
CHECK(false);
ASSERT_TRUE(FileIO::DoesFileExist("/tmp/test.deathtest"));
unlink("/tmp/test.deathtest");
}
void DeathTest::EchoTheString(const std::string& theString) {
stringsEchoed.push_back(theString);
ranEcho = true;
}
void DeathTest::RaceTest(const Death::DeathCallbackArg& theString) {
if (!ranEcho) {
std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 16000));
ranEcho = !ranEcho;
stringsEchoed.push_back(theString);
}
ranTimes++;
}
TEST(DeathTest, VerifySingleton) {
Death & instance1(Death::Instance());
Death & instance2(Death::Instance());
ASSERT_EQ(&instance1, &instance2);
}
TEST(DeathTest, VerifyReceiveCheck) {
RaiiDeathCleanup cleanup;
Death::ClearExits();
Death::Instance().SetupExitHandler();
EXPECT_FALSE(Death::Instance().WasKilled());
CHECK(false);
EXPECT_TRUE(Death::Instance().WasKilled());
}
TEST(DeathTest, VerifyReceiveSignal) {
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
EXPECT_FALSE(Death::Instance().WasKilled());
raise(SIGSEGV);
EXPECT_TRUE(Death::Instance().WasKilled());
}
namespace {
size_t gDeathCounter = 0;
}
TEST(DeathTest, VerifyRecursiveCrash) {
RaiiDeathCleanup cleanup;
Death::ClearExits();
Death::Instance().SetupExitHandler();
gDeathCounter = 0;
auto deathCounter = [](const Death::DeathCallbackArg& arg) {
std::cout << "Death message: " << arg << std::endl;
++gDeathCounter;
assert(gDeathCounter < 10); // just sanity check in case logic changes
CHECK(false);
};
Death::Instance().RegisterDeathEvent(deathCounter, "test");
EXPECT_FALSE(Death::Instance().WasKilled());
CHECK(false);
EXPECT_TRUE(Death::Instance().WasKilled());
EXPECT_EQ(gDeathCounter, 1);
}
TEST(DeathTest, ResetWorks) {
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
EXPECT_FALSE(Death::Instance().WasKilled());
CHECK(false);
EXPECT_TRUE(Death::Instance().WasKilled());
Death::Instance().ClearExits();
EXPECT_FALSE(Death::Instance().WasKilled());
}
TEST(DeathTest, RegisterSomething) {
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
Death::Instance().RegisterDeathEvent(&DeathTest::EchoTheString, "test");
EXPECT_FALSE(DeathTest::ranEcho);
EXPECT_TRUE(DeathTest::stringsEchoed.empty());
CHECK(false);
EXPECT_TRUE(DeathTest::ranEcho);
EXPECT_FALSE(DeathTest::stringsEchoed.empty());
EXPECT_EQ("test", DeathTest::stringsEchoed[0]);
}
TEST(DeathTest, VerifyThreadDeathEventRegistration) {
DeathTest::ranEcho = false;
DeathTest::ranTimes.store(0);
DeathTest::stringsEchoed.clear();
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
EXPECT_FALSE(DeathTest::ranEcho);
EXPECT_TRUE(DeathTest::stringsEchoed.empty());
auto ManyThreads = [&]() {
Death::Instance().RegisterDeathEvent(&DeathTest::RaceTest, "race");
};
std::vector<std::future<void>> waitingPromises;
for (int i = 0; i < 10; i++) {
waitingPromises.push_back(std::async(std::launch::async, ManyThreads));
}
for (auto& waitFor : waitingPromises) {
waitFor.get();
}
CHECK(false);
EXPECT_TRUE(DeathTest::ranEcho);
EXPECT_FALSE(DeathTest::stringsEchoed.empty());
EXPECT_EQ(10, DeathTest::ranTimes);
EXPECT_EQ(1,DeathTest::stringsEchoed.size());
EXPECT_EQ("race", DeathTest::stringsEchoed[0]);
}
TEST(DeathTest, VerifySimultaneousDeathAndDeathEventRegistration) {
DeathTest::ranEcho = false;
DeathTest::ranTimes.store(0);
DeathTest::stringsEchoed.clear();
RaiiDeathCleanup cleanup;
Death::Instance().SetupExitHandler();
const size_t kNumberOfThreads = 10;
EXPECT_FALSE(DeathTest::ranEcho);
EXPECT_TRUE(DeathTest::stringsEchoed.empty());
std::atomic<bool> keepAlive{true};
std::atomic<size_t> runningCounter{0};
auto ManyThreadsDie = [&]() {
++runningCounter;
Death::Instance().RegisterDeathEvent(&DeathTest::RaceTest, "race");
while(keepAlive) {
std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 16000));
}
CHECK(false);
};
std::vector<std::future<void>> waitingPromises;
for (int i = 0; i < kNumberOfThreads; i++) {
waitingPromises.push_back(std::async(std::launch::async, ManyThreadsDie));
}
// wait for all threads to be running before triggering FATAL event
while(runningCounter < kNumberOfThreads) {
std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 16000));
}
// trigger fatal exit in all threads
keepAlive.store(false);
// wait for all threads to finish
for (auto& waitFor : waitingPromises) {
waitFor.get();
}
EXPECT_TRUE(DeathTest::ranEcho);
EXPECT_FALSE(DeathTest::stringsEchoed.empty());
// 10 threads will trigger each a FATAL event that will each time
// trigger the death callback. I.e. 10 * 10 events
EXPECT_EQ(kNumberOfThreads * kNumberOfThreads, DeathTest::ranTimes);
EXPECT_EQ(1,DeathTest::stringsEchoed.size());
EXPECT_EQ("race", DeathTest::stringsEchoed[0]);
}
// --gtest_also_run_disabled_tests
TEST(DeathTest, DISABLED_VerifyReceiveSignalAndExitForReal) {
std::cout << "Running this test will kill the test process ... keep it disabled if possible" << std::endl;
std::cout << "If the test succeeds then another printout will come that says 'Death message: SUCCESS'" << std::endl;
RaiiDeathCleanup cleanup;
auto deathTestCallback = [](const Death::DeathCallbackArg& arg) {
std::cout << "Death message: " << arg << std::endl;
CHECK(false); // make it a recursive death for good measure
};
Death::EnableDefaultFatalCall();
std::string msg = {"SUCCESS"};
Death::RegisterDeathEvent(deathTestCallback, msg);
EXPECT_FALSE(Death::Instance().WasKilled());
raise(SIGSEGV);
EXPECT_TRUE(Death::Instance().WasKilled());
}