[PATCH xorg-gtest 6/8] test/process-test: add test for starting the same process object multiple times

Peter Hutterer peter.hutterer at who-t.net
Tue Oct 9 22:14:31 PDT 2012


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 test/process-test.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

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();
-- 
1.7.11.4



More information about the xorg-devel mailing list