[libsoup] Add a regression test for SOUP_SESSION_TIMEOUT



commit 6c6a254585c5463de5666182ae417a11a1fd0975
Author: Dan Winship <danw gnome org>
Date:   Sat Apr 18 08:34:22 2009 -0400

    Add a regression test for SOUP_SESSION_TIMEOUT
---
 .gitignore           |    1 +
 tests/Makefile.am    |    3 +
 tests/test-utils.c   |   29 ++++++++++++-
 tests/test-utils.h   |    3 +-
 tests/timeout-test.c |  115 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 148 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6bb1459..b53e571 100644
--- a/.gitignore
+++ b/.gitignore
@@ -70,6 +70,7 @@ tests/simple-httpd
 tests/simple-proxy
 tests/ssl-test
 tests/streaming-test
+tests/timeout-test
 tests/uri-parsing
 tests/xmlrpc-server-test
 tests/xmlrpc-test
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3104533..0d46df5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,6 +27,7 @@ noinst_PROGRAMS =	\
 	simple-httpd	\
 	simple-proxy	\
 	streaming-test	\
+	timeout-test	\
 	uri-parsing	\
 	$(CURL_TESTS)	\
 	$(APACHE_TESTS)	\
@@ -59,6 +60,7 @@ simple_httpd_SOURCES = simple-httpd.c
 simple_proxy_SOURCES = simple-proxy.c
 ssl_test_SOURCES = ssl-test.c $(TEST_SRCS)
 streaming_test_SOURCES = streaming-test.c $(TEST_SRCS)
+timeout_test_SOURCES = timeout-test.c $(TEST_SRCS)
 uri_parsing_SOURCES = uri-parsing.c $(TEST_SRCS)
 xmlrpc_test_SOURCES = xmlrpc-test.c $(TEST_SRCS)
 xmlrpc_server_test_SOURCES = xmlrpc-server-test.c $(TEST_SRCS)
@@ -86,6 +88,7 @@ TESTS =			\
 	ntlm-test	\
 	redirect-test	\
 	streaming-test	\
+	timeout-test	\
 	uri-parsing	\
 	$(APACHE_TESTS)	\
 	$(CURL_TESTS)	\
diff --git a/tests/test-utils.c b/tests/test-utils.c
index bc48a55..b970ed4 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -253,13 +253,26 @@ soup_test_session_abort_unref (SoupSession *session)
 
 static gpointer run_server_thread (gpointer user_data);
 
-SoupServer *
-soup_test_server_new (gboolean in_own_thread)
+static SoupServer *
+test_server_new (gboolean in_own_thread, gboolean ssl)
 {
 	GMainContext *async_context;
+	const char *ssl_cert_file, *ssl_key_file;
+
+	if (test_server)
+		test_server_shutdown ();
 
 	async_context = in_own_thread ? g_main_context_new () : NULL;
+
+	if (ssl) {
+		ssl_cert_file = SRCDIR "/test-cert.pem";
+		ssl_key_file = SRCDIR "/test-key.pem";
+	} else
+		ssl_cert_file = ssl_key_file = NULL;
+
 	test_server = soup_server_new (SOUP_SERVER_ASYNC_CONTEXT, async_context,
+				       SOUP_SERVER_SSL_CERT_FILE, ssl_cert_file,
+				       SOUP_SERVER_SSL_KEY_FILE, ssl_key_file,
 				       NULL);
 	if (async_context)
 		g_main_context_unref (async_context);
@@ -278,6 +291,18 @@ soup_test_server_new (gboolean in_own_thread)
 	return test_server;
 }
 
