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

Signal watcher functionality with test #597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 52 additions & 0 deletions bindings/cpp/posix_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#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 std::atomic<bool> only_once {false};
bool expected = false;
if (only_once.compare_exchange_strong(expected, 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"

const char 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();
}