[waffle] [PATCH 7/8] test/gl_basic_test: implement windows version for run_testsuite()

Emil Velikov emil.l.velikov at gmail.com
Mon Aug 4 07:34:22 PDT 2014


Windows lacks fork, waitid and co. As such create a separate thread
rather than cloning the current process.

The thread itself runs the tests, as such properly annotate the
testsuite_wgl functions' calling convention.

Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
---
 include/waffle_test/waffle_test.h |  4 +++
 src/waffle_test/wt_main.c         |  4 +++
 tests/functional/gl_basic_test.c  | 62 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)

diff --git a/include/waffle_test/waffle_test.h b/include/waffle_test/waffle_test.h
index c7517e8..e0cd33e 100644
--- a/include/waffle_test/waffle_test.h
+++ b/include/waffle_test/waffle_test.h
@@ -50,7 +50,11 @@
 /// @param test_runners is a list of functions that call TEST_RUN(). The list
 ///     is a null-terminated.
 /// @return number of failed tests.
+#ifdef _WIN32
+int wt_main(int *argc, char **argv, void (__stdcall *test_suites[])(void));
+#else
 int wt_main(int *argc, char **argv, void (*test_suites[])(void));
+#endif // _WIN32
 
 #define TEST_PASS()                           wt_test_pass()
 #define TEST_SKIP()                           wt_test_skip()
diff --git a/src/waffle_test/wt_main.c b/src/waffle_test/wt_main.c
index 205c83c..b3e6580 100644
--- a/src/waffle_test/wt_main.c
+++ b/src/waffle_test/wt_main.c
@@ -27,7 +27,11 @@
 #include "priv/wt_runner.h"
 
 int
+#ifdef _WIN32
+wt_main(int *argc, char **argv, void (__stdcall *test_suites[])(void))
+#else
 wt_main(int *argc, char **argv, void (*test_suites[])(void))
+#endif // _WIN32
 {
     int num_fail;
 
diff --git a/tests/functional/gl_basic_test.c b/tests/functional/gl_basic_test.c
index 44e37bc..95caf24 100644
--- a/tests/functional/gl_basic_test.c
+++ b/tests/functional/gl_basic_test.c
@@ -39,6 +39,8 @@
 #include <string.h>
 #if !defined(_WIN32)
 #include <unistd.h>
+#else
+#include <windows.h>
 #endif
 
 #include <sys/types.h>
@@ -1098,6 +1100,64 @@ usage_error(void)
     exit(1);
 }
 
+#ifdef _WIN32
+static DWORD __stdcall
+worker_thread(LPVOID lpParam)
+{
+    void (__stdcall *testsuite)(void) = lpParam;
+    void (__stdcall *testsuites[])(void) = {testsuite, 0};
+    int argc = 0;
+    DWORD num_failures = wt_main(&argc, NULL, testsuites);
+
+    return num_failures;
+}
+
+/// Run the testsuite in a separate thread. If the testsuite fails, then exit
+/// immediately.
+static void
+run_testsuite(void (*testsuite)(void))
+{
+    HANDLE hThread;
+    DWORD dwThreadId;
+
+    hThread = CreateThread(NULL, 0, worker_thread, testsuite, 0, &dwThreadId);
+    if (hThread != NULL) {
+        // XXX: Add a decent timeout interval
+        if (WaitForSingleObject(hThread, INFINITE) == WAIT_OBJECT_0) {
+            DWORD exit_status;
+
+            if (GetExitCodeThread(hThread, &exit_status)) {
+                // exit_status is number of failures.
+                if (exit_status == 0) {
+                    // All tests passed.
+                    CloseHandle(hThread);
+                    return;
+                }
+                else {
+                    // Some tests failed. Don't run any more tests.
+                    exit(exit_status);
+                    // or exit process ?
+                }
+            }
+            else {
+                fprintf(stderr, "gl_basic_test: error: get thread exit status"
+                                " failed (%d)\n", GetLastError());
+                abort();
+            }
+        }
+        else {
+            fprintf(stderr, "gl_basic_test: error: wait for thread failed\n");
+            abort();
+        }
+    }
+    else {
+        fprintf(stderr, "gl_basic_test: error: CreateThread failed\n");
+        abort();
+    }
+}
+
+#else
+
 /// Run the testsuite in a separate process. If the testsuite fails, then exit
 /// immediately.
 static void
@@ -1144,6 +1204,8 @@ run_testsuite(void (*testsuite)(void))
     }
 }
 
+#endif // _WIN32
+
 int
 main(int argc, char *argv[])
 {
-- 
2.0.2



More information about the waffle mailing list