Written a test case setting up a file system and tearing it down again afterwards

This commit is contained in:
Sebastian Messmer 2014-11-17 21:13:58 +01:00
parent f5a6f79e09
commit c495e6b475
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#include "Daemon.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
using std::function;
Daemon::Daemon(function<void()> 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");
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#ifndef TEST_TESTUTILS_DAEMON_H_
#define TEST_TESTUTILS_DAEMON_H_
#include <functional>
class Daemon {
public:
Daemon(std::function<void()> runnable);
void start();
void stop();
private:
std::function<void()> _runnable;
pid_t _child_pid;
};
#endif