This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signal watcher functionality with test
- Loading branch information
iivlev
committed
May 7, 2017
1 parent
f35328f
commit e9bcba4
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef POSIX_UTILS_H | ||
#define POSIX_UTILS_H | ||
|
||
namespace wtf { | ||
namespace posix_utils { | ||
|
||
void InstallSignalHandler(int number, char const* filename); | ||
|
||
} // namespace posix_utils | ||
} // namespace wtf | ||
|
||
#endif // POSIX_UTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <wtf/runtime.h> | ||
#include <signal.h> | ||
|
||
#include <thread> | ||
#include <condition_variable> | ||
|
||
namespace { | ||
|
||
std::condition_variable gCv; | ||
std::mutex gMutex; | ||
std::string gFilename; | ||
|
||
void wtfOnSignal(int) { | ||
gCv.notify_all(); | ||
}; | ||
|
||
void wtfSignalWatcherThread () { | ||
std::unique_lock<std::mutex> lock { gMutex }; | ||
while (true) { | ||
gCv.wait(lock); | ||
wtf::Runtime::GetInstance()->SaveToFile(gFilename.c_str()); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
namespace wtf { | ||
namespace posix_utils { | ||
|
||
void InstallSignalHandler(int number, char const* filename) { | ||
if (!filename) { | ||
fprintf(stderr, "%s: Error: filename can't be null\n", __PRETTY_FUNCTION__); | ||
return; | ||
} | ||
|
||
static bool onlyOnce = false; | ||
if (onlyOnce) { | ||
fprintf(stderr, "%s: Error: method be called only once per program execution.\n", __PRETTY_FUNCTION__); | ||
return; | ||
} | ||
|
||
onlyOnce = true; | ||
|
||
gFilename = filename; | ||
std::thread thread { wtfSignalWatcherThread }; | ||
|
||
signal(number, wtfOnSignal); | ||
|
||
thread.detach(); | ||
} | ||
|
||
} // namespace posix_utils | ||
} // namespace wtf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
|
||
#include "wtf/posix_utils.h" | ||
#include "wtf/macros.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
char const* kSignalWatcherFilename = "./tmptstsignal_watcher.wtf-trace"; | ||
|
||
namespace wtf { | ||
|
||
class PosixUtilsTest : public ::testing::Test { | ||
protected: | ||
void TearDown() override { | ||
wtf::Runtime::GetInstance()->DisableCurrentThread(); | ||
wtf::Runtime::GetInstance()->ResetForTesting(); | ||
} | ||
|
||
}; | ||
|
||
|
||
TEST_F(PosixUtilsTest, SignalWatcher) { | ||
unlink(kSignalWatcherFilename); | ||
// watch SIGALRM | ||
wtf::posix_utils::InstallSignalHandler(14, kSignalWatcherFilename); | ||
WTF_EVENT0("MacrosTest#SignalWatcher"); | ||
|
||
alarm(1); | ||
|
||
const int second = 1000; | ||
usleep(5000 * second); | ||
|
||
alarm(0); | ||
// allow the thread to finish it's work | ||
usleep(1000 * second); | ||
struct stat buffer; | ||
|
||
EXPECT_TRUE(stat(kSignalWatcherFilename, &buffer) == 0); | ||
EXPECT_TRUE(buffer.st_size > 0); | ||
} | ||
} //namespace wtf | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |