gio/tests: port from GSimpleAsyncResult to GTask

https://bugzilla.gnome.org/show_bug.cgi?id=661767
This commit is contained in:
Dan Winship 2012-08-02 15:45:24 -04:00
parent d21309464c
commit e162fab4c0
4 changed files with 98 additions and 115 deletions

View File

@ -33,31 +33,27 @@ typedef struct
{
guint iterations_requested;
guint iterations_done;
GCancellable *cancellable;
} MockOperationData;
static void
mock_operation_free (gpointer user_data)
{
MockOperationData *data = user_data;
g_object_unref (data->cancellable);
g_free (data);
}
static void
mock_operation_thread (GSimpleAsyncResult *simple,
GObject *object,
GCancellable *cancellable)
mock_operation_thread (GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable)
{
MockOperationData *data;
MockOperationData *data = task_data;
guint i;
data = g_simple_async_result_get_op_res_gpointer (simple);
g_assert (data->cancellable == cancellable);
for (i = 0; i < data->iterations_requested; i++)
{
if (g_cancellable_is_cancelled (data->cancellable))
if (g_cancellable_is_cancelled (cancellable))
break;
if (g_test_verbose ())
g_printerr ("THRD: %u iteration %u\n", data->iterations_requested, i);
@ -67,35 +63,36 @@ mock_operation_thread (GSimpleAsyncResult *simple,
if (g_test_verbose ())
g_printerr ("THRD: %u stopped at %u\n", data->iterations_requested, i);
data->iterations_done = i;
g_task_return_boolean (task, TRUE);
}
static gboolean
mock_operation_timeout (gpointer user_data)
{
GSimpleAsyncResult *simple;
GTask *task;
MockOperationData *data;
GError *error = NULL;
gboolean done = FALSE;
simple = G_SIMPLE_ASYNC_RESULT (user_data);
data = g_simple_async_result_get_op_res_gpointer (simple);
task = G_TASK (user_data);
data = g_task_get_task_data (task);
if (data->iterations_done >= data->iterations_requested)
done = TRUE;
if (g_cancellable_set_error_if_cancelled (data->cancellable, &error)) {
g_simple_async_result_take_error (simple, error);
if (g_cancellable_is_cancelled (g_task_get_cancellable (task)))
done = TRUE;
}
if (done) {
if (done)
{
if (g_test_verbose ())
g_printerr ("LOOP: %u stopped at %u\n", data->iterations_requested,\
data->iterations_done);
g_simple_async_result_complete (simple);
g_task_return_boolean (task, TRUE);
return FALSE; /* don't call timeout again */
} else {
}
else
{
data->iterations_done++;
if (g_test_verbose ())
g_printerr ("LOOP: %u iteration %u\n", data->iterations_requested,
@ -111,29 +108,29 @@ mock_operation_async (guint wait_iterations,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
GTask *task;
MockOperationData *data;
simple = g_simple_async_result_new (NULL, callback, user_data,
mock_operation_async);
task = g_task_new (NULL, cancellable, callback, user_data);
data = g_new0 (MockOperationData, 1);
data->iterations_requested = wait_iterations;
data->cancellable = g_object_ref (cancellable);
g_simple_async_result_set_op_res_gpointer (simple, data, mock_operation_free);
g_task_set_task_data (task, data, mock_operation_free);
if (run_in_thread) {
g_simple_async_result_run_in_thread (simple, mock_operation_thread,
G_PRIORITY_DEFAULT, cancellable);
if (run_in_thread)
{
g_task_run_in_thread (task, mock_operation_thread);
if (g_test_verbose ())
g_printerr ("THRD: %d started\n", wait_iterations);
} else {
}
else
{
g_timeout_add_full (G_PRIORITY_DEFAULT, WAIT_ITERATION, mock_operation_timeout,
g_object_ref (simple), g_object_unref);
g_object_ref (task), g_object_unref);
if (g_test_verbose ())
g_printerr ("LOOP: %d started\n", wait_iterations);
}
}
g_object_unref (simple);
g_object_unref (task);
}
static guint
@ -141,11 +138,17 @@ mock_operation_finish (GAsyncResult *result,
GError **error)
{
MockOperationData *data;
GTask *task;
g_assert (g_simple_async_result_is_valid (result, NULL, mock_operation_async));
g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error);
g_assert (g_task_is_valid (result, NULL));
data = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
/* This test expects the return value to be iterations_done even
* when an error is set.
*/
task = G_TASK (result);
data = g_task_get_task_data (task);
g_task_propagate_boolean (task, error);
return data->iterations_done;
}

View File

@ -86,18 +86,20 @@ g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
}
static void
ask_password_with_getpass (GSimpleAsyncResult *res,
GObject *object,
GCancellable *cancellable)
ask_password_with_getpass (GTask *task,
gpointer object,
gpointer task_data,
GCancellable *cancellable)
{
GTlsPassword *password;
GTlsPassword *password = task_data;
GError *error = NULL;
password = g_simple_async_result_get_op_res_gpointer (res);
g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password,
cancellable, &error);
if (error != NULL)
g_simple_async_result_take_error (res, error);
g_task_return_error (task, error);
else
g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
}
static void
@ -107,14 +109,12 @@ g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
GTask *task;
res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
g_tls_console_interaction_ask_password);
g_simple_async_result_set_op_res_gpointer (res, g_object_ref (password), g_object_unref);
g_simple_async_result_run_in_thread (res, ask_password_with_getpass,
G_PRIORITY_DEFAULT, cancellable);
g_object_unref (res);
task = g_task_new (interaction, cancellable, callback, user_data);
g_task_set_task_data (task, g_object_ref (password), g_object_unref);
g_task_run_in_thread (task, ask_password_with_getpass);
g_object_unref (task);
}
static GTlsInteractionResult
@ -122,13 +122,16 @@ g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
g_tls_console_interaction_ask_password), G_TLS_INTERACTION_FAILED);
GTlsInteractionResult ret;
if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
g_return_val_if_fail (g_task_is_valid (result, interaction),
G_TLS_INTERACTION_FAILED);
ret = g_task_propagate_int (G_TASK (result), error);
if (ret == (GTlsInteractionResult)-1)
return G_TLS_INTERACTION_FAILED;
return G_TLS_INTERACTION_HANDLED;
else
return ret;
}
static void

