[evolution-data-server] Make tests retry when factory doesn't know about added ESource yet



commit aaa174787eea3a10dd26feeba21683963e7bb172
Author: Milan Crha <mcrha redhat com>
Date:   Wed Oct 17 17:19:16 2018 +0200

    Make tests retry when factory doesn't know about added ESource yet
    
    When the test creates a new ESource and saves it, then it waits
    for an "source-added" signal on an ESourceRegistry instance to know
    that it had been "registered" in the registry. It could sometimes
    happen that the client received this signal before the factory,
    which means that trying to open this ESource fails with a NOT_FOUND
    error due to the factory's ESourceRegistry instance wasn't updated yet.
    This change tries again for three times with a one second delay, which
    should be enough to have the factory's list of known sources updated.

 src/libebackend/e-data-factory.c              |  9 ++-
 tests/test-server-utils/e-test-server-utils.c | 90 +++++++++++++++++++++++----
 2 files changed, 86 insertions(+), 13 deletions(-)
---
diff --git a/src/libebackend/e-data-factory.c b/src/libebackend/e-data-factory.c
index 130b2bb62..15a5249d7 100644
--- a/src/libebackend/e-data-factory.c
+++ b/src/libebackend/e-data-factory.c
@@ -1342,6 +1342,13 @@ data_factory_spawn_subprocess_backend (EDataFactory *data_factory,
        priv = data_factory->priv;
 
        source = e_source_registry_ref_source (priv->registry, uid);
+
+       if (!source) {
+               g_set_error (
+                       &error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                       _("No such source for UID “%s”"), uid);
+       }
+
        if (source && e_source_has_extension (source, extension_name)) {
                ESourceBackend *extension;
 
@@ -1487,7 +1494,7 @@ data_factory_spawn_subprocess_backend (EDataFactory *data_factory,
                        NULL);
 
                g_object_unref (subprocess);
-       } else {
+       } else if (!error) {
                error = g_error_new (G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                        _("Backend factory for source “%s” and extension “%s” cannot be found."),
                        uid, extension_name);
diff --git a/tests/test-server-utils/e-test-server-utils.c b/tests/test-server-utils/e-test-server-utils.c
index 8f8bb4159..9276edb11 100644
--- a/tests/test-server-utils/e-test-server-utils.c
+++ b/tests/test-server-utils/e-test-server-utils.c
@@ -199,6 +199,7 @@ assert_object_finalized (ETestServerFixture *fixture,
 typedef struct {
        ETestServerFixture *fixture;
        ETestServerClosure *closure;
+       guint retries;
 } FixturePair;
 
 static gboolean
@@ -333,12 +334,15 @@ e_test_server_utils_bootstrap_timeout (FixturePair *pair)
        return FALSE;
 }
 
+static gboolean e_test_server_utils_retry_open_client_cb (gpointer user_data);
+
 static void
 e_test_server_utils_client_ready (GObject *source_object,
                                   GAsyncResult *res,
                                   gpointer user_data)
 {
        FixturePair *pair = (FixturePair *) user_data;
+       gboolean need_retry = FALSE;
        GError *error = NULL;
 
        switch (pair->closure->type) {
@@ -346,7 +350,10 @@ e_test_server_utils_client_ready (GObject *source_object,
                pair->fixture->service.book_client = (EBookClient *)
                        e_book_client_connect_finish (res, &error);
 
-               if (!pair->fixture->service.book_client)
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                   pair->retries < 3)
+                       need_retry = TRUE;
+               else if (!pair->fixture->service.book_client)
                        g_error ("Unable to create the test book: %s", error->message);
 
                break;
@@ -354,7 +361,10 @@ e_test_server_utils_client_ready (GObject *source_object,
                pair->fixture->service.book_client = (EBookClient *)
                        e_book_client_connect_direct_finish (res, &error);
 
-               if (!pair->fixture->service.book_client)
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                   pair->retries < 3)
+                       need_retry = TRUE;
+               else if (!pair->fixture->service.book_client)
                        g_error ("Unable to create the test book: %s", error->message);
 
                break;
@@ -362,7 +372,10 @@ e_test_server_utils_client_ready (GObject *source_object,
                pair->fixture->service.calendar_client = (ECalClient *)
                        e_cal_client_connect_finish (res, &error);
 
-               if (!pair->fixture->service.calendar_client)
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                   pair->retries < 3)
+                       need_retry = TRUE;
+               else if (!pair->fixture->service.calendar_client)
                        g_error ("Unable to create the test calendar: %s", error->message);
 
                break;
@@ -372,6 +385,14 @@ e_test_server_utils_client_ready (GObject *source_object,
                g_assert_not_reached ();
        }
 
+       g_clear_error (&error);
+
+       if (need_retry) {
+               pair->retries++;
+               g_timeout_add_seconds (1, e_test_server_utils_retry_open_client_cb, pair);
+               return;
+       }
+
        /* Track ref counts now that we have a client */
        add_weak_ref (pair->fixture, pair->closure->type);
 
@@ -383,6 +404,7 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                                   ESource *source,
                                   FixturePair *pair)
 {
+       gboolean need_retry = FALSE;
        GError  *error = NULL;
 
        if (g_strcmp0 (e_source_get_uid (source), pair->fixture->source_name) != 0)
@@ -410,8 +432,13 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                }
 
                if (!pair->closure->use_async_connect &&
-                   !pair->fixture->service.book_client)
-                       g_error ("Unable to create the test book: %s", error ? error->message : "Unknown 
error");
+                   !pair->fixture->service.book_client) {
+                       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                           pair->retries < 3)
+                               need_retry = TRUE;
+                       else
+                               g_error ("Unable to create the test book: %s", error ? error->message : 
"Unknown error");
+               }
 
                break;
 
@@ -422,8 +449,13 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                if (!pair->fixture->service.book)
                        g_error ("Unable to create the test book: %s", error->message);
 
-               if (!e_book_open (pair->fixture->service.book, FALSE, &error))
-                       g_error ("Unable to open book: %s", error->message);
+               if (!e_book_open (pair->fixture->service.book, FALSE, &error)) {
+                       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                           pair->retries < 3)
+                               need_retry = TRUE;
+                       else
+                               g_error ("Unable to open book: %s", error->message);
+               }
 
                break;
 
@@ -440,8 +472,13 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                                e_cal_client_connect_sync (
                                        source,
                                        pair->closure->calendar_source_type, (guint32) -1, NULL, &error);
-                       if (!pair->fixture->service.calendar_client)
-                               g_error ("Unable to create the test calendar: %s", error->message);
+                       if (!pair->fixture->service.calendar_client) {
+                               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                                   pair->retries < 3)
+                                       need_retry = TRUE;
+                               else
+                                       g_error ("Unable to create the test calendar: %s", error->message);
+                       }
                }
 
                break;
