[gjs/wip/ptomato/internals: 12/12] tests: Move gjs_crash_after_timeout to test utils



commit 8297451aef98fc6d69fb3183091b2ac18135ae57
Author: Philip Chimento <philip endlessm com>
Date:   Thu Oct 20 14:37:43 2016 -0700

    tests: Move gjs_crash_after_timeout to test utils
    
    This function doesn't belong in libgjs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772386

 Makefile.am             |    2 -
 test/gjs-test-utils.cpp |   89 +++++++++++++++++++++++++++++++++
 test/gjs-test-utils.h   |    2 +
 test/gjs-tests.cpp      |    1 -
 util/crash.cpp          |  126 -----------------------------------------------
 util/crash.h            |   34 -------------
 6 files changed, 91 insertions(+), 163 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index aace35d..551b2bf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,8 +98,6 @@ libgjs_la_SOURCES =           \
        util/hash-x32.h                 \
        util/glib.cpp           \
        util/glib.h                     \
-       util/crash.cpp          \
-       util/crash.h                    \
        util/log.cpp            \
        util/log.h                      \
        util/misc.cpp                   \
diff --git a/test/gjs-test-utils.cpp b/test/gjs-test-utils.cpp
index 1fea5e5..990d86a 100644
--- a/test/gjs-test-utils.cpp
+++ b/test/gjs-test-utils.cpp
@@ -1,5 +1,6 @@
 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
 /*
+ * Copyright (c) 2008 litl, LLC
  * Copyright (c) 2016 Endless Mobile, Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -77,3 +78,91 @@ gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
         g_printerr("**\n%s\n", fx->message);
     g_free(fx->message);
 }
+
+/* Fork a process that waits the given time then
+ * sends us ABRT
+ */
+void
+gjs_crash_after_timeout(int seconds)
+{
+    pid_t parent_pid;
+    int pipe_fds[2];
+    fd_set read_fds;
+    struct timeval term_time;
+    struct timeval remaining;
+    struct timeval now;
+    int old_flags;
+
+    /* We use a pipe to know in the child when the parent exited */
+    if (pipe(pipe_fds) != 0) {
+        fprintf(stderr, "Failed to create pipe to crash-in-timeout process: %s\n",
+                strerror(errno));
+        return;
+    }
+
+    /* We want pipe_fds[1] to only be open in the parent process; when it closes
+     * the child will see an EOF. Setting FD_CLOEXEC is protection in case the
+     * parent spawns off some process without properly closing fds.
+     */
+    old_flags = fcntl(pipe_fds[1], F_GETFD);
+    if (old_flags == -1 ||
+        fcntl(pipe_fds[1], F_SETFD, old_flags | FD_CLOEXEC) != 0) {
+        fprintf(stderr, "Couldn't make crash-timeout pipe FD_CLOEXEC: %s\n",
+                strerror(errno));
+        return;
+    }
+
+    parent_pid = getpid();
+
+    switch (fork()) {
+    case -1:
+        fprintf(stderr, "Failed to fork crash-in-timeout process: %s\n",
+                strerror(errno));
+        return;
+    case 0:
+        /* child */
+        break;
+    default:
+        /* parent */
+        close(pipe_fds[0]);
+        return;
+    }
+
+    close (pipe_fds[1]);
+
+    gettimeofday (&now, NULL);
+
+    term_time = now;
+    term_time.tv_sec += seconds;
+
+    FD_ZERO(&read_fds);
+    FD_SET(pipe_fds[0], &read_fds);
+
+    while (true) {
+        remaining.tv_sec = term_time.tv_sec - now.tv_sec;
+        remaining.tv_usec = term_time.tv_usec - now.tv_usec;
+        if (remaining.tv_usec < 0) {
+            remaining.tv_usec += 1000;
+            remaining.tv_sec -= 1;
+        }
+
+        if (remaining.tv_sec < 0) /* expired */
+            break;
+
+        select(pipe_fds[0] + 1, &read_fds, NULL, NULL, &remaining);
+        if (FD_ISSET(pipe_fds[0], &read_fds)) {
+            /* The parent exited */
+            _exit(0);
+        }
+
+        gettimeofday(&now, NULL);
+    }
+
+    if (kill(parent_pid, 0) == 0) {
+        fprintf(stderr, "Timeout of %d seconds expired; aborting process %d\n",
+                seconds, (int) parent_pid);
+        kill(parent_pid, SIGABRT);
+    }
+
+    _exit(1);
+}
diff --git a/test/gjs-test-utils.h b/test/gjs-test-utils.h
index 92e2d9f..f25483a 100644
--- a/test/gjs-test-utils.h
+++ b/test/gjs-test-utils.h
@@ -34,6 +34,8 @@ void gjs_unit_test_fixture_setup(GjsUnitTestFixture *fx,
 void gjs_unit_test_fixture_teardown(GjsUnitTestFixture *fx,
                                     gconstpointer      unused);
 
+void gjs_crash_after_timeout(int seconds);
+
 void gjs_test_add_tests_for_coverage ();
 
 void gjs_test_add_tests_for_parse_call_args(void);
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 9979413..310e82d 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -25,7 +25,6 @@
 #include <glib.h>
 #include <glib-object.h>
 #include <util/glib.h>
-#include <util/crash.h>
 
 #include <gjs/context.h>
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]