View File

@ -159,22 +159,18 @@ g_test_proxy_resolver_lookup_async (GProxyResolver *resolver,
gpointer user_data)
{
GError *error = NULL;
GSimpleAsyncResult *simple;
GTask *task;
gchar **proxies;
proxies = g_test_proxy_resolver_lookup (resolver, uri, cancellable, &error);
simple = g_simple_async_result_new (G_OBJECT (resolver),
callback, user_data,
g_test_proxy_resolver_lookup_async);
task = g_task_new (resolver, NULL, callback, user_data);
if (proxies == NULL)
g_simple_async_result_take_error (simple, error);
g_task_return_error (task, error);
else
g_simple_async_result_set_op_res_gpointer (simple, proxies, (GDestroyNotify) g_strfreev);
g_task_return_pointer (task, proxies, (GDestroyNotify) g_strfreev);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
g_object_unref (task);
}
static gchar **
@ -182,19 +178,7 @@ g_test_proxy_resolver_lookup_finish (GProxyResolver *resolver,
GAsyncResult *result,
GError **error)
{
if (G_IS_SIMPLE_ASYNC_RESULT (result))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
gchar **proxies;
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
proxies = g_simple_async_result_get_op_res_gpointer (simple);
return g_strdupv (proxies);
}
return NULL;
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
@ -292,24 +276,18 @@ g_proxy_base_connect_async (GProxy *proxy,
gpointer user_data)
{
GError *error = NULL;
GSimpleAsyncResult *simple;
GTask *task;
GIOStream *proxy_io_stream;
simple = g_simple_async_result_new (G_OBJECT (proxy),
callback, user_data,
g_proxy_base_connect_async);
task = g_task_new (proxy, NULL, callback, user_data);
proxy_io_stream = g_proxy_connect (proxy, io_stream, proxy_address,
cancellable, &error);
if (proxy_io_stream)
{
g_simple_async_result_set_op_res_gpointer (simple, proxy_io_stream,
g_object_unref);
}
g_task_return_pointer (task, proxy_io_stream, g_object_unref);
else
g_simple_async_result_take_error (simple, error);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
g_task_return_error (task, error);
g_object_unref (task);
}
static GIOStream *
@ -317,12 +295,7 @@ g_proxy_base_connect_finish (GProxy *proxy,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
@ -722,11 +695,18 @@ g_fake_resolver_lookup_by_name_async (GResolver *resolver,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_simple_async_report_error_in_idle (G_OBJECT (resolver),
callback, user_data,
G_RESOLVER_ERROR,
G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
g_task_report_new_error (resolver, callback, user_data,
g_fake_resolver_lookup_by_name_async,
G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
"Not found");
}
static GList *
g_fake_resolver_lookup_by_name_finish (GResolver *resolver,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
@ -736,6 +716,7 @@ g_fake_resolver_class_init (GFakeResolverClass *fake_class)
resolver_class->lookup_by_name = g_fake_resolver_lookup_by_name;
resolver_class->lookup_by_name_async = g_fake_resolver_lookup_by_name_async;
resolver_class->lookup_by_name_finish = g_fake_resolver_lookup_by_name_finish;
}

View File

@ -82,7 +82,7 @@ test_interaction_ask_password_async_success (GTlsInteraction *interaction,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
GTask *task;
TestInteraction *self;
g_assert (TEST_IS_INTERACTION (interaction));
@ -93,13 +93,12 @@ test_interaction_ask_password_async_success (GTlsInteraction *interaction,
g_assert (G_IS_TLS_PASSWORD (password));
g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
test_interaction_ask_password_async_success);
task = g_task_new (self, cancellable, callback, user_data);
/* Don't do this in real life. Include a null terminator for testing */
g_tls_password_set_value (password, (const guchar *)"the password", 13);
g_simple_async_result_complete_in_idle (res);
g_object_unref (res);
g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
g_object_unref (task);
}
@ -115,12 +114,11 @@ test_interaction_ask_password_finish_success (GTlsInteraction *interaction,
g_assert (g_thread_self () == self->test->interaction_thread);
g_assert (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
test_interaction_ask_password_async_success));
g_assert (g_task_is_valid (result, interaction));
g_assert (error != NULL);
g_assert (*error == NULL);
return G_TLS_INTERACTION_HANDLED;
return g_task_propagate_int (G_TASK (result), error);
}
static void
@ -130,7 +128,7 @@ test_interaction_ask_password_async_failure (GTlsInteraction *interaction,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
GTask *task;
TestInteraction *self;
g_assert (TEST_IS_INTERACTION (interaction));
@ -141,12 +139,10 @@ test_interaction_ask_password_async_failure (GTlsInteraction *interaction,
g_assert (G_IS_TLS_PASSWORD (password));
g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
test_interaction_ask_password_async_failure);
task = g_task_new (self, cancellable, callback, user_data);
g_simple_async_result_set_error (res, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
g_simple_async_result_complete_in_idle (res);
g_object_unref (res);
g_task_return_new_error (task, G_FILE_ERROR, G_FILE_ERROR_ACCES, "The message");
g_object_unref (task);
}
static GTlsInteractionResult
@ -161,13 +157,13 @@ test_interaction_ask_password_finish_failure (GTlsInteraction *interaction,
g_assert (g_thread_self () == self->test->interaction_thread);
g_assert (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
test_interaction_ask_password_async_failure));
g_assert (g_task_is_valid (result, interaction));
g_assert (error != NULL);
g_assert (*error == NULL);
if (!g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
if (g_task_propagate_int (G_TASK (result), error) != -1)
g_assert_not_reached ();
return G_TLS_INTERACTION_FAILED;
}