@@ -453,8 +490,13 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                if (!pair->fixture->service.calendar)
                        g_error ("Unable to create the test calendar");
 
-               if (!e_cal_open (pair->fixture->service.calendar, FALSE, &error))
-                       g_error ("Unable to open calendar: %s", error->message);
+               if (!e_cal_open (pair->fixture->service.calendar, FALSE, &error)) {
+                       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
+                           pair->retries < 3)
+                               need_retry = TRUE;
+                       else
+                               g_error ("Unable to open calendar: %s", error->message);
+               }
 
                break;
 
@@ -462,6 +504,14 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                return;
        }
 
+       g_clear_error (&error);
+
+       if (need_retry) {
+               pair->retries++;
+               g_timeout_add_seconds (1, e_test_server_utils_retry_open_client_cb, pair);
+               return;
+       }
+
        /* Add the weak ref now if we just created it */
        if (pair->closure->type != E_TEST_SERVER_NONE &&
            pair->closure->use_async_connect == FALSE)
@@ -471,6 +521,22 @@ e_test_server_utils_source_added (ESourceRegistry *registry,
                g_main_loop_quit (pair->fixture->loop);
 }
 
+static gboolean
+e_test_server_utils_retry_open_client_cb (gpointer user_data)
+{
+       FixturePair *pair = user_data;
+       ESource *source;
+
+       source = e_source_registry_ref_source (pair->fixture->registry, pair->fixture->source_name);
+
+       g_assert (E_IS_SOURCE (source));
+
+       e_test_server_utils_source_added (pair->fixture->registry, source, pair);
+       g_object_unref (source);
+
+       return FALSE;
+}
+
 static gboolean
 e_test_server_utils_bootstrap_idle (FixturePair *pair)
 {
@@ -579,7 +645,7 @@ e_test_server_utils_setup (ETestServerFixture *fixture,
                            gconstpointer user_data)
 {
        ETestServerClosure *closure = (ETestServerClosure *) user_data;
-       FixturePair         pair = { fixture, closure };
+       FixturePair         pair = { fixture, closure, 0 };
 
        /* Create work directory */
        if (!test_installed_services ())


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