Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Signal watcher functionality with test
Browse files Browse the repository at this point in the history
  • Loading branch information
iivlev committed May 7, 2017
1 parent f35328f commit e9bcba4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bindings/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ else
$(error Expected value of THREADING to be single/pthread/std)
endif

ifneq "$(THREADING)" "std"
override POSIX_SUPPORT = "0"
endif

# Required flag customizations.
override CPPFLAGS += -Iinclude
override CPPFLAGS += -I$(GTEST_DIR)/include
Expand All @@ -67,6 +71,10 @@ PLATFORM_HEADERS := \
include/wtf/platform/platform_myriad2sparc_impl.h \
include/wtf/platform/platform_myriad2sparc_inl.h

ifeq "$(POSIX_SUPPORT)" "1"
LIBRARY_HEADERS += include/wtf/posix_utils.h
endif

ALL_HEADERS := $(LIBRARY_HEADERS) $(PLATFORM_HEADERS)

LIBRARY_SOURCES := \
Expand All @@ -81,6 +89,11 @@ TEST_SOURCES := \
runtime_test.cc \
threaded_torture_test.cc

ifeq "$(POSIX_SUPPORT)" "1"
LIBRARY_SOURCES += posix_utils.cc
TEST_SOURCES += posix_utils_test.cc
endif

LIBRARY_OBJECTS := $(LIBRARY_SOURCES:%.cc=%.o)

.PHONY: clean all test
Expand Down Expand Up @@ -113,7 +126,13 @@ clean:
$(wildcard tmp*.wtf-trace)

### TESTING.
ifneq "$(POSIX_SUPPORT)" "1"
test: buffer_test macros_test runtime_test threaded_torture_test
else
test: buffer_test macros_test runtime_test threaded_torture_test posix_utils_test
@echo "Running posix_utils_test"
./posix_utils_test
endif
@echo "Running buffer_test"
./buffer_test
@echo "Running macros_test"
Expand All @@ -138,6 +157,9 @@ macros_test: macros_test.o gtest.o libwtf.a
runtime_test: runtime_test.o gtest.o libwtf.a
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)

posix_utils_test: posix_utils_test.o gtest.o libwtf.a
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $+ $(LDLIBS)

### THREADED TORTURE TEST
ifneq "$(THREADING)" "single"
threaded_torture_test: threaded_torture_test.o libwtf.a
Expand Down
12 changes: 12 additions & 0 deletions bindings/cpp/include/wtf/posix_utils.h
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
53 changes: 53 additions & 0 deletions bindings/cpp/posix_utils.cc
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
47 changes: 47 additions & 0 deletions bindings/cpp/posix_utils_test.cc
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();
}

0 comments on commit e9bcba4

Please sign in to comment.