[libsoup] tests: parallelize proxy-test to make it faster



commit a93481f9c50c50ce419ba0b20e50296bc17da8f6
Author: Dan Winship <danw gnome org>
Date:   Fri Aug 10 16:53:30 2012 -0400

    tests: parallelize proxy-test to make it faster
    
    TODO: parallelize auth-test and timeout-test, make it easier to use.

 tests/proxy-test.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++-----
 tests/test-utils.c |    7 +++++
 tests/test-utils.h |    1 +
 3 files changed, 67 insertions(+), 7 deletions(-)
---
diff --git a/tests/proxy-test.c b/tests/proxy-test.c
index 35c9d84..ee0197a 100644
--- a/tests/proxy-test.c
+++ b/tests/proxy-test.c
@@ -15,7 +15,7 @@ static SoupProxyTest tests[] = {
 	{ "GET -> 401 -> 401", "/Basic/realm2/", SOUP_STATUS_UNAUTHORIZED },
 	{ "GET -> 403", "http://no-such-hostname.xx/";, SOUP_STATUS_FORBIDDEN },
 };
-static int ntests = sizeof (tests) / sizeof (tests[0]);
+static const int ntests = sizeof (tests) / sizeof (tests[0]);
 
 #define HTTP_SERVER    "http://127.0.0.1:47524";
 #define HTTPS_SERVER   "https://127.0.0.1:47525";
@@ -97,6 +97,7 @@ test_url (const char *url, int proxy, guint expected,
 	proxy_uri = soup_uri_new (proxies[proxy]);
 	session = soup_test_session_new (sync ? SOUP_TYPE_SESSION_SYNC : SOUP_TYPE_SESSION_ASYNC,
 					 SOUP_SESSION_PROXY_URI, proxy_uri,
+					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
 					 NULL);
 	soup_uri_free (proxy_uri);
 	g_signal_connect (session, "authenticate",
@@ -235,6 +236,62 @@ run_test (int i, gboolean sync)
 	debug_printf (1, "\n");
 }
 
+static gpointer
+async_proxy_test_thread (gpointer num)
+{
+	GMainContext *context = g_main_context_new ();
+
+	g_main_context_push_thread_default (context);
+	run_test (GPOINTER_TO_INT (num), FALSE);
+	g_main_context_pop_thread_default (context);
+
+	return NULL;
+}
+
+static gpointer
+sync_proxy_test_thread (gpointer num)
+{
+	run_test (GPOINTER_TO_INT (num), TRUE);
+	return NULL;
+}
+
+static void
+do_proxy_tests (void)
+{
+	int i;
+
+	debug_printf (1, "Basic proxy tests\n");
+
+	if (parallelize) {
+		GThread *threads[ntests];
+
+		/* Doing the sync and async tests separately is faster
+		 * than doing them both at the same time (hitting
+		 * apache's connection limit maybe?)
+		 */
+		for (i = 0; i < ntests; i++) {
+			threads[i] = g_thread_new ("async_proxy_test",
+						   async_proxy_test_thread,
+						   GINT_TO_POINTER (i));
+		}
+		for (i = 0; i < ntests; i++)
+			g_thread_join (threads[i]);
+
+		for (i = 0; i < ntests; i++) {
+			threads[i] = g_thread_new ("sync_proxy_test",
+						   sync_proxy_test_thread,
+						   GINT_TO_POINTER (i));
+		}
+		for (i = 0; i < ntests; i++)
+			g_thread_join (threads[i]);
+	} else {
+		for (i = 0; i < ntests; i++) {
+			run_test (i, FALSE);
+			run_test (i, TRUE);
+		}
+	}
+}
+
 static void
 server_callback (SoupServer *server, SoupMessage *msg,
 		 const char *path, GHashTable *query,
@@ -319,21 +376,16 @@ main (int argc, char **argv)
 {
 	SoupServer *server;
 	SoupURI *base_uri;
-	int i;
 
 	test_init (argc, argv, NULL);
 	apache_init ();
 
-	for (i = 0; i < ntests; i++) {
-		run_test (i, FALSE);
-		run_test (i, TRUE);
-	}
-
 	server = soup_test_server_new (TRUE);
 	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
 	base_uri = soup_uri_new ("http://127.0.0.1/";);
 	soup_uri_set_port (base_uri, soup_server_get_port (server));
 
+	do_proxy_tests ();
 	do_proxy_fragment_test (base_uri);
 	do_proxy_redirect_test ();
 
diff --git a/tests/test-utils.c b/tests/test-utils.c
index 606de32..c34b0b7 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -14,6 +14,7 @@ static gboolean apache_running;
 static SoupLogger *logger;
 
 int debug_level, errors;
+gboolean parallelize = TRUE;
 gboolean expect_warning, tls_available;
 static int http_debug_level;
 
@@ -37,6 +38,9 @@ static GOptionEntry debug_entry[] = {
 	{ "debug", 'd', G_OPTION_FLAG_NO_ARG,
 	  G_OPTION_ARG_CALLBACK, increment_debug_level,
 	  "Enable (or increase) test-specific debugging", NULL },
+	{ "parallel", 'p', G_OPTION_FLAG_REVERSE,
+	  G_OPTION_ARG_NONE, &parallelize,
+	  "Toggle parallelization (default is on, unless -d or -h)", NULL },
 	{ "http-debug", 'h', G_OPTION_FLAG_NO_ARG,
 	  G_OPTION_ARG_CALLBACK, increment_http_debug_level,
 	  "Enable (or increase) HTTP-level debugging", NULL },
@@ -101,6 +105,9 @@ test_init (int argc, char **argv, GOptionEntry *entries)
 	}
 	g_option_context_free (opts);
 
+	if (debug_level > 0 || http_debug_level > 0)
+		parallelize = !parallelize;
+
 	/* Exit cleanly on ^C in case we're valgrinding. */
 	signal (SIGINT, quit);
 
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 5c244f3..2783aab 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -17,6 +17,7 @@ void test_init    (int argc, char **argv, GOptionEntry *entries);
 void test_cleanup (void);
 
 extern int debug_level, errors;
+extern gboolean parallelize;
 extern gboolean expect_warning, tls_available;
 void debug_printf (int level, const char *format, ...) G_GNUC_PRINTF (2, 3);
 



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