+SoupServer *
+soup_test_server_new (gboolean in_own_thread)
+{
+	return test_server_new (in_own_thread, FALSE);
+}
+
+SoupServer *
+soup_test_server_new_ssl (gboolean in_own_thread)
+{
+	return test_server_new (in_own_thread, TRUE);
+}
+
 static gpointer
 run_server_thread (gpointer user_data)
 {
diff --git a/tests/test-utils.h b/tests/test-utils.h
index ff9f472..803c344 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -18,4 +18,5 @@ void apache_cleanup (void);
 SoupSession *soup_test_session_new         (GType type, ...);
 void         soup_test_session_abort_unref (SoupSession *session);
 
-SoupServer  *soup_test_server_new  (gboolean in_own_thread);
+SoupServer  *soup_test_server_new     (gboolean in_own_thread);
+SoupServer  *soup_test_server_new_ssl (gboolean in_own_thread);
diff --git a/tests/timeout-test.c b/tests/timeout-test.c
new file mode 100644
index 0000000..73cad70
--- /dev/null
+++ b/tests/timeout-test.c
@@ -0,0 +1,115 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "libsoup/soup.h"
+
+#include "test-utils.h"
+
+static void
+do_tests_for_session (SoupSession *session,
+		      char *fast_uri, char *slow_uri)
+{
+	SoupMessage *msg;
+
+	debug_printf (1, "    fast\n");
+	msg = soup_message_new ("GET", fast_uri);
+	soup_session_send_message (session, msg);
+	if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
+		debug_printf (1, "      FAILED: %d %s (expected 200 OK)\n",
+			      msg->status_code, msg->reason_phrase);
+		errors++;
+	}
+	g_object_unref (msg);
+
+	debug_printf (1, "    slow\n");
+	msg = soup_message_new ("GET", slow_uri);
+	soup_session_send_message (session, msg);
+	if (msg->status_code != SOUP_STATUS_IO_ERROR) {
+		debug_printf (1, "      FAILED: %d %s (expected %d %s)\n",
+			      msg->status_code, msg->reason_phrase,
+			      SOUP_STATUS_IO_ERROR,
+			      soup_status_get_phrase (SOUP_STATUS_IO_ERROR));
+		errors++;
+	}
+	g_object_unref (msg);
+}
+
+static void
+do_timeout_tests (char *fast_uri, char *slow_uri)
+{
+	SoupSession *session;
+
+	debug_printf (1, "  async\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
+					 SOUP_SESSION_TIMEOUT, 1,
+					 NULL);
+	do_tests_for_session (session, fast_uri, slow_uri);
+	soup_test_session_abort_unref (session);
+
+	debug_printf (1, "  sync\n");
+	session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
+					 SOUP_SESSION_TIMEOUT, 1,
+					 NULL);
+	do_tests_for_session (session, fast_uri, slow_uri);
+	soup_test_session_abort_unref (session);
+}
+
+static void
+server_handler (SoupServer        *server,
+		SoupMessage       *msg, 
+		const char        *path,
+		GHashTable        *query,
+		SoupClientContext *client,
+		gpointer           user_data)
+{
+	if (!strcmp (path, "/slow")) {
+		/* Sleep 1.1 seconds. */
+		g_usleep (1100000);
+	}
+
+	soup_message_set_status (msg, SOUP_STATUS_OK);
+	soup_message_set_response (msg, "text/plain",
+				   SOUP_MEMORY_STATIC,
+				   "ok\r\n", 4);
+}
+
+int
+main (int argc, char **argv)
+{
+	SoupServer *server;
+	char *fast_uri, *slow_uri;
+
+	test_init (argc, argv, NULL);
+
+	debug_printf (1, "http\n");
+	server = soup_test_server_new (TRUE);
+	soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
+	fast_uri = g_strdup_printf ("http://127.0.0.1:%u/";,
+				    soup_server_get_port (server));
+	slow_uri = g_strdup_printf ("http://127.0.0.1:%u/slow";,
+				    soup_server_get_port (server));
+	do_timeout_tests (fast_uri, slow_uri);
+	g_free (fast_uri);
+	g_free (slow_uri);
+
+	debug_printf (1, "https\n");
+	server = soup_test_server_new_ssl (TRUE);
+	soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
+	fast_uri = g_strdup_printf ("https://127.0.0.1:%u/";,
+				    soup_server_get_port (server));
+	slow_uri = g_strdup_printf ("https://127.0.0.1:%u/slow";,
+				    soup_server_get_port (server));
+	do_timeout_tests (fast_uri, slow_uri);
+	g_free (fast_uri);
+	g_free (slow_uri);
+
+	test_cleanup ();
+	return errors != 0;
+}



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