From 2a96a353bc1c6f4455ba80708c726442599f9b8e Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 24 Jul 2012 15:22:44 -0400 Subject: SoupSessionAsync: fix handling of timed-out SoupRequests When using the SoupRequest API, a message that hit SOUP_SESSION_TIMEOUT would error out, but would not emit "finished" or get fully removed from the queue. Fix that and add SoupRequest tests to timeout-test (along with tests that "finished" gets emitted). --- tests/timeout-test.c | 207 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 186 insertions(+), 21 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 2bb91d8a..aad2c3d4 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -2,14 +2,35 @@ #include "test-utils.h" +static void +message_finished (SoupMessage *msg, gpointer user_data) +{ + gboolean *finished = user_data; + + *finished = TRUE; +} + +static void +request_started_cb (SoupSession *session, SoupMessage *msg, + SoupSocket *socket, gpointer user_data) +{ + SoupSocket **ret = user_data; + + *ret = socket; +} + static void do_message_to_session (SoupSession *session, const char *uri, const char *comment, guint expected_status) { SoupMessage *msg; + gboolean finished = FALSE; - debug_printf (1, " %s\n", comment); + debug_printf (1, " msg %s\n", comment); msg = soup_message_new ("GET", uri); + + g_signal_connect (msg, "finished", + G_CALLBACK (message_finished), &finished); soup_session_send_message (session, msg); if (msg->status_code != expected_status) { @@ -22,27 +43,26 @@ do_message_to_session (SoupSession *session, const char *uri, if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) && !soup_message_is_keepalive (msg)) { - debug_printf (1, " ERROR: message is not keepalive!"); + debug_printf (1, " ERROR: message is not keepalive!\n"); errors++; } - g_object_unref (msg); -} - -static void -request_started_cb (SoupSession *session, SoupMessage *msg, - SoupSocket *socket, gpointer user_data) -{ - SoupSocket **ret = user_data; + if (!finished) { + debug_printf (1, " ERROR: 'finished' was not emitted\n"); + errors++; + } - *ret = socket; + g_signal_handlers_disconnect_by_func (msg, + G_CALLBACK (message_finished), + &finished); + g_object_unref (msg); } static void -do_tests_for_session (SoupSession *timeout_session, - SoupSession *idle_session, - SoupSession *plain_session, - char *fast_uri, char *slow_uri) +do_msg_tests_for_session (SoupSession *timeout_session, + SoupSession *idle_session, + SoupSession *plain_session, + char *fast_uri, char *slow_uri) { SoupSocket *ret, *idle_first, *idle_second; SoupSocket *plain_first, *plain_second; @@ -91,6 +111,144 @@ do_tests_for_session (SoupSession *timeout_session, } } +static void +do_request_to_session (SoupRequester *requester, const char *uri, + const char *comment, gboolean expect_timeout) +{ + SoupRequest *req; + SoupMessage *msg; + GInputStream *stream; + GError *error = NULL; + gboolean finished = FALSE; + + debug_printf (1, " req %s\n", comment); + req = soup_requester_request (requester, uri, NULL); + msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (req)); + + g_signal_connect (msg, "finished", + G_CALLBACK (message_finished), &finished); + if (SOUP_IS_SESSION_SYNC (soup_request_get_session (req))) + stream = soup_request_send (req, NULL, &error); + else + stream = soup_test_request_send_async_as_sync (req, NULL, &error); + + if (expect_timeout && !error) { + debug_printf (1, " FAILED: request did not time out\n"); + errors++; + } else if (expect_timeout && !g_error_matches (error, G_IO_ERROR, + G_IO_ERROR_TIMED_OUT)) { + debug_printf (1, " FAILED: wrong error: %s\n", + error->message); + errors++; + } else if (!expect_timeout && error) { + debug_printf (1, " FAILED: expected success but got error: %s\n", + error->message); + errors++; + } + g_clear_error (&error); + + if (stream) { + if (SOUP_IS_SESSION_SYNC (soup_request_get_session (req))) + g_input_stream_close (stream, NULL, &error); + else + soup_test_stream_close_async_as_sync (stream, NULL, &error); + + if (error) { + debug_printf (1, " ERROR closing string: %s", + error->message); + errors++; + } + } + + if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) && + !soup_message_is_keepalive (msg)) { + debug_printf (1, " ERROR: message is not keepalive!\n"); + errors++; + } + + if (!finished) { + debug_printf (1, " ERROR: 'finished' was not emitted\n"); + errors++; + } + + g_signal_handlers_disconnect_by_func (msg, + G_CALLBACK (message_finished), + &finished); + g_object_unref (msg); + g_object_unref (req); +} + +static void +do_req_tests_for_session (SoupSession *timeout_session, + SoupSession *idle_session, + SoupSession *plain_session, + char *fast_uri, char *slow_uri) +{ + SoupRequester *timeout_requester, *idle_requester, *plain_requester; + SoupSocket *ret, *idle_first, *idle_second; + SoupSocket *plain_first, *plain_second; + + debug_printf (1, "\n"); + + if (idle_session) { + idle_requester = soup_requester_new (); + soup_session_add_feature (idle_session, + SOUP_SESSION_FEATURE (idle_requester)); + g_object_unref (idle_requester); + + g_signal_connect (idle_session, "request-started", + G_CALLBACK (request_started_cb), &ret); + do_request_to_session (idle_requester, fast_uri, "fast to idle", FALSE); + idle_first = ret; + } + + if (plain_session) { + plain_requester = soup_requester_new (); + soup_session_add_feature (plain_session, + SOUP_SESSION_FEATURE (plain_requester)); + g_object_unref (plain_requester); + + g_signal_connect (plain_session, "request-started", + G_CALLBACK (request_started_cb), &ret); + do_request_to_session (plain_requester, fast_uri, "fast to plain", FALSE); + plain_first = ret; + } + + timeout_requester = soup_requester_new (); + soup_session_add_feature (timeout_session, + SOUP_SESSION_FEATURE (timeout_requester)); + g_object_unref (timeout_requester); + + do_request_to_session (timeout_requester, fast_uri, "fast to timeout", FALSE); + do_request_to_session (timeout_requester, slow_uri, "slow to timeout", TRUE); + + if (idle_session) { + do_request_to_session (idle_requester, fast_uri, "fast to idle", FALSE); + idle_second = ret; + g_signal_handlers_disconnect_by_func (idle_session, + (gpointer)request_started_cb, + &ret); + + if (idle_first == idle_second) { + debug_printf (1, " ERROR: idle_session did not close first connection\n"); + errors++; + } + } + + if (plain_session) { + do_request_to_session (plain_requester, fast_uri, "fast to plain", FALSE); + plain_second = ret; + g_signal_handlers_disconnect_by_func (plain_session, + (gpointer)request_started_cb, + &ret); + + if (plain_first != plain_second) { + debug_printf (1, " ERROR: plain_session closed connection\n"); + errors++; + } + } +} + static void do_timeout_tests (char *fast_uri, char *slow_uri) { @@ -98,10 +256,12 @@ do_timeout_tests (char *fast_uri, char *slow_uri) debug_printf (1, " async\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, - SOUP_SESSION_TIMEOUT, 1, - NULL); + SOUP_SESSION_TIMEOUT, 1, + SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, + NULL); idle_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_IDLE_TIMEOUT, 1, + SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); /* The "plain" session also has an idle timeout, but it's longer * than the test takes, so for our purposes it should behave like @@ -109,21 +269,26 @@ do_timeout_tests (char *fast_uri, char *slow_uri) */ plain_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_IDLE_TIMEOUT, 2, + SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); - do_tests_for_session (timeout_session, idle_session, plain_session, - fast_uri, slow_uri); + + do_msg_tests_for_session (timeout_session, idle_session, plain_session, + fast_uri, slow_uri); + do_req_tests_for_session (timeout_session, idle_session, plain_session, + fast_uri, slow_uri); soup_test_session_abort_unref (timeout_session); soup_test_session_abort_unref (idle_session); soup_test_session_abort_unref (plain_session); - debug_printf (1, " sync\n"); + debug_printf (1, "\n sync\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, SOUP_SESSION_TIMEOUT, 1, NULL); /* SOUP_SESSION_TIMEOUT doesn't work with sync sessions */ plain_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL); - do_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri); + do_msg_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri); + do_req_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri); soup_test_session_abort_unref (timeout_session); soup_test_session_abort_unref (plain_session); } -- cgit v1.2.1 From 6cddb7cdd31fbee94ec424993e99e0b7f7bdb90b Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 25 Jul 2012 08:54:11 -0400 Subject: test-utils: simplify the SoupRequest test utils a bit --- tests/timeout-test.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index aad2c3d4..5c9d6a81 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -127,10 +127,7 @@ do_request_to_session (SoupRequester *requester, const char *uri, g_signal_connect (msg, "finished", G_CALLBACK (message_finished), &finished); - if (SOUP_IS_SESSION_SYNC (soup_request_get_session (req))) - stream = soup_request_send (req, NULL, &error); - else - stream = soup_test_request_send_async_as_sync (req, NULL, &error); + stream = soup_test_request_send (req, NULL, &error); if (expect_timeout && !error) { debug_printf (1, " FAILED: request did not time out\n"); @@ -148,10 +145,7 @@ do_request_to_session (SoupRequester *requester, const char *uri, g_clear_error (&error); if (stream) { - if (SOUP_IS_SESSION_SYNC (soup_request_get_session (req))) - g_input_stream_close (stream, NULL, &error); - else - soup_test_stream_close_async_as_sync (stream, NULL, &error); + soup_test_request_close_stream (req, stream, NULL, &error); if (error) { debug_printf (1, " ERROR closing string: %s", -- cgit v1.2.1 From 61b86e0724150d4b7684acd467ecb333386b8617 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sat, 11 Aug 2012 20:03:44 -0400 Subject: valgrindage --- tests/timeout-test.c | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 5c9d6a81..9d9b2d13 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -152,6 +152,7 @@ do_request_to_session (SoupRequester *requester, const char *uri, error->message); errors++; } + g_object_unref (stream); } if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) && -- cgit v1.2.1 From e00cf242322f82f0800956b17d726f0437db683a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 5 Dec 2011 16:00:40 -0500 Subject: Move SoupRequester API into SoupSession, declare SoupRequest stable Add soup_session_request() and soup_session_request_uri(), implementing basically the same behavior as soup_requester_request() and soup_requester_request_uri() (but without requiring a separate SoupSessionFeature), and remove the unstable-api ifdefs from soup-request*.h. SoupRequester still exists, but it is still guarded by the unstable-api ifdefs, and is just a dummy wrapper around the functionality that is now in SoupSession. --- tests/timeout-test.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 9d9b2d13..e523f2db 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -112,7 +112,7 @@ do_msg_tests_for_session (SoupSession *timeout_session, } static void -do_request_to_session (SoupRequester *requester, const char *uri, +do_request_to_session (SoupSession *session, const char *uri, const char *comment, gboolean expect_timeout) { SoupRequest *req; @@ -122,7 +122,7 @@ do_request_to_session (SoupRequester *requester, const char *uri, gboolean finished = FALSE; debug_printf (1, " req %s\n", comment); - req = soup_requester_request (requester, uri, NULL); + req = soup_session_request (session, uri, NULL); msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (req)); g_signal_connect (msg, "finished", @@ -179,46 +179,30 @@ do_req_tests_for_session (SoupSession *timeout_session, SoupSession *plain_session, char *fast_uri, char *slow_uri) { - SoupRequester *timeout_requester, *idle_requester, *plain_requester; SoupSocket *ret, *idle_first, *idle_second; SoupSocket *plain_first, *plain_second; debug_printf (1, "\n"); if (idle_session) { - idle_requester = soup_requester_new (); - soup_session_add_feature (idle_session, - SOUP_SESSION_FEATURE (idle_requester)); - g_object_unref (idle_requester); - g_signal_connect (idle_session, "request-started", G_CALLBACK (request_started_cb), &ret); - do_request_to_session (idle_requester, fast_uri, "fast to idle", FALSE); + do_request_to_session (idle_session, fast_uri, "fast to idle", FALSE); idle_first = ret; } if (plain_session) { - plain_requester = soup_requester_new (); - soup_session_add_feature (plain_session, - SOUP_SESSION_FEATURE (plain_requester)); - g_object_unref (plain_requester); - g_signal_connect (plain_session, "request-started", G_CALLBACK (request_started_cb), &ret); - do_request_to_session (plain_requester, fast_uri, "fast to plain", FALSE); + do_request_to_session (plain_session, fast_uri, "fast to plain", FALSE); plain_first = ret; } - timeout_requester = soup_requester_new (); - soup_session_add_feature (timeout_session, - SOUP_SESSION_FEATURE (timeout_requester)); - g_object_unref (timeout_requester); - - do_request_to_session (timeout_requester, fast_uri, "fast to timeout", FALSE); - do_request_to_session (timeout_requester, slow_uri, "slow to timeout", TRUE); + do_request_to_session (timeout_session, fast_uri, "fast to timeout", FALSE); + do_request_to_session (timeout_session, slow_uri, "slow to timeout", TRUE); if (idle_session) { - do_request_to_session (idle_requester, fast_uri, "fast to idle", FALSE); + do_request_to_session (idle_session, fast_uri, "fast to idle", FALSE); idle_second = ret; g_signal_handlers_disconnect_by_func (idle_session, (gpointer)request_started_cb, @@ -231,7 +215,7 @@ do_req_tests_for_session (SoupSession *timeout_session, } if (plain_session) { - do_request_to_session (plain_requester, fast_uri, "fast to plain", FALSE); + do_request_to_session (plain_session, fast_uri, "fast to plain", FALSE); plain_second = ret; g_signal_handlers_disconnect_by_func (plain_session, (gpointer)request_started_cb, -- cgit v1.2.1 From b980d54cb2b77b6b9c54168e9c7ff772734caaac Mon Sep 17 00:00:00 2001 From: Sergio Villar Senin Date: Wed, 30 Jan 2013 17:56:38 +0100 Subject: test-utils: add cancellation support to soup_test_request_send The function gets a new parametter used to enable request cancellation for both sync and async sessions. The cancellation could be performed by either using the GCancellable or by directly cancelling the SoupMessage. Also the GMainContext used to simulate sync operations with async ones will now try to execute all its pending events before quiting the main loop. https://bugzilla.gnome.org/show_bug.cgi?id=692310 --- tests/timeout-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index e523f2db..27bcbff1 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -127,7 +127,7 @@ do_request_to_session (SoupSession *session, const char *uri, g_signal_connect (msg, "finished", G_CALLBACK (message_finished), &finished); - stream = soup_test_request_send (req, NULL, &error); + stream = soup_test_request_send (req, NULL, 0, &error); if (expect_timeout && !error) { debug_printf (1, " FAILED: request did not time out\n"); -- cgit v1.2.1 From c3cc4a9d3d033c9a53ca83fa8b903d7be5dddeb5 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 30 Aug 2013 09:12:05 -0400 Subject: timeout-test: fix on very slow machines On slow old machines, the TLS handshake when making an https request can take longer than 1 second, which meant that even the "fast" https requests were hitting the 1-second timeout and failing. Fix this by making the timeouts longer. However, we don't want to do that unless absolutely necessary, since the test has to hit this timeout multiple times, and the test already takes 10 seconds to run with the 1-second timeout. So check how long it takes to make a single https request, and only slow down the rest of the tests if it looks like it will be needed. Also fix an unrelated bug, where the "session did/didn't close connection" check could fail if the same memory location got used for the first and second sockets. https://bugzilla.gnome.org/show_bug.cgi?id=660581 --- tests/timeout-test.c | 53 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 27bcbff1..405ec3c1 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -26,7 +26,8 @@ do_message_to_session (SoupSession *session, const char *uri, SoupMessage *msg; gboolean finished = FALSE; - debug_printf (1, " msg %s\n", comment); + if (comment) + debug_printf (1, " msg %s\n", comment); msg = soup_message_new ("GET", uri); g_signal_connect (msg, "finished", @@ -71,14 +72,14 @@ do_msg_tests_for_session (SoupSession *timeout_session, g_signal_connect (idle_session, "request-started", G_CALLBACK (request_started_cb), &ret); do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK); - idle_first = ret; + idle_first = g_object_ref (ret); } if (plain_session) { g_signal_connect (plain_session, "request-started", G_CALLBACK (request_started_cb), &ret); do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK); - plain_first = ret; + plain_first = g_object_ref (ret); } do_message_to_session (timeout_session, fast_uri, "fast to timeout", SOUP_STATUS_OK); @@ -95,6 +96,7 @@ do_msg_tests_for_session (SoupSession *timeout_session, debug_printf (1, " ERROR: idle_session did not close first connection\n"); errors++; } + g_object_unref (idle_first); } if (plain_session) { @@ -108,6 +110,7 @@ do_msg_tests_for_session (SoupSession *timeout_session, debug_printf (1, " ERROR: plain_session closed connection\n"); errors++; } + g_object_unref (plain_first); } } @@ -148,7 +151,7 @@ do_request_to_session (SoupSession *session, const char *uri, soup_test_request_close_stream (req, stream, NULL, &error); if (error) { - debug_printf (1, " ERROR closing string: %s", + debug_printf (1, " ERROR closing stream: %s\n", error->message); errors++; } @@ -188,14 +191,14 @@ do_req_tests_for_session (SoupSession *timeout_session, g_signal_connect (idle_session, "request-started", G_CALLBACK (request_started_cb), &ret); do_request_to_session (idle_session, fast_uri, "fast to idle", FALSE); - idle_first = ret; + idle_first = g_object_ref (ret); } if (plain_session) { g_signal_connect (plain_session, "request-started", G_CALLBACK (request_started_cb), &ret); do_request_to_session (plain_session, fast_uri, "fast to plain", FALSE); - plain_first = ret; + plain_first = g_object_ref (ret); } do_request_to_session (timeout_session, fast_uri, "fast to timeout", FALSE); @@ -212,6 +215,7 @@ do_req_tests_for_session (SoupSession *timeout_session, debug_printf (1, " ERROR: idle_session did not close first connection\n"); errors++; } + g_object_unref (idle_first); } if (plain_session) { @@ -225,21 +229,22 @@ do_req_tests_for_session (SoupSession *timeout_session, debug_printf (1, " ERROR: plain_session closed connection\n"); errors++; } + g_object_unref (plain_first); } } static void -do_timeout_tests (char *fast_uri, char *slow_uri) +do_timeout_tests (char *fast_uri, char *slow_uri, gboolean extra_slow) { SoupSession *timeout_session, *idle_session, *plain_session; debug_printf (1, " async\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, - SOUP_SESSION_TIMEOUT, 1, + SOUP_SESSION_TIMEOUT, extra_slow ? 3 : 1, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); idle_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, - SOUP_SESSION_IDLE_TIMEOUT, 1, + SOUP_SESSION_IDLE_TIMEOUT, extra_slow ? 2 : 1, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); /* The "plain" session also has an idle timeout, but it's longer @@ -247,7 +252,7 @@ do_timeout_tests (char *fast_uri, char *slow_uri) * it has no timeout. */ plain_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, - SOUP_SESSION_IDLE_TIMEOUT, 2, + SOUP_SESSION_IDLE_TIMEOUT, 20, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, NULL); @@ -261,7 +266,7 @@ do_timeout_tests (char *fast_uri, char *slow_uri) debug_printf (1, "\n sync\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, - SOUP_SESSION_TIMEOUT, 1, + SOUP_SESSION_TIMEOUT, extra_slow ? 3 : 1, NULL); /* SOUP_SESSION_TIMEOUT doesn't work with sync sessions */ plain_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, @@ -298,7 +303,7 @@ server_handler (SoupServer *server, soup_server_pause_message (server, msg); g_object_set_data (G_OBJECT (msg), "server", server); soup_add_timeout (soup_server_get_async_context (server), - 1100, timeout_finish_message, msg); + 4000, timeout_finish_message, msg); } } @@ -317,12 +322,16 @@ main (int argc, char **argv) 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); + do_timeout_tests (fast_uri, slow_uri, FALSE); g_free (fast_uri); g_free (slow_uri); soup_test_server_quit_unref (server); if (tls_available) { + SoupSession *test_session; + gboolean extra_slow; + gint64 start, end; + debug_printf (1, "\nhttps\n"); server = soup_test_server_new_ssl (TRUE); soup_server_add_handler (server, NULL, server_handler, NULL, NULL); @@ -330,7 +339,23 @@ main (int argc, char **argv) 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); + + /* The 1-second timeouts are too fast for some machines... */ + test_session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); + start = g_get_monotonic_time (); + do_message_to_session (test_session, fast_uri, NULL, SOUP_STATUS_OK); + end = g_get_monotonic_time (); + soup_test_session_abort_unref (test_session); + debug_printf (2, " (https request took %0.3fs)\n", (end - start) / 1000000.0); + if (end - start > 750000) { + debug_printf (1, " (using extra-slow mode)\n\n"); + extra_slow = TRUE; + } else { + debug_printf (2, "\n"); + extra_slow = FALSE; + } + + do_timeout_tests (fast_uri, slow_uri, extra_slow); g_free (fast_uri); g_free (slow_uri); soup_test_server_quit_unref (server); -- cgit v1.2.1 From 96da2df64c9dd8cc52e97ce73e54615d6b520664 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sun, 29 Sep 2013 09:06:37 -0400 Subject: Fix soup_client_input_stream_close to not block Closing a SoupClientInputStream for a message that hadn't been completely read was trying to read to the end of the message first. Fix it to just cancel the read instead. Also fix a few tests that were implicitly assuming the old behavior. https://bugzilla.gnome.org/show_bug.cgi?id=695652 --- tests/timeout-test.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 405ec3c1..5903069b 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -147,6 +147,15 @@ do_request_to_session (SoupSession *session, const char *uri, } g_clear_error (&error); + if (stream) { + soup_test_request_read_all (req, stream, NULL, &error); + if (error) { + debug_printf (1, " ERROR reading stream: %s\n", + error->message); + errors++; + } + } + if (stream) { soup_test_request_close_stream (req, stream, NULL, &error); -- cgit v1.2.1 From 64e667bda5009c8f1acd03659c457e26b16457a6 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 10 Dec 2013 15:47:34 +0100 Subject: tests: initial port to the gtestutils framework Some programs need to be split up into more tests, and the debug output is mostly not updated for the new format. --- tests/timeout-test.c | 187 +++++++++++++++++++++++---------------------------- 1 file changed, 86 insertions(+), 101 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 5903069b..fba515db 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -2,6 +2,8 @@ #include "test-utils.h" +static gboolean slow_https; + static void message_finished (SoupMessage *msg, gpointer user_data) { @@ -34,24 +36,10 @@ do_message_to_session (SoupSession *session, const char *uri, G_CALLBACK (message_finished), &finished); soup_session_send_message (session, msg); - if (msg->status_code != expected_status) { - debug_printf (1, " FAILED: %d %s (expected %d %s)\n", - msg->status_code, msg->reason_phrase, - expected_status, - soup_status_get_phrase (expected_status)); - errors++; - } - - if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) && - !soup_message_is_keepalive (msg)) { - debug_printf (1, " ERROR: message is not keepalive!\n"); - errors++; - } - - if (!finished) { - debug_printf (1, " ERROR: 'finished' was not emitted\n"); - errors++; - } + soup_test_assert_message_status (msg, expected_status); + if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) + g_assert_true (soup_message_is_keepalive (msg)); + g_assert_true (finished); g_signal_handlers_disconnect_by_func (msg, G_CALLBACK (message_finished), @@ -63,7 +51,8 @@ static void do_msg_tests_for_session (SoupSession *timeout_session, SoupSession *idle_session, SoupSession *plain_session, - char *fast_uri, char *slow_uri) + const char *fast_uri, + const char *slow_uri) { SoupSocket *ret, *idle_first, *idle_second; SoupSocket *plain_first, *plain_second; @@ -92,10 +81,8 @@ do_msg_tests_for_session (SoupSession *timeout_session, (gpointer)request_started_cb, &ret); - if (idle_first == idle_second) { - debug_printf (1, " ERROR: idle_session did not close first connection\n"); - errors++; - } + soup_test_assert (idle_first != idle_second, + "idle_session did not close first connection"); g_object_unref (idle_first); } @@ -106,10 +93,8 @@ do_msg_tests_for_session (SoupSession *timeout_session, (gpointer)request_started_cb, &ret); - if (plain_first != plain_second) { - debug_printf (1, " ERROR: plain_session closed connection\n"); - errors++; - } + soup_test_assert (plain_first == plain_second, + "plain_session closed connection"); g_object_unref (plain_first); } } @@ -132,51 +117,26 @@ do_request_to_session (SoupSession *session, const char *uri, G_CALLBACK (message_finished), &finished); stream = soup_test_request_send (req, NULL, 0, &error); - if (expect_timeout && !error) { - debug_printf (1, " FAILED: request did not time out\n"); - errors++; - } else if (expect_timeout && !g_error_matches (error, G_IO_ERROR, - G_IO_ERROR_TIMED_OUT)) { - debug_printf (1, " FAILED: wrong error: %s\n", - error->message); - errors++; - } else if (!expect_timeout && error) { - debug_printf (1, " FAILED: expected success but got error: %s\n", - error->message); - errors++; - } + if (expect_timeout) + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT); + else + g_assert_no_error (error); g_clear_error (&error); if (stream) { soup_test_request_read_all (req, stream, NULL, &error); - if (error) { - debug_printf (1, " ERROR reading stream: %s\n", - error->message); - errors++; - } + g_assert_no_error (error); } if (stream) { soup_test_request_close_stream (req, stream, NULL, &error); - - if (error) { - debug_printf (1, " ERROR closing stream: %s\n", - error->message); - errors++; - } + g_assert_no_error (error); g_object_unref (stream); } - if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) && - !soup_message_is_keepalive (msg)) { - debug_printf (1, " ERROR: message is not keepalive!\n"); - errors++; - } - - if (!finished) { - debug_printf (1, " ERROR: 'finished' was not emitted\n"); - errors++; - } + if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) + g_assert_true (soup_message_is_keepalive (msg)); + g_assert_true (finished); g_signal_handlers_disconnect_by_func (msg, G_CALLBACK (message_finished), @@ -189,7 +149,8 @@ static void do_req_tests_for_session (SoupSession *timeout_session, SoupSession *idle_session, SoupSession *plain_session, - char *fast_uri, char *slow_uri) + const char *fast_uri, + const char *slow_uri) { SoupSocket *ret, *idle_first, *idle_second; SoupSocket *plain_first, *plain_second; @@ -220,10 +181,8 @@ do_req_tests_for_session (SoupSession *timeout_session, (gpointer)request_started_cb, &ret); - if (idle_first == idle_second) { - debug_printf (1, " ERROR: idle_session did not close first connection\n"); - errors++; - } + soup_test_assert (idle_first != idle_second, + "idle_session did not close first connection"); g_object_unref (idle_first); } @@ -234,20 +193,30 @@ do_req_tests_for_session (SoupSession *timeout_session, (gpointer)request_started_cb, &ret); - if (plain_first != plain_second) { - debug_printf (1, " ERROR: plain_session closed connection\n"); - errors++; - } + soup_test_assert (plain_first == plain_second, + "plain_session closed connection"); g_object_unref (plain_first); } } static void -do_timeout_tests (char *fast_uri, char *slow_uri, gboolean extra_slow) +do_async_timeout_tests (gconstpointer data) { SoupSession *timeout_session, *idle_session, *plain_session; + const char *fast_uri = data; + const char *slow_uri = g_build_path ("/", fast_uri, "slow", NULL); + gboolean extra_slow; + + if (g_str_has_prefix (fast_uri, "https")) { + if (!tls_available) { + g_test_skip ("TLS not available"); + return; + } + + extra_slow = slow_https; + } else + extra_slow = FALSE; - debug_printf (1, " async\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, SOUP_SESSION_TIMEOUT, extra_slow ? 3 : 1, SOUP_SESSION_USE_THREAD_CONTEXT, TRUE, @@ -272,8 +241,26 @@ do_timeout_tests (char *fast_uri, char *slow_uri, gboolean extra_slow) soup_test_session_abort_unref (timeout_session); soup_test_session_abort_unref (idle_session); soup_test_session_abort_unref (plain_session); +} + +static void +do_sync_timeout_tests (gconstpointer data) +{ + SoupSession *timeout_session, *plain_session; + const char *fast_uri = data; + const char *slow_uri = g_build_path ("/", fast_uri, "slow", NULL); + gboolean extra_slow; + + if (g_str_has_prefix (fast_uri, "https")) { + if (!tls_available) { + g_test_skip ("TLS not available"); + return; + } + + extra_slow = slow_https; + } else + extra_slow = FALSE; - debug_printf (1, "\n sync\n"); timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, SOUP_SESSION_TIMEOUT, extra_slow ? 3 : 1, NULL); @@ -319,57 +306,55 @@ server_handler (SoupServer *server, int main (int argc, char **argv) { - SoupServer *server; - char *fast_uri, *slow_uri; + SoupServer *server, *https_server = NULL; + char *uri, *https_uri = NULL; + int ret; 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, FALSE); - g_free (fast_uri); - g_free (slow_uri); - soup_test_server_quit_unref (server); + uri = g_strdup_printf ("http://127.0.0.1:%u/", + soup_server_get_port (server)); if (tls_available) { SoupSession *test_session; - gboolean extra_slow; gint64 start, end; - debug_printf (1, "\nhttps\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)); + https_server = soup_test_server_new_ssl (TRUE); + soup_server_add_handler (https_server, NULL, server_handler, NULL, NULL); + https_uri = g_strdup_printf ("https://127.0.0.1:%u/", + soup_server_get_port (https_server)); /* The 1-second timeouts are too fast for some machines... */ test_session = soup_test_session_new (SOUP_TYPE_SESSION, NULL); start = g_get_monotonic_time (); - do_message_to_session (test_session, fast_uri, NULL, SOUP_STATUS_OK); + do_message_to_session (test_session, uri, NULL, SOUP_STATUS_OK); end = g_get_monotonic_time (); soup_test_session_abort_unref (test_session); debug_printf (2, " (https request took %0.3fs)\n", (end - start) / 1000000.0); if (end - start > 750000) { debug_printf (1, " (using extra-slow mode)\n\n"); - extra_slow = TRUE; + slow_https = TRUE; } else { debug_printf (2, "\n"); - extra_slow = FALSE; + slow_https = FALSE; } - - do_timeout_tests (fast_uri, slow_uri, extra_slow); - g_free (fast_uri); - g_free (slow_uri); - soup_test_server_quit_unref (server); } + g_test_add_data_func ("/timeout/http/async", uri, do_async_timeout_tests); + g_test_add_data_func ("/timeout/http/sync", uri, do_sync_timeout_tests); + g_test_add_data_func ("/timeout/https/async", https_uri, do_async_timeout_tests); + g_test_add_data_func ("/timeout/https/sync", https_uri, do_sync_timeout_tests); + + ret = g_test_run (); + + g_free (uri); + g_free (https_uri); + soup_test_server_quit_unref (server); + if (https_server) + soup_test_server_quit_unref (https_server); + test_cleanup (); - return errors != 0; + return ret; } -- cgit v1.2.1 From 2b0de6b7de7537c7d36ab33a7e5cf0e274ea3e23 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 17 Feb 2014 22:29:44 -0500 Subject: tests: skip individual tests rather than whole test programs Use g_skip_test() to skip individual tests rather than just returning status 77 from the test program as a whole. In several cases, we still end up skipping more than necessary, due to test cases that need to be split up more. Remove the "MISSING_REGRESSION_TESTS_PACKAGES" functionality, since the skipped tests are now pointed out explicitly. --- tests/timeout-test.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index fba515db..48c672bf 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -208,10 +208,7 @@ do_async_timeout_tests (gconstpointer data) gboolean extra_slow; if (g_str_has_prefix (fast_uri, "https")) { - if (!tls_available) { - g_test_skip ("TLS not available"); - return; - } + SOUP_TEST_SKIP_IF_NO_TLS; extra_slow = slow_https; } else @@ -252,10 +249,7 @@ do_sync_timeout_tests (gconstpointer data) gboolean extra_slow; if (g_str_has_prefix (fast_uri, "https")) { - if (!tls_available) { - g_test_skip ("TLS not available"); - return; - } + SOUP_TEST_SKIP_IF_NO_TLS; extra_slow = slow_https; } else @@ -340,7 +334,8 @@ main (int argc, char **argv) debug_printf (2, "\n"); slow_https = FALSE; } - } + } else + https_uri = g_strdup ("https://fail."); g_test_add_data_func ("/timeout/http/async", uri, do_async_timeout_tests); g_test_add_data_func ("/timeout/http/sync", uri, do_sync_timeout_tests); -- cgit v1.2.1 From 8e2402651807403c6d42ecb75563f480405e8660 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sat, 15 Mar 2014 11:57:34 -0400 Subject: tests: remove debug_printf()s that are redundant with test names --- tests/timeout-test.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'tests/timeout-test.c') diff --git a/tests/timeout-test.c b/tests/timeout-test.c index 48c672bf..81fb4331 100644 --- a/tests/timeout-test.c +++ b/tests/timeout-test.c @@ -155,8 +155,6 @@ do_req_tests_for_session (SoupSession *timeout_session, SoupSocket *ret, *idle_first, *idle_second; SoupSocket *plain_first, *plain_second; - debug_printf (1, "\n"); - if (idle_session) { g_signal_connect (idle_session, "request-started", G_CALLBACK (request_started_cb), &ret); -- cgit v1.2.1