[libgda] GdaWorker: more tests
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] GdaWorker: more tests
- Date: Sat, 13 Dec 2014 21:08:58 +0000 (UTC)
commit 55618df8a515ad1e635159a3e72d39df44660dd8
Author: Vivien Malerba <malerba gnome-db org>
Date: Wed Dec 3 21:54:48 2014 +0100
GdaWorker: more tests
libgda/thread-wrapper/Makefile.am | 4 +
libgda/thread-wrapper/test-connect.c | 112 ++++++++++++++++++++++++++++++++++
libgda/thread-wrapper/test-worker.c | 78 +++++++++++++++++++++++-
3 files changed, 193 insertions(+), 1 deletions(-)
---
diff --git a/libgda/thread-wrapper/Makefile.am b/libgda/thread-wrapper/Makefile.am
index 95e3ead..5841449 100644
--- a/libgda/thread-wrapper/Makefile.am
+++ b/libgda/thread-wrapper/Makefile.am
@@ -152,6 +152,10 @@ test_connect_SOURCES = \
itsignaler.c \
gda-connect.h \
gda-connect.c \
+ background.h \
+ background.c \
+ gda-worker.h \
+ gda-worker.c \
test-connect.c
test_connect_LDADD = \
diff --git a/libgda/thread-wrapper/test-connect.c b/libgda/thread-wrapper/test-connect.c
index 09bfcd1..412e079 100644
--- a/libgda/thread-wrapper/test-connect.c
+++ b/libgda/thread-wrapper/test-connect.c
@@ -19,6 +19,7 @@
#include <glib.h>
#include <string.h>
#include "gda-connect.h"
+#include "gda-worker.h"
#include "dummy-object.h"
#include <stdlib.h>
#include <string.h>
@@ -29,6 +30,7 @@ int test1 (void);
int test2 (void);
int test3 (void);
int test4 (void);
+int test5 (void);
int
main (int argc, char** argv)
@@ -48,6 +50,7 @@ main (int argc, char** argv)
nfailed += test2 ();
nfailed += test3 ();
nfailed += test4 ();
+ nfailed += test5 ();
g_main_context_release (context);
g_main_context_unref (context);
@@ -415,3 +418,112 @@ test4 (void)
return retval;
}
+/*
+ * Test 5: gda_worker_do_job() for several jobs, with signal emitting
+ */
+static gpointer
+test5_func (DummyObject *obj, GError **error)
+{
+ static guint i = 1;
+ g_usleep (G_USEC_PER_SEC / 2);
+ i++;
+ g_signal_emit_by_name (obj, "sig2", i * 2, NULL);
+
+ return (gpointer) GUINT_TO_POINTER (i);
+}
+
+typedef struct {
+ GdaWorker *worker;
+ DummyObject *obj;
+} Test5Data;
+gboolean test5_sub_failed = FALSE;
+gboolean
+test5_time_func (Test5Data *data)
+{
+ /* this time we should get 3, because the previous call to test5_func() should have returned 2 */
+ g_print ("Sumbitting another job for test5_func()\n");
+ gpointer result;
+ GError *error = NULL;
+ if (!gda_worker_do_job (data->worker, NULL, 0, &result, NULL,
+ (GdaWorkerFunc) test5_func, data->obj, NULL,
+ NULL, &error)) {
+ g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No
detail");
+ g_clear_error (&error);
+ test5_sub_failed = TRUE;
+ }
+ else if (GPOINTER_TO_UINT (result) != 3) {
+ g_print ("In %s(): expected 3 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result));
+ test5_sub_failed = TRUE;
+ }
+
+ gda_worker_unref (data->worker);
+
+ return G_SOURCE_REMOVE;
+}
+
+gint test5_sig1_int = 0;
+gint test5_sig2_int = 0;
+static void
+test5_sig2_cb (DummyObject *obj, gint i, gchar *str, gpointer data)
+{
+ g_print ("%s(..., %d, ...) in thread %p\n", __FUNCTION__, i, g_thread_self ());
+ if (test5_sig1_int == 0)
+ test5_sig1_int = i;
+ else if (test5_sig2_int == 0)
+ test5_sig2_int = i;
+ else
+ g_assert_not_reached ();
+}
+
+int
+test5 (void)
+{
+ g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ());
+ GdaWorker *worker;
+ gint nfailed = 0;
+
+ DummyObject *obj;
+ obj = dummy_object_new ();
+
+ gulong hid;
+ hid = gda_signal_connect (obj, "sig2",
+ G_CALLBACK (test5_sig2_cb),
+ NULL, NULL, 0, NULL);
+
+
+ worker = gda_worker_new ();
+ GError *error = NULL;
+ Test5Data d5;
+ d5.worker = gda_worker_ref (worker);
+ d5.obj = obj;
+ g_timeout_add (200, (GSourceFunc) test5_time_func, &d5);
+
+ g_print ("Sumbitting job for test5_func()\n");
+ gpointer result;
+ if (!gda_worker_do_job (worker, NULL, 0, &result, NULL,
+ (GdaWorkerFunc) test5_func, obj, NULL,
+ NULL, &error)) {
+ g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No
detail");
+ g_clear_error (&error);
+ nfailed ++;
+ }
+ else if (GPOINTER_TO_UINT (result) != 2) {
+ g_print ("In %s(): expected 2 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result));
+ nfailed ++;
+ }
+ else if (test5_sub_failed)
+ nfailed ++;
+ else if ((test5_sig1_int != 4) || (test5_sig2_int != 6)) {
+ g_print ("Expected test5_sig1_int=4 and test5_sig2_int=6, got test5_sig1_int=%d and
test5_sig2_int=%d\n",
+ test5_sig1_int, test5_sig2_int);
+ nfailed++;
+ }
+
+ gda_signal_handler_disconnect (obj, hid);
+ g_print ("Unref worker...\n");
+
+ gda_worker_unref (worker);
+ g_print ("%s done\n", __FUNCTION__);
+
+ return nfailed;
+}
diff --git a/libgda/thread-wrapper/test-worker.c b/libgda/thread-wrapper/test-worker.c
index 5dabbb9..89f9b6b 100644
--- a/libgda/thread-wrapper/test-worker.c
+++ b/libgda/thread-wrapper/test-worker.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Vivien Malerba <malerba gnome-db org>
+ * Copyright (C) 2013 - 2014 Vivien Malerba <malerba gnome-db org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -32,6 +32,7 @@ int test9 (void);
int test10 (void);
int test11 (void);
int test12 (void);
+int test13 (void);
int
@@ -50,6 +51,7 @@ main (int argc, char** argv)
nfailed += test10 ();
nfailed += test11 ();
nfailed += test12 ();
+ nfailed += test13 ();
g_print ("Test %s\n", nfailed > 0 ? "Failed" : "Ok");
return nfailed > 0 ? 1 : 0;
@@ -810,3 +812,77 @@ test12 (void)
return nfailed;
}
+
+/*
+ * Test 13: gda_worker_do_job() for several jobs
+ */
+static gpointer
+test13_func (gpointer data, GError **error)
+{
+ g_print ("%s (%s) in thread %p\n", __FUNCTION__, (gchar *) data, g_thread_self ());
+ static guint i = 1;
+ g_usleep (G_USEC_PER_SEC / 2); /* wait half a second */
+ i++;
+ return (gpointer) GUINT_TO_POINTER (i);
+}
+
+gboolean test13_sub_failed = FALSE;
+gboolean
+test13_time_func (GdaWorker *worker)
+{
+ /* this time we should get 3, because the previous call to test13_func() should have returned 2 */
+ g_print ("Sumbitting another job for test13_func()\n");
+ gpointer result;
+ GError *error = NULL;
+ if (!gda_worker_do_job (worker, NULL, 0, &result, NULL,
+ (GdaWorkerFunc) test13_func, "2nd", NULL,
+ NULL, &error)) {
+ g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No
detail");
+ g_clear_error (&error);
+ test13_sub_failed = TRUE;
+ }
+ else if (GPOINTER_TO_UINT (result) != 3) {
+ g_print ("In %s(): expected 3 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result));
+ test13_sub_failed = TRUE;
+ }
+
+ gda_worker_unref (worker);
+
+ return G_SOURCE_REMOVE;
+}
+
+int
+test13 (void)
+{
+ g_print ("%s started, main thread is %p\n", __FUNCTION__, g_thread_self ());
+ GdaWorker *worker;
+ gint nfailed = 0;
+
+ worker = gda_worker_new ();
+ GError *error = NULL;
+
+ g_timeout_add (200, (GSourceFunc) test13_time_func, gda_worker_ref (worker));
+
+ g_print ("Sumbitting job for test13_func()\n");
+ gpointer result;
+ if (!gda_worker_do_job (worker, NULL, 0, &result, NULL,
+ (GdaWorkerFunc) test13_func, "1st", NULL,
+ NULL, &error)) {
+ g_print ("gda_worker_do_job() failed: %s\n", error && error->message ? error->message : "No
detail");
+ g_clear_error (&error);
+ nfailed ++;
+ }
+ else if (GPOINTER_TO_UINT (result) != 2) {
+ g_print ("In %s(): expected 2 and got %u\n", __FUNCTION__, GPOINTER_TO_UINT (result));
+ nfailed ++;
+ }
+ else if (test13_sub_failed)
+ nfailed ++;
+
+ g_print ("Unref worker...\n");
+
+ gda_worker_unref (worker);
+ g_print ("%s done\n", __FUNCTION__);
+
+ return nfailed;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]