diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2012-10-10 11:05:40 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2012-10-10 15:12:23 +1000 |
commit | f4062d1f3e4a2c42732cc6c9cbfa78505ef66ee2 (patch) | |
tree | a8d95d6ff6e05cd323f73743ca4e9efdc236c0dc /test | |
parent | b9312e5abac029d7011db2b93e30bbb65dba6e5b (diff) |
test/process-test: add test for starting the same process object multiple times
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Chase Douglas <chase.douglas@ubuntu.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/process-test.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/test/process-test.cpp b/test/process-test.cpp index 0ed47aa..935035f 100644 --- a/test/process-test.cpp +++ b/test/process-test.cpp @@ -6,6 +6,8 @@ #include <gtest/gtest.h> #include <xorg/gtest/xorg-gtest.h> +#include <stdexcept> + using namespace xorg::testing; TEST(Process, StartWithNULLArg) @@ -143,6 +145,90 @@ TEST(Process, KillExitStatus) ASSERT_EQ(p.GetState(), Process::FINISHED_FAILURE); } +TEST(Process, DoubleStart) +{ + struct timespec sig_timeout = {0, 5000000L}; + + SCOPED_TRACE("TESTCASE: starting a process after it has been started\n" + "fails. Re-starting a process succeeds\n"); + + /* Process double-started must fail */ + Process p; + p.Start("echo", "-n", NULL); + try { + p.Start("echo", "-n", NULL);; + FAIL() << "Expected exception"; + } catch (std::runtime_error &e) { + } + p.Terminate(1000); + /* we sent it a SIGTERM, counts as failure */ + ASSERT_EQ(p.GetState(), Process::FINISHED_FAILURE); + + sigset_t sig_mask; + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGCHLD); + sigaddset(&sig_mask, SIGUSR1); + sigprocmask(SIG_BLOCK, &sig_mask, 0); + + /* restart job after a failed one, must succeed */ + try { + p.Start("echo", "-n", NULL); + } catch (std::runtime_error &e) { + FAIL(); + } + + ASSERT_EQ(sigtimedwait(&sig_mask, NULL, &sig_timeout), SIGCHLD); + ASSERT_EQ(p.GetState(), Process::FINISHED_SUCCESS); + + /* restart job after successful one, must succeed */ + try { + p.Start("echo", "-n", NULL); + } catch (std::runtime_error &e) { + FAIL(); + } + ASSERT_EQ(sigtimedwait(&sig_mask, NULL, &sig_timeout), SIGCHLD); + ASSERT_EQ(p.GetState(), Process::FINISHED_SUCCESS); + + /* job that must be killed, followed by job */ + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGUSR1); + p.Start(TEST_ROOT_DIR "process-test-helper", NULL); + sigtimedwait(&sig_mask, NULL, &sig_timeout); + ASSERT_EQ(p.GetState(), Process::RUNNING); + p.Kill(100); + ASSERT_EQ(p.GetState(), Process::FINISHED_FAILURE); + + /* restart job after successful one, must succeed */ + try { + p.Start("echo", "-n", NULL); + } catch (std::runtime_error &e) { + FAIL(); + } + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGCHLD); + ASSERT_EQ(sigtimedwait(&sig_mask, NULL, &sig_timeout), SIGCHLD); + ASSERT_EQ(p.GetState(), Process::FINISHED_SUCCESS); + + /* job that fails to terminate, starting another one must fail */ + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGUSR1); + p.Start(TEST_ROOT_DIR "process-test-helper", NULL); + sigtimedwait(&sig_mask, NULL, &sig_timeout); + ASSERT_EQ(p.GetState(), Process::RUNNING); + ASSERT_FALSE(p.Terminate(100)); + + try { + p.Start("echo", "-n", NULL); + FAIL() << "exception expected"; + } catch (std::runtime_error &e) { + } + + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGCHLD); + sigaddset(&sig_mask, SIGUSR1); + sigprocmask(SIG_UNBLOCK, &sig_mask, 0); +} + int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); |