From c495e6b4753d11b0104819ce6bc4de9bbd1e3de0 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Mon, 17 Nov 2014 21:13:58 +0100 Subject: [PATCH] Written a test case setting up a file system and tearing it down again afterwards --- src/test/testutils/Daemon.cpp | 32 ++++++++++++++++++++++++++++++++ src/test/testutils/Daemon.h | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/test/testutils/Daemon.cpp create mode 100644 src/test/testutils/Daemon.h diff --git a/src/test/testutils/Daemon.cpp b/src/test/testutils/Daemon.cpp new file mode 100644 index 00000000..675e1273 --- /dev/null +++ b/src/test/testutils/Daemon.cpp @@ -0,0 +1,32 @@ +#include "Daemon.h" + +#include +#include +#include +#include + +using std::function; + +Daemon::Daemon(function runnable) + : _runnable(runnable), _child_pid(0) { +} + +void Daemon::start() { + _child_pid = fork(); + if (_child_pid == 0) { + _runnable(); + exit(0); + } +} + +void Daemon::stop() { + int retval = kill(_child_pid, SIGINT); + if (retval != 0) { + throw std::runtime_error("Failed killing child process"); + } + int status; + pid_t pid = waitpid(_child_pid, &status, 0); + if (pid != _child_pid) { + throw std::runtime_error("Failed waiting for child process to die"); + } +} diff --git a/src/test/testutils/Daemon.h b/src/test/testutils/Daemon.h new file mode 100644 index 00000000..36b861f9 --- /dev/null +++ b/src/test/testutils/Daemon.h @@ -0,0 +1,18 @@ +#pragma once +#ifndef TEST_TESTUTILS_DAEMON_H_ +#define TEST_TESTUTILS_DAEMON_H_ + +#include + +class Daemon { +public: + Daemon(std::function runnable); + void start(); + void stop(); + +private: + std::function _runnable; + pid_t _child_pid; +}; + +#endif