From 3034833ea3b458dafbf39c650722a5a638a24e3f Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Mon, 12 Jul 2010 12:00:55 +0100 Subject: WIP --- rest/rest-proxy-call.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++-- rest/rest-proxy-call.h | 5 +++++ tests/proxy.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c index f70b983..044e7ff 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -927,7 +927,6 @@ rest_proxy_call_sync (RestProxyCall *call, { RestProxyCallPrivate *priv; SoupMessage *message; - guint status; gboolean ret; g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE); @@ -938,7 +937,7 @@ rest_proxy_call_sync (RestProxyCall *call, if (!message) return FALSE; - status = _rest_proxy_send_message (priv->proxy, message); + _rest_proxy_send_message (priv->proxy, message); ret = finish_call (call, message, error_out); @@ -1093,6 +1092,55 @@ rest_proxy_call_invoke_finish (RestProxyCall *call, return TRUE; } +static void +on_invoke_cancelled (GCancellable *cancellable, gpointer user_data) +{ + SoupMessage *message = SOUP_MESSAGE (user_data); + RestProxyCall *call; + + call = g_object_get_data (G_OBJECT (message), "rest-proxy-call"); + g_assert (call); + + _rest_proxy_cancel_message (GET_PRIVATE (call)->proxy, message); +} + +gboolean +rest_proxy_call_invoke (RestProxyCall *call, + GCancellable *cancellable, + GError **error) +{ + RestProxyCallPrivate *priv; + SoupMessage *message; + gboolean ret; + gulong cancel_id = 0; + + g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE); + priv = GET_PRIVATE (call); + + if (g_cancellable_set_error_if_cancelled (cancellable, error)) + return FALSE; + + message = prepare_message (call, error); + if (!message) + return FALSE; + + /* Proxy GCancellable to the SoupSession */ + if (cancellable) { + g_object_set_data (G_OBJECT (message), "rest-proxy-call", call); + cancel_id = g_signal_connect (cancellable, "cancelled", G_CALLBACK (on_invoke_cancelled), message); + } + + _rest_proxy_send_message (priv->proxy, message); + + ret = finish_call (call, message, error); + + if (cancel_id) + g_signal_handler_disconnect (cancellable, cancel_id); + + g_object_unref (message); + + return ret; +} /** * rest_proxy_call_lookup_response_header: diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h index 77e8a44..5dde834 100644 --- a/rest/rest-proxy-call.h +++ b/rest/rest-proxy-call.h @@ -162,6 +162,11 @@ gboolean rest_proxy_call_cancel (RestProxyCall *call); gboolean rest_proxy_call_sync (RestProxyCall *call, GError **error_out); +gboolean +rest_proxy_call_invoke (RestProxyCall *call, + GCancellable *cancellable, + GError **error); + void rest_proxy_call_invoke_async (RestProxyCall *call, GCancellable *cancellable, GObject *weak_object, diff --git a/tests/proxy.c b/tests/proxy.c index 54c456e..cae9230 100644 --- a/tests/proxy.c +++ b/tests/proxy.c @@ -36,6 +36,7 @@ #include #include +static const gboolean verbose = TRUE; static int errors = 0; static void @@ -43,9 +44,15 @@ server_callback (SoupServer *server, SoupMessage *msg, const char *path, GHashTable *query, SoupClientContext *client, gpointer user_data) { + if (verbose) g_message (path); + if (g_str_equal (path, "/ping")) { soup_message_set_status (msg, SOUP_STATUS_OK); } + else if (g_str_equal (path, "/slowping")) { + g_usleep (G_USEC_PER_SEC * 2); + soup_message_set_status (msg, SOUP_STATUS_OK); + } else if (g_str_equal (path, "/echo")) { const char *value; @@ -100,10 +107,12 @@ ping_test (RestProxy *proxy) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "ping"); - if (!rest_proxy_call_run (call, NULL, &error)) { + if (!rest_proxy_call_invoke (call, NULL, &error)) { g_printerr ("Call failed: %s\n", error->message); g_error_free (error); errors++; @@ -132,6 +141,8 @@ echo_test (RestProxy *proxy) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "echo"); rest_proxy_call_add_param (call, "value", "echome"); @@ -167,6 +178,8 @@ reverse_test (RestProxy *proxy) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "reverse"); rest_proxy_call_add_param (call, "value", "reverseme"); @@ -202,6 +215,8 @@ status_ok_test (RestProxy *proxy, int status) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "status"); rest_proxy_call_add_param (call, "status", g_strdup_printf ("%d", status)); @@ -229,6 +244,8 @@ status_error_test (RestProxy *proxy, int status) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "status"); rest_proxy_call_add_param (call, "status", g_strdup_printf ("%d", status)); @@ -255,6 +272,8 @@ test_status_ok (RestProxy *proxy, const char *function) RestProxyCall *call; GError *error = NULL; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, function); @@ -302,6 +321,8 @@ test_async_cancelled (RestProxy *proxy) RestProxyCall *call; GCancellable *cancel; + if (verbose) g_message (__FUNCTION__); + cancel = g_cancellable_new (); g_cancellable_cancel (cancel); @@ -313,6 +334,27 @@ test_async_cancelled (RestProxy *proxy) g_object_unref (cancel); } +static void +test_sync_cancelled (RestProxy *proxy) +{ + RestProxyCall *call; + GCancellable *cancel; + + if (verbose) g_message (__FUNCTION__); + + cancel = g_cancellable_new (); + g_cancellable_cancel (cancel); + + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "ping"); + if (rest_proxy_call_invoke (call, cancel, NULL)) { + g_printerr ("Call succeeded incorrectly\n"); + errors++; + } + + g_object_unref (cancel); +} + static void status_ok_cb (GObject *object, GAsyncResult *result, gpointer user_data) { @@ -345,6 +387,8 @@ status_ok_test_async (RestProxy *proxy, int status) { RestProxyCall *call; + if (verbose) g_message (__FUNCTION__); + call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "status"); rest_proxy_call_add_param (call, "status", g_strdup_printf ("%d", status)); @@ -363,11 +407,11 @@ main (int argc, char **argv) g_thread_init (NULL); g_type_init (); - session = soup_session_async_new (); + session = soup_session_sync_new (); server = soup_server_new (NULL); soup_server_add_handler (server, NULL, server_callback, NULL, NULL); - soup_server_run_async (server); + g_thread_create ((GThreadFunc)soup_server_run, server, FALSE, NULL); url = g_strdup_printf ("http://127.0.0.1:%d/", soup_server_get_port (server)); proxy = rest_proxy_new (url, FALSE); @@ -386,6 +430,7 @@ main (int argc, char **argv) status_ok_test_async (proxy, SOUP_STATUS_NO_CONTENT); test_async_cancelled (proxy); + test_sync_cancelled (proxy); test_status_ok (proxy, "useragent/none"); rest_proxy_set_user_agent (proxy, "TestSuite-1.0"); -- cgit v1.2.1