LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-threading.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 239 253 94.5 %
Date: 2024-04-23 05:16:05 Functions: 15 16 93.8 %
Branches: 35 48 72.9 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  *
       3                 :            :  * Copyright (C) 2008-2010 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: David Zeuthen <davidz@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <gio/gio.h>
      24                 :            : #include <unistd.h>
      25                 :            : #include <string.h>
      26                 :            : 
      27                 :            : #include "gdbus-tests.h"
      28                 :            : 
      29                 :            : /* all tests rely on a global connection */
      30                 :            : static GDBusConnection *c = NULL;
      31                 :            : 
      32                 :            : typedef struct
      33                 :            : {
      34                 :            :   GMainContext *context;
      35                 :            :   gboolean timed_out;
      36                 :            : } TimeoutData;
      37                 :            : 
      38                 :            : static gboolean
      39                 :          0 : timeout_cb (gpointer user_data)
      40                 :            : {
      41                 :          0 :   TimeoutData *data = user_data;
      42                 :            : 
      43                 :          0 :   data->timed_out = TRUE;
      44                 :          0 :   g_main_context_wakeup (data->context);
      45                 :            : 
      46                 :          0 :   return G_SOURCE_REMOVE;
      47                 :            : }
      48                 :            : 
      49                 :            : static gboolean
      50                 :         16 : wakeup_cb (gpointer user_data)
      51                 :            : {
      52                 :            :   /* nothing to do here */
      53                 :         16 :   return G_SOURCE_CONTINUE;
      54                 :            : }
      55                 :            : 
      56                 :            : /* Check that the given @connection has only one ref, waiting to let any pending
      57                 :            :  * unrefs complete first. This is typically used on the shared connection, to
      58                 :            :  * ensure it’s in a correct state before beginning the next test. */
      59                 :            : static void
      60                 :       1002 : (assert_connection_has_one_ref) (GDBusConnection *connection,
      61                 :            :                                  GMainContext    *context,
      62                 :            :                                  const gchar     *calling_function)
      63                 :            : {
      64                 :       1002 :   GSource *timeout_source = NULL;
      65                 :       1002 :   GSource *wakeup_source = NULL;
      66                 :       1002 :   TimeoutData data = { context, FALSE };
      67                 :            : 
      68         [ +  + ]:       1002 :   if (g_atomic_int_get (&G_OBJECT (connection)->ref_count) == 1)
      69                 :        986 :     return;
      70                 :            : 
      71                 :            :   /* Use two timeout sources: @timeout_source to set a deadline after which the
      72                 :            :    * test will fail if the @connection doesn’t have the right number of refs;
      73                 :            :    * and @wakeup_source to periodically wake the @context up to allow the
      74                 :            :    * termination condition to be checked. This allows the termination condition
      75                 :            :    * to be fulfilled by something which doesn’t wake @context up, such as an
      76                 :            :    * unref happening in the GDBus worker thread. */
      77                 :         16 :   timeout_source = g_timeout_source_new_seconds (3);
      78                 :         16 :   g_source_set_callback (timeout_source, timeout_cb, &data, NULL);
      79                 :         16 :   g_source_attach (timeout_source, context);
      80                 :            : 
      81                 :         16 :   wakeup_source = g_timeout_source_new (50 /* ms */);
      82                 :         16 :   g_source_set_callback (wakeup_source, wakeup_cb, NULL, NULL);
      83                 :         16 :   g_source_attach (wakeup_source, context);
      84                 :            : 
      85   [ +  +  +  - ]:         33 :   while (g_atomic_int_get (&G_OBJECT (connection)->ref_count) != 1 && !data.timed_out)
      86                 :            :     {
      87                 :         17 :       g_debug ("refcount of %p is not right (%u rather than 1) in %s(), sleeping",
      88                 :            :                connection, g_atomic_int_get (&G_OBJECT (connection)->ref_count), calling_function);
      89                 :         17 :       g_main_context_iteration (NULL, TRUE);
      90                 :            :     }
      91                 :            : 
      92                 :         16 :   g_source_destroy (wakeup_source);
      93                 :         16 :   g_source_unref (wakeup_source);
      94                 :            : 
      95                 :         16 :   g_source_destroy (timeout_source);
      96                 :         16 :   g_source_unref (timeout_source);
      97                 :            : 
      98         [ -  + ]:         16 :   if (g_atomic_int_get (&G_OBJECT (connection)->ref_count) != 1)
      99                 :          0 :     g_error ("connection %p had too many refs (%u rather than 1) in %s()",
     100                 :            :              connection, g_atomic_int_get (&G_OBJECT (connection)->ref_count), calling_function);
     101                 :            : }
     102                 :            : 
     103                 :            : /* Macro wrapper to add in the calling function name */
     104                 :            : #define assert_connection_has_one_ref(connection, context) \
     105                 :            :   (assert_connection_has_one_ref) (connection, context, G_STRFUNC)
     106                 :            : 
     107                 :            : /* ---------------------------------------------------------------------------------------------------- */
     108                 :            : /* Ensure that signal and method replies are delivered in the right thread */
     109                 :            : /* ---------------------------------------------------------------------------------------------------- */
     110                 :            : 
     111                 :            : typedef struct {
     112                 :            :   GThread *thread;
     113                 :            :   GMainContext *context;
     114                 :            :   guint signal_count;
     115                 :            :   gboolean unsubscribe_complete;
     116                 :            :   GAsyncResult *async_result;
     117                 :            : } DeliveryData;
     118                 :            : 
     119                 :            : static void
     120                 :          4 : async_result_cb (GDBusConnection *connection,
     121                 :            :                  GAsyncResult    *res,
     122                 :            :                  gpointer         user_data)
     123                 :            : {
     124                 :          4 :   DeliveryData *data = user_data;
     125                 :            : 
     126                 :          4 :   data->async_result = g_object_ref (res);
     127                 :            : 
     128                 :          4 :   g_assert_true (g_thread_self () == data->thread);
     129                 :            : 
     130                 :          4 :   g_main_context_wakeup (data->context);
     131                 :          4 : }
     132                 :            : 
     133                 :            : static void
     134                 :          1 : signal_handler (GDBusConnection *connection,
     135                 :            :                 const gchar      *sender_name,
     136                 :            :                 const gchar      *object_path,
     137                 :            :                 const gchar      *interface_name,
     138                 :            :                 const gchar      *signal_name,
     139                 :            :                 GVariant         *parameters,
     140                 :            :                 gpointer         user_data)
     141                 :            : {
     142                 :          1 :   DeliveryData *data = user_data;
     143                 :            : 
     144                 :          1 :   g_assert_true (g_thread_self () == data->thread);
     145                 :            : 
     146                 :          1 :   data->signal_count++;
     147                 :            : 
     148                 :          1 :   g_main_context_wakeup (data->context);
     149                 :          1 : }
     150                 :            : 
     151                 :            : static void
     152                 :          1 : signal_data_free_cb (gpointer user_data)
     153                 :            : {
     154                 :          1 :   DeliveryData *data = user_data;
     155                 :            : 
     156                 :          1 :   g_assert_true (g_thread_self () == data->thread);
     157                 :            : 
     158                 :          1 :   data->unsubscribe_complete = TRUE;
     159                 :            : 
     160                 :          1 :   g_main_context_wakeup (data->context);
     161                 :          1 : }
     162                 :            : 
     163                 :            : static gpointer
     164                 :          1 : test_delivery_in_thread_func (gpointer _data)
     165                 :            : {
     166                 :            :   GMainContext *thread_context;
     167                 :            :   DeliveryData data;
     168                 :            :   GCancellable *ca;
     169                 :            :   guint subscription_id;
     170                 :          1 :   GError *error = NULL;
     171                 :          1 :   GVariant *result_variant = NULL;
     172                 :            : 
     173                 :          1 :   thread_context = g_main_context_new ();
     174                 :          1 :   g_main_context_push_thread_default (thread_context);
     175                 :            : 
     176                 :          1 :   data.thread = g_thread_self ();
     177                 :          1 :   data.context = thread_context;
     178                 :          1 :   data.signal_count = 0;
     179                 :          1 :   data.unsubscribe_complete = FALSE;
     180                 :          1 :   data.async_result = NULL;
     181                 :            : 
     182                 :            :   /* ---------------------------------------------------------------------------------------------------- */
     183                 :            : 
     184                 :            :   /*
     185                 :            :    * Check that we get a reply to the GetId() method call.
     186                 :            :    */
     187                 :          1 :   g_dbus_connection_call (c,
     188                 :            :                           "org.freedesktop.DBus",  /* bus_name */
     189                 :            :                           "/org/freedesktop/DBus", /* object path */
     190                 :            :                           "org.freedesktop.DBus",  /* interface name */
     191                 :            :                           "GetId",                 /* method name */
     192                 :            :                           NULL, NULL,
     193                 :            :                           G_DBUS_CALL_FLAGS_NONE,
     194                 :            :                           -1,
     195                 :            :                           NULL,
     196                 :            :                           (GAsyncReadyCallback) async_result_cb,
     197                 :            :                           &data);
     198         [ +  + ]:          2 :   while (data.async_result == NULL)
     199                 :          1 :     g_main_context_iteration (thread_context, TRUE);
     200                 :            : 
     201                 :          1 :   result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
     202                 :          1 :   g_assert_no_error (error);
     203                 :          1 :   g_assert_nonnull (result_variant);
     204                 :          1 :   g_clear_pointer (&result_variant, g_variant_unref);
     205                 :          1 :   g_clear_object (&data.async_result);
     206                 :            : 
     207                 :            :   /*
     208                 :            :    * Check that we never actually send a message if the GCancellable
     209                 :            :    * is already cancelled - i.e.  we should get G_IO_ERROR_CANCELLED
     210                 :            :    * when the actual connection is not up.
     211                 :            :    */
     212                 :          1 :   ca = g_cancellable_new ();
     213                 :          1 :   g_cancellable_cancel (ca);
     214                 :          1 :   g_dbus_connection_call (c,
     215                 :            :                           "org.freedesktop.DBus",  /* bus_name */
     216                 :            :                           "/org/freedesktop/DBus", /* object path */
     217                 :            :                           "org.freedesktop.DBus",  /* interface name */
     218                 :            :                           "GetId",                 /* method name */
     219                 :            :                           NULL, NULL,
     220                 :            :                           G_DBUS_CALL_FLAGS_NONE,
     221                 :            :                           -1,
     222                 :            :                           ca,
     223                 :            :                           (GAsyncReadyCallback) async_result_cb,
     224                 :            :                           &data);
     225         [ +  + ]:          3 :   while (data.async_result == NULL)
     226                 :          2 :     g_main_context_iteration (thread_context, TRUE);
     227                 :            : 
     228                 :          1 :   result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
     229                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
     230                 :          1 :   g_assert_false (g_dbus_error_is_remote_error (error));
     231                 :          1 :   g_clear_error (&error);
     232                 :          1 :   g_assert_null (result_variant);
     233                 :          1 :   g_clear_object (&data.async_result);
     234                 :            : 
     235                 :          1 :   g_object_unref (ca);
     236                 :            : 
     237                 :            :   /*
     238                 :            :    * Check that cancellation works when the message is already in flight.
     239                 :            :    */
     240                 :          1 :   ca = g_cancellable_new ();
     241                 :          1 :   g_dbus_connection_call (c,
     242                 :            :                           "org.freedesktop.DBus",  /* bus_name */
     243                 :            :                           "/org/freedesktop/DBus", /* object path */
     244                 :            :                           "org.freedesktop.DBus",  /* interface name */
     245                 :            :                           "GetId",                 /* method name */
     246                 :            :                           NULL, NULL,
     247                 :            :                           G_DBUS_CALL_FLAGS_NONE,
     248                 :            :                           -1,
     249                 :            :                           ca,
     250                 :            :                           (GAsyncReadyCallback) async_result_cb,
     251                 :            :                           &data);
     252                 :          1 :   g_cancellable_cancel (ca);
     253                 :            : 
     254         [ +  + ]:          4 :   while (data.async_result == NULL)
     255                 :          3 :     g_main_context_iteration (thread_context, TRUE);
     256                 :            : 
     257                 :          1 :   result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
     258                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
     259                 :          1 :   g_assert_false (g_dbus_error_is_remote_error (error));
     260                 :          1 :   g_clear_error (&error);
     261                 :          1 :   g_assert_null (result_variant);
     262                 :          1 :   g_clear_object (&data.async_result);
     263                 :            : 
     264                 :          1 :   g_object_unref (ca);
     265                 :            : 
     266                 :            :   /*
     267                 :            :    * Check that signals are delivered to the correct thread.
     268                 :            :    *
     269                 :            :    * First we subscribe to the signal, then we call EmitSignal(). This should
     270                 :            :    * cause a TestSignal emission from the testserver.
     271                 :            :    */
     272                 :          1 :   subscription_id = g_dbus_connection_signal_subscribe (c,
     273                 :            :                                                         "com.example.TestService", /* sender */
     274                 :            :                                                         "com.example.Frob",        /* interface */
     275                 :            :                                                         "TestSignal",              /* member */
     276                 :            :                                                         "/com/example/TestObject", /* path */
     277                 :            :                                                         NULL,
     278                 :            :                                                         G_DBUS_SIGNAL_FLAGS_NONE,
     279                 :            :                                                         signal_handler,
     280                 :            :                                                         &data,
     281                 :            :                                                         signal_data_free_cb);
     282                 :          1 :   g_assert_cmpuint (subscription_id, !=, 0);
     283                 :          1 :   g_assert_cmpuint (data.signal_count, ==, 0);
     284                 :            : 
     285                 :          1 :   g_dbus_connection_call (c,
     286                 :            :                           "com.example.TestService", /* bus_name */
     287                 :            :                           "/com/example/TestObject", /* object path */
     288                 :            :                           "com.example.Frob",        /* interface name */
     289                 :            :                           "EmitSignal",              /* method name */
     290                 :            :                           g_variant_new_parsed ("('hello', @o '/com/example/TestObject')"),
     291                 :            :                           NULL,
     292                 :            :                           G_DBUS_CALL_FLAGS_NONE,
     293                 :            :                           -1,
     294                 :            :                           NULL,
     295                 :            :                           (GAsyncReadyCallback) async_result_cb,
     296                 :            :                           &data);
     297   [ +  +  -  + ]:          3 :   while (data.async_result == NULL || data.signal_count < 1)
     298                 :          2 :     g_main_context_iteration (thread_context, TRUE);
     299                 :            : 
     300                 :          1 :   result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
     301                 :          1 :   g_assert_no_error (error);
     302                 :          1 :   g_assert_nonnull (result_variant);
     303                 :          1 :   g_clear_pointer (&result_variant, g_variant_unref);
     304                 :          1 :   g_clear_object (&data.async_result);
     305                 :            : 
     306                 :          1 :   g_assert_cmpuint (data.signal_count, ==, 1);
     307                 :            : 
     308                 :          1 :   g_dbus_connection_signal_unsubscribe (c, subscription_id);
     309                 :          1 :   subscription_id = 0;
     310                 :            : 
     311         [ +  + ]:          2 :   while (!data.unsubscribe_complete)
     312                 :          1 :     g_main_context_iteration (thread_context, TRUE);
     313                 :          1 :   g_assert_true (data.unsubscribe_complete);
     314                 :            : 
     315                 :            :   /* ---------------------------------------------------------------------------------------------------- */
     316                 :            : 
     317                 :          1 :   g_main_context_pop_thread_default (thread_context);
     318                 :          1 :   g_main_context_unref (thread_context);
     319                 :            : 
     320                 :          1 :   return NULL;
     321                 :            : }
     322                 :            : 
     323                 :            : static void
     324                 :          1 : test_delivery_in_thread (void)
     325                 :            : {
     326                 :            :   GThread *thread;
     327                 :            : 
     328                 :          1 :   thread = g_thread_new ("deliver",
     329                 :            :                          test_delivery_in_thread_func,
     330                 :            :                          NULL);
     331                 :            : 
     332                 :          1 :   g_thread_join (thread);
     333                 :            : 
     334                 :          1 :   assert_connection_has_one_ref (c, NULL);
     335                 :          1 : }
     336                 :            : 
     337                 :            : /* ---------------------------------------------------------------------------------------------------- */
     338                 :            : 
     339                 :            : typedef struct {
     340                 :            :   GDBusProxy *proxy;
     341                 :            :   gint msec;
     342                 :            :   guint num;
     343                 :            :   gboolean async;
     344                 :            : 
     345                 :            :   GMainLoop *thread_loop;
     346                 :            :   GThread *thread;
     347                 :            : } SyncThreadData;
     348                 :            : 
     349                 :            : static void
     350                 :         34 : sleep_cb (GDBusProxy   *proxy,
     351                 :            :           GAsyncResult *res,
     352                 :            :           gpointer      user_data)
     353                 :            : {
     354                 :         34 :   SyncThreadData *data = user_data;
     355                 :            :   GError *error;
     356                 :            :   GVariant *result;
     357                 :            : 
     358                 :         34 :   error = NULL;
     359                 :         34 :   result = g_dbus_proxy_call_finish (proxy,
     360                 :            :                                      res,
     361                 :            :                                      &error);
     362                 :         34 :   g_assert_no_error (error);
     363                 :         34 :   g_assert_nonnull (result);
     364                 :         34 :   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     365                 :         34 :   g_variant_unref (result);
     366                 :            : 
     367                 :         34 :   g_assert_true (data->thread == g_thread_self ());
     368                 :            : 
     369                 :         34 :   g_main_loop_quit (data->thread_loop);
     370                 :            : 
     371                 :            :   //g_debug ("async cb (%p)", g_thread_self ());
     372                 :         34 : }
     373                 :            : 
     374                 :            : static gpointer
     375                 :          6 : test_sleep_in_thread_func (gpointer _data)
     376                 :            : {
     377                 :          6 :   SyncThreadData *data = _data;
     378                 :            :   GMainContext *thread_context;
     379                 :            :   guint n;
     380                 :            : 
     381                 :          6 :   thread_context = g_main_context_new ();
     382                 :          6 :   data->thread_loop = g_main_loop_new (thread_context, FALSE);
     383                 :          6 :   g_main_context_push_thread_default (thread_context);
     384                 :            : 
     385                 :          6 :   data->thread = g_thread_self ();
     386                 :            : 
     387         [ +  + ]:         74 :   for (n = 0; n < data->num; n++)
     388                 :            :     {
     389         [ +  + ]:         68 :       if (data->async)
     390                 :            :         {
     391                 :            :           //g_debug ("invoking async (%p)", g_thread_self ());
     392                 :         34 :           g_dbus_proxy_call (data->proxy,
     393                 :            :                              "Sleep",
     394                 :            :                              g_variant_new ("(i)", data->msec),
     395                 :            :                              G_DBUS_CALL_FLAGS_NONE,
     396                 :            :                              -1,
     397                 :            :                              NULL,
     398                 :            :                              (GAsyncReadyCallback) sleep_cb,
     399                 :            :                              data);
     400                 :         34 :           g_main_loop_run (data->thread_loop);
     401         [ -  + ]:         34 :           if (g_test_verbose ())
     402                 :          0 :             g_printerr ("A");
     403                 :            :           //g_debug ("done invoking async (%p)", g_thread_self ());
     404                 :            :         }
     405                 :            :       else
     406                 :            :         {
     407                 :            :           GError *error;
     408                 :            :           GVariant *result;
     409                 :            : 
     410                 :         34 :           error = NULL;
     411                 :            :           //g_debug ("invoking sync (%p)", g_thread_self ());
     412                 :         34 :           result = g_dbus_proxy_call_sync (data->proxy,
     413                 :            :                                            "Sleep",
     414                 :            :                                            g_variant_new ("(i)", data->msec),
     415                 :            :                                            G_DBUS_CALL_FLAGS_NONE,
     416                 :            :                                            -1,
     417                 :            :                                            NULL,
     418                 :            :                                            &error);
     419         [ -  + ]:         34 :           if (g_test_verbose ())
     420                 :          0 :             g_printerr ("S");
     421                 :            :           //g_debug ("done invoking sync (%p)", g_thread_self ());
     422                 :         34 :           g_assert_no_error (error);
     423                 :         34 :           g_assert_nonnull (result);
     424                 :         34 :           g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
     425                 :         34 :           g_variant_unref (result);
     426                 :            :         }
     427                 :            :     }
     428                 :            : 
     429                 :          6 :   g_main_context_pop_thread_default (thread_context);
     430                 :          6 :   g_main_loop_unref (data->thread_loop);
     431                 :          6 :   g_main_context_unref (thread_context);
     432                 :            : 
     433                 :          6 :   return NULL;
     434                 :            : }
     435                 :            : 
     436                 :            : static void
     437                 :          1 : test_method_calls_on_proxy (GDBusProxy *proxy)
     438                 :            : {
     439                 :            :   guint n, divisor;
     440                 :            : 
     441                 :            :   /*
     442                 :            :    * Check that multiple threads can do calls without interfering with
     443                 :            :    * each other. We do this by creating three threads that call the
     444                 :            :    * Sleep() method on the server (which handles it asynchronously, e.g.
     445                 :            :    * it won't block other requests) with different sleep durations and
     446                 :            :    * a number of times. We do this so each set of calls add up to 4000
     447                 :            :    * milliseconds.
     448                 :            :    *
     449                 :            :    * The dbus test server that this code calls into uses glib timeouts
     450                 :            :    * to do the sleeping which have only a granularity of 1ms.  It is
     451                 :            :    * therefore possible to lose as much as 40ms; the test could finish
     452                 :            :    * in slightly less than 4 seconds.
     453                 :            :    *
     454                 :            :    * We run this test twice - first with async calls in each thread, then
     455                 :            :    * again with sync calls
     456                 :            :    */
     457                 :            : 
     458         [ -  + ]:          1 :   if (g_test_thorough ())
     459                 :          0 :     divisor = 1;
     460                 :            :   else
     461                 :          1 :     divisor = 10;
     462                 :            : 
     463         [ +  + ]:          3 :   for (n = 0; n < 2; n++)
     464                 :            :     {
     465                 :            :       gboolean do_async;
     466                 :            :       GThread *thread1;
     467                 :            :       GThread *thread2;
     468                 :            :       GThread *thread3;
     469                 :            :       SyncThreadData data1;
     470                 :            :       SyncThreadData data2;
     471                 :            :       SyncThreadData data3;
     472                 :            :       gint64 start_time, end_time;
     473                 :            :       guint elapsed_msec;
     474                 :            : 
     475                 :          2 :       do_async = (n == 0);
     476                 :            : 
     477                 :          2 :       start_time = g_get_real_time ();
     478                 :            : 
     479                 :          2 :       data1.proxy = proxy;
     480                 :          2 :       data1.msec = 40;
     481                 :          2 :       data1.num = 100 / divisor;
     482                 :          2 :       data1.async = do_async;
     483                 :          2 :       thread1 = g_thread_new ("sleep",
     484                 :            :                               test_sleep_in_thread_func,
     485                 :            :                               &data1);
     486                 :            : 
     487                 :          2 :       data2.proxy = proxy;
     488                 :          2 :       data2.msec = 20;
     489                 :          2 :       data2.num = 200 / divisor;
     490                 :          2 :       data2.async = do_async;
     491                 :          2 :       thread2 = g_thread_new ("sleep2",
     492                 :            :                               test_sleep_in_thread_func,
     493                 :            :                               &data2);
     494                 :            : 
     495                 :          2 :       data3.proxy = proxy;
     496                 :          2 :       data3.msec = 100;
     497                 :          2 :       data3.num = 40 / divisor;
     498                 :          2 :       data3.async = do_async;
     499                 :          2 :       thread3 = g_thread_new ("sleep3",
     500                 :            :                               test_sleep_in_thread_func,
     501                 :            :                               &data3);
     502                 :            : 
     503                 :          2 :       g_thread_join (thread1);
     504                 :          2 :       g_thread_join (thread2);
     505                 :          2 :       g_thread_join (thread3);
     506                 :            : 
     507                 :          2 :       end_time = g_get_real_time ();
     508                 :            : 
     509                 :          2 :       elapsed_msec = (end_time - start_time) / 1000;
     510                 :            : 
     511                 :            :       //g_debug ("Elapsed time for %s = %d msec", n == 0 ? "async" : "sync", elapsed_msec);
     512                 :            : 
     513                 :            :       /* elapsed_msec should be 4000 msec +/- change for overhead/inaccuracy */
     514                 :          2 :       g_assert_cmpint (elapsed_msec, >=, 3950 / divisor);
     515                 :          2 :       g_assert_cmpint (elapsed_msec,  <, 30000 / divisor);
     516                 :            : 
     517         [ -  + ]:          2 :       if (g_test_verbose ())
     518                 :          0 :         g_printerr (" ");
     519                 :            :     }
     520                 :          1 : }
     521                 :            : 
     522                 :            : static void
     523                 :          1 : test_method_calls_in_thread (void)
     524                 :            : {
     525                 :            :   GDBusProxy *proxy;
     526                 :            :   GDBusConnection *connection;
     527                 :            :   GError *error;
     528                 :            : 
     529                 :          1 :   error = NULL;
     530                 :          1 :   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
     531                 :            :                                NULL,
     532                 :            :                                &error);
     533                 :          1 :   g_assert_no_error (error);
     534                 :          1 :   error = NULL;
     535                 :          1 :   proxy = g_dbus_proxy_new_sync (connection,
     536                 :            :                                  G_DBUS_PROXY_FLAGS_NONE,
     537                 :            :                                  NULL,                      /* GDBusInterfaceInfo */
     538                 :            :                                  "com.example.TestService", /* name */
     539                 :            :                                  "/com/example/TestObject", /* object path */
     540                 :            :                                  "com.example.Frob",        /* interface */
     541                 :            :                                  NULL, /* GCancellable */
     542                 :            :                                  &error);
     543                 :          1 :   g_assert_no_error (error);
     544                 :            : 
     545                 :          1 :   test_method_calls_on_proxy (proxy);
     546                 :            : 
     547                 :          1 :   g_object_unref (proxy);
     548                 :          1 :   g_object_unref (connection);
     549                 :            : 
     550         [ -  + ]:          1 :   if (g_test_verbose ())
     551                 :          0 :     g_printerr ("\n");
     552                 :            : 
     553                 :          1 :   assert_connection_has_one_ref (c, NULL);
     554                 :          1 : }
     555                 :            : 
     556                 :            : #define SLEEP_MIN_USEC 1
     557                 :            : #define SLEEP_MAX_USEC 10
     558                 :            : 
     559                 :            : /* Can run in any thread */
     560                 :            : static void
     561                 :       2000 : ensure_connection_works (GDBusConnection *conn)
     562                 :            : {
     563                 :            :   GVariant *v;
     564                 :       2000 :   GError *error = NULL;
     565                 :            : 
     566                 :       2000 :   v = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
     567                 :            :       "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetId", NULL, NULL, 0, -1,
     568                 :            :       NULL, &error);
     569                 :       2000 :   g_assert_no_error (error);
     570                 :       2000 :   g_assert_nonnull (v);
     571                 :       2000 :   g_assert_true (g_variant_is_of_type (v, G_VARIANT_TYPE ("(s)")));
     572                 :       2000 :   g_variant_unref (v);
     573                 :       2000 : }
     574                 :            : 
     575                 :            : /**
     576                 :            :  * get_sync_in_thread:
     577                 :            :  * @data: (type guint): delay in microseconds
     578                 :            :  *
     579                 :            :  * Sleep for a short time, then get a session bus connection and call
     580                 :            :  * a method on it.
     581                 :            :  *
     582                 :            :  * Runs in a non-main thread.
     583                 :            :  *
     584                 :            :  * Returns: (transfer full): the connection
     585                 :            :  */
     586                 :            : static gpointer
     587                 :       1000 : get_sync_in_thread (gpointer data)
     588                 :            : {
     589                 :       1000 :   guint delay = GPOINTER_TO_UINT (data);
     590                 :       1000 :   GError *error = NULL;
     591                 :            :   GDBusConnection *conn;
     592                 :            : 
     593                 :       1000 :   g_usleep (delay);
     594                 :            : 
     595                 :       1000 :   conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
     596                 :       1000 :   g_assert_no_error (error);
     597                 :            : 
     598                 :       1000 :   ensure_connection_works (conn);
     599                 :            : 
     600                 :       1000 :   return conn;
     601                 :            : }
     602                 :            : 
     603                 :            : static void
     604                 :          1 : test_threaded_singleton (void)
     605                 :            : {
     606                 :            :   guint i, n;
     607                 :          1 :   guint unref_wins = 0;
     608                 :          1 :   guint get_wins = 0;
     609                 :            : 
     610         [ -  + ]:          1 :   if (g_test_thorough ())
     611                 :          0 :     n = 100000;
     612                 :            :   else
     613                 :          1 :     n = 1000;
     614                 :            : 
     615         [ +  + ]:       1001 :   for (i = 0; i < n; i++)
     616                 :            :     {
     617                 :            :       GThread *thread;
     618                 :            :       guint unref_delay, get_delay;
     619                 :            :       GDBusConnection *new_conn;
     620                 :            : 
     621                 :            :       /* We want to be the last ref, so let it finish setting up */
     622                 :       1000 :       assert_connection_has_one_ref (c, NULL);
     623                 :            : 
     624   [ -  +  -  - ]:       1000 :       if (g_test_verbose () && (i % (n/50)) == 0)
     625                 :          0 :         g_printerr ("%u%%\n", ((i * 100) / n));
     626                 :            : 
     627                 :            :       /* Delay for a random time on each side of the race, to perturb the
     628                 :            :        * timing. Ideally, we want each side to win half the races; these
     629                 :            :        * timings are about right on smcv's laptop.
     630                 :            :        */
     631                 :       1000 :       unref_delay = g_random_int_range (SLEEP_MIN_USEC, SLEEP_MAX_USEC);
     632                 :       1000 :       get_delay = g_random_int_range (SLEEP_MIN_USEC / 2, SLEEP_MAX_USEC / 2);
     633                 :            : 
     634                 :            :       /* One half of the race is to call g_bus_get_sync... */
     635                 :       1000 :       thread = g_thread_new ("get_sync_in_thread", get_sync_in_thread,
     636                 :       1000 :           GUINT_TO_POINTER (get_delay));
     637                 :            : 
     638                 :            :       /* ... and the other half is to unref the shared connection, which must
     639                 :            :        * have exactly one ref at this point
     640                 :            :        */
     641                 :       1000 :       g_usleep (unref_delay);
     642                 :       1000 :       g_object_unref (c);
     643                 :            : 
     644                 :            :       /* Wait for the thread to run; see what it got */
     645                 :       1000 :       new_conn = g_thread_join (thread);
     646                 :            : 
     647                 :            :       /* If the thread won the race, it will have kept the same connection,
     648                 :            :        * and it'll have one ref
     649                 :            :        */
     650         [ +  + ]:       1000 :       if (new_conn == c)
     651                 :            :         {
     652                 :        586 :           get_wins++;
     653                 :            :         }
     654                 :            :       else
     655                 :            :         {
     656                 :        414 :           unref_wins++;
     657                 :            :           /* c is invalid now, but new_conn is suitable for the
     658                 :            :            * next round
     659                 :            :            */
     660                 :        414 :           c = new_conn;
     661                 :            :         }
     662                 :            : 
     663                 :       1000 :       ensure_connection_works (c);
     664                 :            :     }
     665                 :            : 
     666         [ -  + ]:          1 :   if (g_test_verbose ())
     667                 :          0 :     g_printerr ("Unref won %u races; Get won %u races\n", unref_wins, get_wins);
     668                 :          1 : }
     669                 :            : 
     670                 :            : /* ---------------------------------------------------------------------------------------------------- */
     671                 :            : 
     672                 :            : int
     673                 :          1 : main (int   argc,
     674                 :            :       char *argv[])
     675                 :            : {
     676                 :            :   GError *error;
     677                 :            :   gint ret;
     678                 :            :   gchar *path;
     679                 :            : 
     680                 :          1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     681                 :            : 
     682                 :          1 :   session_bus_up ();
     683                 :            : 
     684                 :            :   /* this is safe; testserver will exit once the bus goes away */
     685                 :          1 :   path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
     686                 :          1 :   g_assert_true (g_spawn_command_line_async (path, NULL));
     687                 :          1 :   g_free (path);
     688                 :            : 
     689                 :            :   /* Create the connection in the main thread */
     690                 :          1 :   error = NULL;
     691                 :          1 :   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
     692                 :          1 :   g_assert_no_error (error);
     693                 :          1 :   g_assert_nonnull (c);
     694                 :            : 
     695                 :          1 :   ensure_gdbus_testserver_up (c, NULL);
     696                 :            : 
     697                 :          1 :   g_test_add_func ("/gdbus/delivery-in-thread", test_delivery_in_thread);
     698                 :          1 :   g_test_add_func ("/gdbus/method-calls-in-thread", test_method_calls_in_thread);
     699                 :          1 :   g_test_add_func ("/gdbus/threaded-singleton", test_threaded_singleton);
     700                 :            : 
     701                 :          1 :   ret = g_test_run();
     702                 :            : 
     703                 :          1 :   g_object_unref (c);
     704                 :            : 
     705                 :            :   /* tear down bus */
     706                 :          1 :   session_bus_down ();
     707                 :            : 
     708                 :          1 :   return ret;
     709                 :            : }

Generated by: LCOV version 1.14