Refactored a bit.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Mon, 18 Jul 2011 15:25:54 +0300
changeset 5768 40d331dc9c9e
parent 5767 36f7413f460f
child 5769 8f51d6a55006
Refactored a bit.
test/test-automation/runner.c
--- a/test/test-automation/runner.c	Mon Jul 18 14:41:01 2011 +0300
+++ b/test/test-automation/runner.c	Mon Jul 18 15:25:54 2011 +0300
@@ -131,6 +131,7 @@
 TestCaseSetUpFp LoadTestSetUpFunction(void *suite);
 TestCaseTearDownFp LoadTestTearDownFunction(void *suite);
 CountFailedAssertsFp LoadCountFailedAssertsFunction(void *suite);
+void KillHungTest(int signum);
 
 
 /*! Pointers to selected logger implementation */
@@ -574,6 +575,31 @@
 
 
 /*!
+ * Set timeout for test.
+ *
+ * \param timeout Timeout interval in seconds!
+ * \param callback Function that will be called after timeout has elapsed
+ */
+void SetTestTimeout(int timeout, void (*callback)(int))
+{
+#if 0
+	/* Note:
+	 * SDL_Init(SDL_INIT_TIMER) should be successfully called before using this
+	 */
+	int timeoutInMilliseconds = timeout * 1000;
+	SDL_TimerID timerID = SDL_AddTimer(timeoutInMilliseconds, callback, 0x0);
+	if(timerID == NULL) {
+		fprintf(stderr, "Error: Creation of SDL timer failed.\n");
+		fprintf(stderr, "%s\n", SDL_GetError());
+	}
+#else
+	signal(SIGALRM, callback);
+	alarm((unsigned int) timeout);
+#endif
+}
+
+
+/*!
  * Kills test that hungs. Test hungs when its execution
  * takes longer than timeout specified for it.
  *
@@ -585,7 +611,8 @@
  *
  * \param signum
  */
-void KillHungTest(int signum) {
+void KillHungTest(int signum)
+{
 	exit(TEST_RESULT_KILLED);
 }
 
@@ -598,7 +625,17 @@
  * \param test result
  */
 int
-RunTest(TestCase *testItem) {
+RunTest(TestCase *testItem)
+{
+	if(testItem->timeout > 0) {
+		if(execute_inproc) {
+			Log("Test asked for timeout which is not supported.", time(0));
+		}
+		else {
+			SetTestTimeout(testItem->timeout, KillHungTest);
+		}
+	}
+
 	testItem->initTestEnvironment();
 
 	if(testItem->testSetUp) {
@@ -632,18 +669,10 @@
 	int retVal = -1;
 
 	if(execute_inproc) {
-		if(testItem->timeout > 0) {
-			Log("Test asked for timeout which is not supported.", time(0));
-		}
 		retVal = RunTest(testItem);
 	} else {
 		int childpid = fork();
 		if(childpid == 0) {
-			if(testItem->timeout > 0) {
-				signal(SIGALRM, KillHungTest);
-				alarm((unsigned int) testItem->timeout);
-			}
-
 			exit(RunTest(testItem));
 		} else {
 			int stat_lock = -1;