[gnome-keyring] Run tests in a second thread, and run mainloop.



commit 21e59b2d1baa4320971cf495393b456c440ec5d9
Author: Stef Walter <stef memberwebs com>
Date:   Tue Jun 8 05:32:12 2010 +0000

    Run tests in a second thread, and run mainloop.
    
    Run mainloop on main thread, and tests in a second thread.

 egg/tests/unit-test-spawn.c         |    6 +-
 gp11/tests/gp11-test.h              |    4 --
 gp11/tests/unit-test-gp11-crypto.c  |   31 ++++++------
 gp11/tests/unit-test-gp11-object.c  |   15 +++---
 gp11/tests/unit-test-gp11-session.c |   21 ++++----
 pkcs11/gkm/tests/unit-test-object.c |    2 +-
 pkcs11/gkm/tests/unit-test-timer.c  |   10 ++--
 tests/gtest-helpers.c               |   87 +++++++++++++++++++++++------------
 tests/gtest-helpers.h               |    5 +-
 9 files changed, 103 insertions(+), 78 deletions(-)
---
diff --git a/egg/tests/unit-test-spawn.c b/egg/tests/unit-test-spawn.c
index f61798b..7ed7cfd 100644
--- a/egg/tests/unit-test-spawn.c
+++ b/egg/tests/unit-test-spawn.c
@@ -113,7 +113,7 @@ completed_func (gpointer user_data)
 	g_assert (!data->completed);
 	data->completed = TRUE;
 	if (data->is_async)
-		test_mainloop_quit ();
+		test_wait_stop ();
 }
 
 static void
@@ -224,7 +224,7 @@ DEFINE_TEST(test_spawn_async)
 	g_assert (!data.output);
 	g_assert (!data.completed);
 
-	test_mainloop_run (2000);
+	test_wait_until (2000);
 
 	g_assert (data.finalized);
 	g_assert (data.completed);
@@ -252,7 +252,7 @@ DEFINE_TEST(test_spawn_async_none)
 	g_assert (!data.completed);
 	g_assert (!data.output);
 
-	test_mainloop_run (2000);
+	test_wait_until (2000);
 
 	g_assert (data.finalized);
 	g_assert (data.completed);
diff --git a/gp11/tests/gp11-test.h b/gp11/tests/gp11-test.h
index 013ef0d..3968928 100644
--- a/gp11/tests/gp11-test.h
+++ b/gp11/tests/gp11-test.h
@@ -15,10 +15,6 @@
 	g_clear_error (&err); \
 	} while(0)
 
-
-#define WAIT_UNTIL(cond) \
-	while(!cond) g_main_context_iteration (NULL, TRUE);
-
 /* 
  * Some dumb crypto mechanisms for simple testing.
  * 
diff --git a/gp11/tests/unit-test-gp11-crypto.c b/gp11/tests/unit-test-gp11-crypto.c
index 90a0438..67a8560 100644
--- a/gp11/tests/unit-test-gp11-crypto.c
+++ b/gp11/tests/unit-test-gp11-crypto.c
@@ -44,6 +44,7 @@ fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
 {
 	*((GAsyncResult**)user_data) = result;
 	g_object_ref (result);
+	test_wait_stop ();
 }
 
 static GP11Object*
@@ -158,7 +159,7 @@ DEFINE_TEST(encrypt)
 	/* Asynchronous one */
 	gp11_session_encrypt_async (session, key, mech, (const guchar*)"second chance", 14, NULL, fetch_async_result, &result);
 
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	
 	/* Get the result */
@@ -205,9 +206,9 @@ DEFINE_TEST(decrypt)
 	/* Asynchronous one */
 	gp11_session_decrypt_async (session, key, mech, (const guchar*)"FAT CHANCE", 11, NULL, fetch_async_result, &result);
 
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
-	
+
 	/* Get the result */
 	output = gp11_session_decrypt_finish (session, result, &n_output, &error);
 	SUCCESS_RES (output, error);
