xorg-gtest
0.1
Xorg testing extension to Google Test
|
Class that abstracts child process creation and termination. More...
#include <xorg/gtest/xorg-gtest-process.h>
Public Types | |
enum | State { ERROR, NONE, RUNNING, FINISHED_SUCCESS, FINISHED_FAILURE, TERMINATED } |
Describes the state of a process as seen by this library. More... | |
Public Member Functions | |
Process () | |
Creates a child-process that is in a terminated state. More... | |
pid_t | Fork () |
Fork manually. More... | |
void | Start (const std::string &program, const std::vector< std::string > &args) |
Starts a program as a child process. More... | |
void | Start (const std::string &program, va_list *args) |
Starts a program as a child process. More... | |
void | Start (const std::string &program,...) _X_SENTINEL(0) |
Starts a program as a child process. More... | |
virtual bool | Terminate (unsigned int timeout=0) |
Terminates (SIGTERM) this child process and waits a given timeout for the process to terminate. More... | |
virtual bool | Kill (unsigned int timeout=0) |
Kills (SIGKILL) this child process and waits a given timeout for the process to terminate. More... | |
pid_t | Pid () const |
Accesses the pid of the child process. More... | |
enum Process::State | GetState () |
Return the state of the process. More... | |
Static Public Member Functions | |
static void | SetEnv (const std::string &name, const std::string &value, bool overwrite) |
Helper function to adjust the environment of the current process. More... | |
static std::string | GetEnv (const std::string &name, bool *exists=NULL) |
Helper function to query the environment of the current process. More... | |
Class that abstracts child process creation and termination.
This class allows for forking, running and terminating child processes. In addition, manipulation of the child process' environment is supported. For example, starting an X server instance on display port 133 as a child process can be realized with the following code snippet:
Describes the state of a process as seen by this library.
This state changes some behaviors inside the library, most notably:
xorg::testing::Process::Process | ( | ) |
Creates a child-process that is in a terminated state.
pid_t xorg::testing::Process::Fork | ( | ) |
Fork manually.
Usually, fork() is called as part of Start() but for use-cases where the parent process and the child process need special processing before the child is replaced by an execvp call Fork() may be called manually.
A process may only be forked once.
The state of both the parent and the child after a Fork() is Process::RUNNING. If Fork() is called directly, Start() may only be called on the child process.
std::runtime_error | on failure. |
|
static |
Helper function to query the environment of the current process.
[in] | name | The name of the environment variable. |
[out] | exists | If not NULL, the variable will be set to true if the environment variable exists and to false otherwise. |
enum Process::State xorg::testing::Process::GetState | ( | ) |
Return the state of the process.
|
virtual |
Kills (SIGKILL) this child process and waits a given timeout for the process to terminate.
[in] | timeout | The timeout in millis to wait for the process to terminate. A timeout of 0 implies not to wait but return immediately. |
std::runtime_error | if child tries to kill itself. |
Reimplemented in xorg::testing::XServer.
pid_t xorg::testing::Process::Pid | ( | ) | const |
Accesses the pid of the child process.
|
static |
Helper function to adjust the environment of the current process.
[in] | name | Name of the environment variable. |
[in] | value | Value of the environment variable. |
[in] | overwrite | Whether to overwrite the value of existing env variables. |
std::runtime_error | if adjusting the environment does not succeed. |
void xorg::testing::Process::Start | ( | const std::string & | program, |
const std::vector< std::string > & | args | ||
) |
Starts a program as a child process.
See 'man execvp' for further information on the elements in the vector.
If Fork() was called previously, Start() may only be called on the child process.
program | The program to start. |
args | Vector of arguments passed to the program. |
std::runtime_error | on failure. |
void xorg::testing::Process::Start | ( | const std::string & | program, |
va_list * | args | ||
) |
Starts a program as a child process.
See 'man execvp' for further information on the variadic argument list.
If Fork() was called previously, Start() may only be called on the child process.
program | The program to start. |
args | Pointer to a variadic list of arguments passed to the program. This list must end with NULL. |
std::runtime_error | on failure. |
void xorg::testing::Process::Start | ( | const std::string & | program, |
... | |||
) |
Starts a program as a child process.
Takes a variadic list of arguments passed to the program. This list must end with NULL. See 'man execvp' for further information on the variadic argument list.
If Fork() was called previously, Start() may only be called on the child process.
program | The program to start. |
std::runtime_error | on failure. |
|
virtual |
Terminates (SIGTERM) this child process and waits a given timeout for the process to terminate.
[in] | timeout | The timeout in millis to wait for the process to terminate. A timeout of 0 implies not to wait but return immediately. |
std::runtime_error | if child tries to terminate itself. |
Reimplemented in xorg::testing::XServer.