Written a test case setting up a file system and tearing it down again afterwards
This commit is contained in:
parent
f5a6f79e09
commit
c495e6b475
32
src/test/testutils/Daemon.cpp
Normal file
32
src/test/testutils/Daemon.cpp
Normal 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");
|
||||
}
|
||||
}
|
18
src/test/testutils/Daemon.h
Normal file
18
src/test/testutils/Daemon.h
Normal 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
|
Loading…
Reference in New Issue
Block a user