[tracker/wip/rishi/tracker_sparql_connection_update_array-test-zero-length: 3/3] libtracker-sparql, tests: Support empty array updates




commit d11c28c8ab6d82b3bca5fc2632630a5991044e2f
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Apr 2 22:35:31 2021 +0200

    libtracker-sparql, tests: Support empty array updates
    
    This is for the convenience of users of the API where the length of
    the array depends on runtime conditions and it's possible that it will
    sometimes be empty.
    
    The 'bus' and 'direct' implementations of the update_array_async
    virtual method are mainly predicated on the length of the array. So, as
    long as an empty array is accompanied by a zero length, it should work
    as a NOP.
    
    For the 'bus' implementation, another alternative was to handle it in
    the endpoint (or the service-side) which would otherwise receive an
    empty GUnixFDList. However, it's slightly nicer to handle it in the
    client to avoid the IPC.
    
    https://gitlab.gnome.org/GNOME/tracker/-/merge_requests/392

 src/libtracker-sparql/bus/tracker-bus.vala |  3 ++
 src/libtracker-sparql/tracker-connection.c |  2 +-
 tests/libtracker-sparql/tracker-fd-test.c  | 46 ++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)
---
diff --git a/src/libtracker-sparql/bus/tracker-bus.vala b/src/libtracker-sparql/bus/tracker-bus.vala
index 985d3f684..a09ad6b9c 100644
--- a/src/libtracker-sparql/bus/tracker-bus.vala
+++ b/src/libtracker-sparql/bus/tracker-bus.vala
@@ -239,6 +239,9 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
        }
 
        public async override bool update_array_async (string[] sparql, Cancellable? cancellable = null) 
throws Sparql.Error, GLib.Error, GLib.IOError, DBusError {
+               if (sparql.length == 0)
+                       return true;
+
                UnixInputStream input;
                UnixOutputStream output;
                pipe (out input, out output);
diff --git a/src/libtracker-sparql/tracker-connection.c b/src/libtracker-sparql/tracker-connection.c
index 2cacd7735..d21bce3e2 100644
--- a/src/libtracker-sparql/tracker-connection.c
+++ b/src/libtracker-sparql/tracker-connection.c
@@ -429,7 +429,7 @@ tracker_sparql_connection_update_array_async (TrackerSparqlConnection  *connecti
                                               gpointer                  user_data)
 {
        g_return_if_fail (TRACKER_IS_SPARQL_CONNECTION (connection));
-       g_return_if_fail (sparql != NULL);
+       g_return_if_fail (sparql != NULL || sparql_length == 0);
        g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
 
        TRACKER_SPARQL_CONNECTION_GET_CLASS (connection)->update_array_async (connection,
diff --git a/tests/libtracker-sparql/tracker-fd-test.c b/tests/libtracker-sparql/tracker-fd-test.c
index 7b49990ea..2ba589d3a 100644
--- a/tests/libtracker-sparql/tracker-fd-test.c
+++ b/tests/libtracker-sparql/tracker-fd-test.c
@@ -376,6 +376,50 @@ test_tracker_sparql_update_array_async (DataFixture   *fixture,
 
 }
 
+static void
+async_update_array_empty_callback (GObject      *source_object,
+                                   GAsyncResult *result,
+                                   gpointer      user_data)
+{
+       GError *error = NULL;
+       AsyncData *data = user_data;
+
+       tracker_sparql_connection_update_array_finish (connection, result, &error);
+
+       /* main error is only set on fatal (D-Bus) errors that apply to the whole update */
+       g_assert_no_error (error);
+
+       g_main_loop_quit (data->main_loop);
+}
+
+static void
+test_tracker_sparql_update_array_async_empty (DataFixture   *fixture,
+                                              gconstpointer  user_data)
+{
+       const gchar **queries = NULL;
+       GMainLoop *main_loop;
+       AsyncData *data;
+
+       main_loop = g_main_loop_new (NULL, FALSE);
+
+       data = g_slice_new (AsyncData);
+       data->main_loop = main_loop;
+
+       /* Cast here is because vala doesn't make const-char-** possible :( */
+       tracker_sparql_connection_update_array_async (connection,
+                                                     (char**) queries,
+                                                     0,
+                                                     NULL,
+                                                     async_update_array_empty_callback,
+                                                     data);
+
+       g_main_loop_run (main_loop);
+
+       g_slice_free (AsyncData, data);
+       g_main_loop_unref (main_loop);
+
+}
+
 static void
 test_tracker_sparql_update_fast_error (DataFixture  *fixture,
                                        gconstpointer user_data)
@@ -816,6 +860,8 @@ main (gint argc, gchar **argv)
                        test_tracker_sparql_update_blank_async, delete_test_data);
        g_test_add ("/steroids/tracker/tracker_sparql_update_array_async", DataFixture, NULL, 
insert_test_data,
                        test_tracker_sparql_update_array_async, delete_test_data);
+       g_test_add ("/steroids/tracker/tracker_sparql_update_array_async_empty", DataFixture, NULL, 
insert_test_data,
+                       test_tracker_sparql_update_array_async_empty, delete_test_data);
 
        return g_test_run ();
 }


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