-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDeath.h
56 lines (49 loc) · 1.54 KB
/
Death.h
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
#pragma once
#include <string>
#include <g3log/g3log.hpp>
#include <mutex>
#include <vector>
#include <functional>
/**
* By calling @ref UseDeathHandler all CHECK, LOG(FATAL) or fatal signals will be caught by g2log
* but will instead of exiting the test/application call @ref Death::Received
*
* The reason for using this instead of Google's gtest DEATH framework is that the DEATH framework
* will do popen/fork which clashes with internal usage of process forking.
*/
class Death {
public:
using DeathCallbackArg = std::string;
using DeathCallbackType = void (*)(const DeathCallbackArg& arg);
static Death& Instance();
static void ClearExits();
static bool WasKilled();
static void SetupExitHandler();
static std::string Message();
static void RegisterDeathEvent(DeathCallbackType deathFunction, const DeathCallbackArg& deathArg);
static void EnableDefaultFatalCall();
static void DeleteIpcFiles(const std::string& binding);
private:
Death();
Death(Death&) = delete;
Death& operator=(Death&) = delete;
static void Received(g3::FatalMessagePtr death);
bool mReceived;
std::string mMessage;
std::mutex mListLock;
std::vector<std::pair<DeathCallbackType, DeathCallbackArg> > mShutdownFunctions;
bool mEnableDefaultFatal;
};
/** Makes sure that any Death tests will be cleaned up at test exit
* Please add this to your test
*
* TEST_F(Something, Something) {
* RaiiDeathCleanup cleanup;
* ... // test logic
* }
*/
struct RaiiDeathCleanup {
~RaiiDeathCleanup() {
Death::ClearExits();
}
};