@@ -278,9 +279,9 @@ DEFINE_TEST(sign)
 	/* Asynchronous one */
 	gp11_session_sign_async (session, key, mech, (const guchar*)"Conrad", 7, NULL, fetch_async_result, &result);
 
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
-	
+
 	/* Get the result */
 	output = gp11_session_sign_finish (session, result, &n_output, &error);
 	SUCCESS_RES (output, error);
@@ -329,7 +330,7 @@ DEFINE_TEST(verify)
 	/* Asynchronous one */
 	gp11_session_verify_async (session, key, mech, (const guchar*)"Labarbara", 10,
 	                           (const guchar*)"my-prefix:Labarbara", 20, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_verify_finish (session, result, &error);
 	SUCCESS_RES (ret, error);
@@ -339,7 +340,7 @@ DEFINE_TEST(verify)
 	result = NULL;
 	gp11_session_verify_async (session, key, mech, (const guchar*)"Labarbara", 10,
 	                           (const guchar*)"my-prefix:Labarxoro", 20, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_verify_finish (session, result, &error);
 	FAIL_RES (ret, error);
@@ -384,7 +385,7 @@ DEFINE_TEST(generate_key_pair)
 	/* Asynchronous one */
 	mech->type = CKM_GENERATE;
 	gp11_session_generate_key_pair_async (session, mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_generate_key_pair_finish (session, result, &pub_key, &prv_key, &error);
 	SUCCESS_RES (ret, error);
@@ -397,7 +398,7 @@ DEFINE_TEST(generate_key_pair)
 	mech->type = 0;
 	pub_key = prv_key = NULL;
 	gp11_session_generate_key_pair_async (session, mech, pub_attrs, prv_attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_generate_key_pair_finish (session, result, &pub_key, &prv_key, &error);
 	FAIL_RES (ret, error);
@@ -448,7 +449,7 @@ DEFINE_TEST(wrap_key)
 	/* Asynchronous one */
 	mech->type = CKM_WRAP;
 	gp11_session_wrap_key_async (session, wrapper, mech, wrapped, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	output = gp11_session_wrap_key_finish (session, result, &n_output, &error);
 	SUCCESS_RES (output, error);
@@ -462,7 +463,7 @@ DEFINE_TEST(wrap_key)
 	mech->type = 0;
 	n_output = 0;
 	gp11_session_wrap_key_async (session, wrapper, mech, wrapped, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	output = gp11_session_wrap_key_finish (session, result, &n_output, &error);
 	FAIL_RES (output, error);
@@ -509,7 +510,7 @@ DEFINE_TEST(unwrap_key)
 	/* Asynchronous one */
 	mech->type = CKM_WRAP;
 	gp11_session_unwrap_key_async (session, wrapper, mech, "special", 7, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	unwrapped = gp11_session_unwrap_key_finish (session, result, &error);
 	SUCCESS_RES (unwrapped, error);
@@ -522,7 +523,7 @@ DEFINE_TEST(unwrap_key)
 	result = NULL;
 	mech->type = 0;
 	gp11_session_unwrap_key_async (session, wrapper, mech, "special", 6, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	unwrapped = gp11_session_unwrap_key_finish (session, result, &error);
 	FAIL_RES (unwrapped, error);
@@ -569,7 +570,7 @@ g_printerr ("derived is: %lu", gp11_object_get_handle (derived));
 	/* Asynchronous one */
 	mech->type = CKM_DERIVE;
 	gp11_session_derive_key_async (session, wrapper, mech, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	derived = gp11_session_derive_key_finish (session, result, &error);
 	SUCCESS_RES (derived, error);
@@ -582,7 +583,7 @@ g_printerr ("derived is: %lu", gp11_object_get_handle (derived));
 	result = NULL;
 	mech->type = 0;
 	gp11_session_derive_key_async (session, wrapper, mech, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	derived = gp11_session_derive_key_finish (session, result, &error);
 	FAIL_RES (derived, error);
diff --git a/gp11/tests/unit-test-gp11-object.c b/gp11/tests/unit-test-gp11-object.c
index 3c7c96d..8daac3f 100644
--- a/gp11/tests/unit-test-gp11-object.c
+++ b/gp11/tests/unit-test-gp11-object.c
@@ -94,6 +94,7 @@ fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
 {
 	*((GAsyncResult**)user_data) = result;
 	g_object_ref (result);
+	test_wait_stop ();
 }
 
 DEFINE_TEST(create_object)
@@ -138,7 +139,7 @@ DEFINE_TEST(create_object)
 
 	/* Using async */
 	gp11_session_create_object_async (session, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	
 	object = gp11_session_create_object_finish (session, result, &err);
@@ -204,7 +205,7 @@ DEFINE_TEST(destroy_object)
 	
 	/* Using async */
 	gp11_object_destroy_async (object, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	ret = gp11_object_destroy_finish (object, result, &err);
@@ -246,7 +247,7 @@ DEFINE_TEST(get_attributes)
 	/* Async */
 	attrs = gp11_attributes_new_empty (CKA_CLASS, CKA_LABEL, GP11_INVALID);
 	gp11_object_get_async (object, attrs, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	attrs_ret = gp11_object_get_finish (object, result, &err);
@@ -288,7 +289,7 @@ DEFINE_TEST(get_data_attribute)
 
 	/* Async */
 	gp11_object_get_data_async (object, CKA_CLASS, NULL, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	klass = gp11_object_get_data_finish (object, result, &n_data, &err);
@@ -347,7 +348,7 @@ DEFINE_TEST(set_attributes)
 
 	/* Async */
 	gp11_object_set_async (object, templ, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	ret = gp11_object_set_finish (object, result, &err);
@@ -398,7 +399,7 @@ DEFINE_TEST(find_objects)
 	/* Async, None */
 	gp11_attributes_add_string (templ, CKA_LABEL, "blah blah");
 	gp11_session_find_objects_async (session, templ, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	objects = gp11_session_find_objects_finish (session, result, &err);
@@ -435,7 +436,7 @@ DEFINE_TEST(explicit_sessions)
 
 	/* Async */
 	gp11_object_get_data_async (object, CKA_CLASS, NULL, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 
 	klass = gp11_object_get_data_finish (object, result, &n_data, &err);
diff --git a/gp11/tests/unit-test-gp11-session.c b/gp11/tests/unit-test-gp11-session.c
index a909b97..5bc6cc5 100644
--- a/gp11/tests/unit-test-gp11-session.c
+++ b/gp11/tests/unit-test-gp11-session.c
@@ -73,6 +73,7 @@ fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
 {
 	*((GAsyncResult**)user_data) = result;
 	g_object_ref (result);
+	test_wait_stop ();
 }
 
 DEFINE_TEST(open_close_session)
@@ -88,10 +89,10 @@ DEFINE_TEST(open_close_session)
 	
 	/* Test opening async */
 	gp11_slot_open_session_async (slot, 0, NULL, NULL, NULL, fetch_async_result, &result);
-	
-	WAIT_UNTIL (result);
+
+	test_wait_until (500);
 	g_assert (result != NULL);
-	
+
 	/* Get the result */
 	sess = gp11_slot_open_session_finish (slot, result, &err);
 	SUCCESS_RES (sess, err);
@@ -131,7 +132,7 @@ DEFINE_TEST(open_reused)
 	
 	/* Test opening async */
 	gp11_slot_open_session_async (slot, 0, NULL, NULL, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	sess = gp11_slot_open_session_finish (slot, result, &err);
 	SUCCESS_RES (sess, err);
@@ -176,7 +177,7 @@ DEFINE_TEST(init_set_pin)
 
 	/* init pin async */
 	gp11_session_init_pin_async (session, (guchar*)"booo", 4, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_init_pin_finish (session, result, &err);
 	SUCCESS_RES (ret, err);
@@ -185,7 +186,7 @@ DEFINE_TEST(init_set_pin)
 
 	/* set pin async */
 	gp11_session_set_pin_async (session, (guchar*)"booo", 4, (guchar*)"tooo", 4, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	ret = gp11_session_set_pin_finish (session, result, &err);
 	SUCCESS_RES (ret, err);
@@ -216,7 +217,7 @@ DEFINE_TEST(login_logout)
 
 	/* login async */
 	gp11_session_login_async (session, CKU_USER, (guchar*)"booo", 4, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	
 	ret = gp11_session_login_finish (session, result, &err);
@@ -227,7 +228,7 @@ DEFINE_TEST(login_logout)
 	
 	/* logout async */
 	gp11_session_logout_async (session, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	
 	ret = gp11_session_logout_finish (session, result, &err);
@@ -297,7 +298,7 @@ DEFINE_TEST(auto_login)
 	
 	/* Now try the same thing, but asyncronously */
 	gp11_slot_open_session_async (slot, CKF_RW_SESSION, NULL, NULL, NULL, fetch_async_result, &result);
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	new_session = gp11_slot_open_session_finish (slot, result, &err);
 	SUCCESS_RES (new_session, err);
@@ -306,7 +307,7 @@ DEFINE_TEST(auto_login)
 	
 	result = NULL;
 	gp11_session_create_object_async (session, attrs, NULL, fetch_async_result, &result); 
-	WAIT_UNTIL (result);
+	test_wait_until (500);
 	g_assert (result != NULL);
 	object = gp11_session_create_object_finish (session, result, &err);
 	SUCCESS_RES (object, err);
diff --git a/pkcs11/gkm/tests/unit-test-object.c b/pkcs11/gkm/tests/unit-test-object.c
index 80bc1ba..e3cf835 100644
--- a/pkcs11/gkm/tests/unit-test-object.c
+++ b/pkcs11/gkm/tests/unit-test-object.c
@@ -191,7 +191,7 @@ DEFINE_TEST(object_create_auto_destruct)
 	g_assert (memcmp (&lifetime, &check, sizeof (lifetime)) == 0);
 
 	test_module_leave ();
-	test_mainloop_run (2200);
+	test_wait_until (2200);
 	test_module_enter ();
 
 	g_assert (!check_object_exists (handle));
diff --git a/pkcs11/gkm/tests/unit-test-timer.c b/pkcs11/gkm/tests/unit-test-timer.c
index c2a7376..893c081 100644
--- a/pkcs11/gkm/tests/unit-test-timer.c
+++ b/pkcs11/gkm/tests/unit-test-timer.c
@@ -60,7 +60,7 @@ DEFINE_TEST(timer_simple)
 	timer = gkm_timer_start (module, 2, timer_callback, &timer);
 
 	test_module_leave ();
-	test_mainloop_run (2200);
+	test_wait_until (2200);
 	test_module_enter ();
 
 	g_assert (timer == NULL);
@@ -73,13 +73,13 @@ DEFINE_TEST(timer_cancel)
 	timer = gkm_timer_start (module, 2, timer_callback, &timer);
 
 	test_module_leave ();
-	test_mainloop_run (500);
+	test_wait_until (500);
 	test_module_enter ();
 
 	gkm_timer_cancel (timer);
 
 	test_module_leave ();
-	test_mainloop_run (2000);
+	test_wait_until (2000);
 	test_module_enter ();
 
 	/* The callback should not have been called */
@@ -97,7 +97,7 @@ DEFINE_TEST(timer_immediate)
 	g_assert (timer != NULL);
 
 	test_module_leave ();
-	test_mainloop_run (50);
+	test_wait_until (50);
 	test_module_enter ();
 
 	/* Should have been called now */
@@ -129,7 +129,7 @@ DEFINE_TEST(timer_multiple)
 	gkm_timer_start (module, 0, multiple_callback, GINT_TO_POINTER (0));
 
 	test_module_leave ();
-	test_mainloop_run (3500);
+	test_wait_until (3500);
 	test_module_enter ();
 
 	g_assert (timer_check == 4);
diff --git a/tests/gtest-helpers.c b/tests/gtest-helpers.c
index 0fec77f..e470676 100644
--- a/tests/gtest-helpers.c
+++ b/tests/gtest-helpers.c
@@ -51,41 +51,43 @@ static const gchar *test_path = NULL;
 
 EGG_SECURE_GLIB_DEFINITIONS ();
 
-static GMainLoop *mainloop = NULL;
-
-static gboolean
-quit_loop (gpointer unused)
-{
-	g_main_loop_quit (mainloop);
-	return TRUE;	
-}
+static GCond *wait_condition = NULL;
+static GMutex *wait_mutex = NULL;
+static gboolean wait_waiting = FALSE;
 
 void
-test_mainloop_quit (void)
+test_wait_stop (void)
 {
-	g_main_loop_quit (mainloop);
+	g_assert (wait_mutex);
+	g_assert (wait_condition);
+	g_mutex_lock (wait_mutex);
+		g_assert (wait_waiting);
+		g_cond_broadcast (wait_condition);
+	g_mutex_unlock (wait_mutex);
 }
 
-void
-test_mainloop_run (int timeout)
+gboolean
+test_wait_until (int timeout)
 {
-	guint id = 0;
-	
-	if (timeout)
-		id = g_timeout_add (timeout, quit_loop, NULL);
-	g_main_loop_run (mainloop);
-	if (timeout)
-		g_source_remove (id); 
+	GTimeVal tv;
+	gboolean ret;
+
+	g_get_current_time (&tv);
+	g_time_val_add (&tv, timeout * 1000);
+
+	g_assert (wait_mutex);
+	g_assert (wait_condition);
+	g_mutex_lock (wait_mutex);
+		g_assert (!wait_waiting);
+		wait_waiting = TRUE;
+		ret = g_cond_timed_wait (wait_condition, wait_mutex, &tv);
+		g_assert (wait_waiting);
+		wait_waiting = FALSE;
+	g_mutex_unlock (wait_mutex);
+
+	return ret;
 } 
 
-GMainLoop* 
-test_mainloop_get (void)
-{
-	if (!mainloop)
-		mainloop = g_main_loop_new (NULL, FALSE);
-	return mainloop;
-}
-
 const gchar*
 test_scratch_directory (void)
 {
@@ -229,11 +231,24 @@ chdir_base_dir (char* argv0)
 	g_free (dir);
 }
 
+static gpointer
+testing_thread (gpointer loop)
+{
+	/* Must have been defined by the test including this file */
+	gint ret = run();
+	g_main_loop_quit (loop);
+	return GINT_TO_POINTER (ret);
+}
+
 int
 main (int argc, char* argv[])
 {
 	GLogLevelFlags fatal_mask;
+	GThread *thread;
+	GMainLoop *loop;
+	gpointer ret;
 
+	g_type_init ();
 	g_thread_init (NULL);
 
 	test_path = getenv ("GNOME_KEYRING_TEST_PATH");
@@ -246,12 +261,24 @@ main (int argc, char* argv[])
 	chdir_base_dir (argv[0]);
 	g_test_init (&argc, &argv, NULL);
 	gtk_init (&argc, &argv);
-	mainloop = g_main_loop_new (NULL, FALSE);
+
+	loop = g_main_loop_new (NULL, FALSE);
+	wait_condition = g_cond_new ();
+	wait_mutex = g_mutex_new ();
 
 	fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
 	fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
 	g_log_set_always_fatal (fatal_mask);
 
-	/* Must have been defined by the test including this file */
-	return run();
+	thread = g_thread_create (testing_thread, loop, TRUE, NULL);
+	g_assert (thread);
+
+	g_main_loop_run (loop);
+	ret = g_thread_join (thread);
+	g_main_loop_unref (loop);
+
+	g_cond_free (wait_condition);
+	g_mutex_free (wait_mutex);
+
+	return GPOINTER_TO_INT (ret);
 }
diff --git a/tests/gtest-helpers.h b/tests/gtest-helpers.h
index 2716d5b..c2d05a1 100644
--- a/tests/gtest-helpers.h
+++ b/tests/gtest-helpers.h
@@ -35,9 +35,8 @@
 #include <string.h>
 #include <unistd.h>
 
-void test_mainloop_quit (void);
-void test_mainloop_run (int timeout);
-GMainLoop* test_mainloop_get (void);
+gboolean test_wait_until (gint timeout);
+void test_wait_stop (void);
 
 const gchar* test_data_directory (void);
 const gchar* test_scratch_directory (void);



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