Blame gio/tests/gdbus-threading.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gdbus-tests.h"
Packit ae235b
Packit ae235b
/* all tests rely on a global connection */
Packit ae235b
static GDBusConnection *c = NULL;
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Ensure that signal and method replies are delivered in the right thread */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GThread *thread;
Packit ae235b
  GMainLoop *thread_loop;
Packit ae235b
  guint signal_count;
Packit ae235b
} DeliveryData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_success (GDBusConnection *connection,
Packit ae235b
                       GAsyncResult    *res,
Packit ae235b
                       gpointer         user_data)
Packit ae235b
{
Packit ae235b
  DeliveryData *data = user_data;
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (result != NULL);
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == data->thread);
Packit ae235b
Packit ae235b
  g_main_loop_quit (data->thread_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
msg_cb_expect_error_cancelled (GDBusConnection *connection,
Packit ae235b
                               GAsyncResult    *res,
Packit ae235b
                               gpointer         user_data)
Packit ae235b
{
Packit ae235b
  DeliveryData *data = user_data;
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_connection_call_finish (connection,
Packit ae235b
                                          res,
Packit ae235b
                                          &error);
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_error_free (error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == data->thread);
Packit ae235b
Packit ae235b
  g_main_loop_quit (data->thread_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
signal_handler (GDBusConnection *connection,
Packit ae235b
                const gchar      *sender_name,
Packit ae235b
                const gchar      *object_path,
Packit ae235b
                const gchar      *interface_name,
Packit ae235b
                const gchar      *signal_name,
Packit ae235b
                GVariant         *parameters,
Packit ae235b
                gpointer         user_data)
Packit ae235b
{
Packit ae235b
  DeliveryData *data = user_data;
Packit ae235b
Packit ae235b
  g_assert (g_thread_self () == data->thread);
Packit ae235b
Packit ae235b
  data->signal_count++;
Packit ae235b
Packit ae235b
  g_main_loop_quit (data->thread_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_delivery_in_thread_func (gpointer _data)
Packit ae235b
{
Packit ae235b
  GMainLoop *thread_loop;
Packit ae235b
  GMainContext *thread_context;
Packit ae235b
  DeliveryData data;
Packit ae235b
  GCancellable *ca;
Packit ae235b
  guint subscription_id;
Packit ae235b
  GDBusConnection *priv_c;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
Packit ae235b
  thread_context = g_main_context_new ();
Packit ae235b
  thread_loop = g_main_loop_new (thread_context, FALSE);
Packit ae235b
  g_main_context_push_thread_default (thread_context);
Packit ae235b
Packit ae235b
  data.thread = g_thread_self ();
Packit ae235b
  data.thread_loop = thread_loop;
Packit ae235b
  data.signal_count = 0;
Packit ae235b
Packit ae235b
  /* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we get a reply to the GetId() method call.
Packit ae235b
   */
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_success,
Packit ae235b
                          &data);
Packit ae235b
  g_main_loop_run (thread_loop);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that we never actually send a message if the GCancellable
Packit ae235b
   * is already cancelled - i.e.  we should get #G_IO_ERROR_CANCELLED
Packit ae235b
   * when the actual connection is not up.
Packit ae235b
   */
Packit ae235b
  ca = g_cancellable_new ();
Packit ae235b
  g_cancellable_cancel (ca);
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          ca,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
Packit ae235b
                          &data);
Packit ae235b
  g_main_loop_run (thread_loop);
Packit ae235b
  g_object_unref (ca);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that cancellation works when the message is already in flight.
Packit ae235b
   */
Packit ae235b
  ca = g_cancellable_new ();
Packit ae235b
  g_dbus_connection_call (c,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus_name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "GetId",                 /* method name */
Packit ae235b
                          NULL, NULL,
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          ca,
Packit ae235b
                          (GAsyncReadyCallback) msg_cb_expect_error_cancelled,
Packit ae235b
                          &data);
Packit ae235b
  g_cancellable_cancel (ca);
Packit ae235b
  g_main_loop_run (thread_loop);
Packit ae235b
  g_object_unref (ca);
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that signals are delivered to the correct thread.
Packit ae235b
   *
Packit ae235b
   * First we subscribe to the signal, then we create a a private
Packit ae235b
   * connection. This should cause a NameOwnerChanged message from
Packit ae235b
   * the message bus.
Packit ae235b
   */
Packit ae235b
  subscription_id = g_dbus_connection_signal_subscribe (c,
Packit ae235b
                                                        "org.freedesktop.DBus",  /* sender */
Packit ae235b
                                                        "org.freedesktop.DBus",  /* interface */
Packit ae235b
                                                        "NameOwnerChanged",      /* member */
Packit ae235b
                                                        "/org/freedesktop/DBus", /* path */
Packit ae235b
                                                        NULL,
Packit ae235b
                                                        G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                        signal_handler,
Packit ae235b
                                                        &data,
Packit ae235b
                                                        NULL);
Packit ae235b
  g_assert (subscription_id != 0);
Packit ae235b
  g_assert (data.signal_count == 0);
Packit ae235b
Packit ae235b
  priv_c = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (priv_c != NULL);
Packit ae235b
Packit ae235b
  g_main_loop_run (thread_loop);
Packit ae235b
  g_assert (data.signal_count == 1);
Packit ae235b
Packit ae235b
  g_object_unref (priv_c);
Packit ae235b
Packit ae235b
  g_dbus_connection_signal_unsubscribe (c, subscription_id);
Packit ae235b
Packit ae235b
  /* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
  g_main_context_pop_thread_default (thread_context);
Packit ae235b
  g_main_loop_unref (thread_loop);
Packit ae235b
  g_main_context_unref (thread_context);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_delivery_in_thread (void)
Packit ae235b
{
Packit ae235b
  GThread *thread;
Packit ae235b
Packit ae235b
  thread = g_thread_new ("deliver",
Packit ae235b
                         test_delivery_in_thread_func,
Packit ae235b
                         NULL);
Packit ae235b
Packit ae235b
  g_thread_join (thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  gint msec;
Packit ae235b
  guint num;
Packit ae235b
  gboolean async;
Packit ae235b
Packit ae235b
  GMainLoop *thread_loop;
Packit ae235b
  GThread *thread;
Packit ae235b
} SyncThreadData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
sleep_cb (GDBusProxy   *proxy,
Packit ae235b
          GAsyncResult *res,
Packit ae235b
          gpointer      user_data)
Packit ae235b
{
Packit ae235b
  SyncThreadData *data = user_data;
Packit ae235b
  GError *error;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  result = g_dbus_proxy_call_finish (proxy,
Packit ae235b
                                     res,
Packit ae235b
                                     &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (result != NULL);
Packit ae235b
  g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
Packit ae235b
  g_variant_unref (result);
Packit ae235b
Packit ae235b
  g_assert (data->thread == g_thread_self ());
Packit ae235b
Packit ae235b
  g_main_loop_quit (data->thread_loop);
Packit ae235b
Packit ae235b
  //g_debug ("async cb (%p)", g_thread_self ());
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_sleep_in_thread_func (gpointer _data)
Packit ae235b
{
Packit ae235b
  SyncThreadData *data = _data;
Packit ae235b
  GMainContext *thread_context;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  thread_context = g_main_context_new ();
Packit ae235b
  data->thread_loop = g_main_loop_new (thread_context, FALSE);
Packit ae235b
  g_main_context_push_thread_default (thread_context);
Packit ae235b
Packit ae235b
  data->thread = g_thread_self ();
Packit ae235b
Packit ae235b
  for (n = 0; n < data->num; n++)
Packit ae235b
    {
Packit ae235b
      if (data->async)
Packit ae235b
        {
Packit ae235b
          //g_debug ("invoking async (%p)", g_thread_self ());
Packit ae235b
          g_dbus_proxy_call (data->proxy,
Packit ae235b
                             "Sleep",
Packit ae235b
                             g_variant_new ("(i)", data->msec),
Packit ae235b
                             G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                             -1,
Packit ae235b
                             NULL,
Packit ae235b
                             (GAsyncReadyCallback) sleep_cb,
Packit ae235b
                             data);
Packit ae235b
          g_main_loop_run (data->thread_loop);
Packit ae235b
          if (g_test_verbose ())
Packit ae235b
            g_printerr ("A");
Packit ae235b
          //g_debug ("done invoking async (%p)", g_thread_self ());
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          GError *error;
Packit ae235b
          GVariant *result;
Packit ae235b
Packit ae235b
          error = NULL;
Packit ae235b
          //g_debug ("invoking sync (%p)", g_thread_self ());
Packit ae235b
          result = g_dbus_proxy_call_sync (data->proxy,
Packit ae235b
                                           "Sleep",
Packit ae235b
                                           g_variant_new ("(i)", data->msec),
Packit ae235b
                                           G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                           -1,
Packit ae235b
                                           NULL,
Packit ae235b
                                           &error);
Packit ae235b
          if (g_test_verbose ())
Packit ae235b
            g_printerr ("S");
Packit ae235b
          //g_debug ("done invoking sync (%p)", g_thread_self ());
Packit ae235b
          g_assert_no_error (error);
Packit ae235b
          g_assert (result != NULL);
Packit ae235b
          g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
Packit ae235b
          g_variant_unref (result);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_context_pop_thread_default (thread_context);
Packit ae235b
  g_main_loop_unref (data->thread_loop);
Packit ae235b
  g_main_context_unref (thread_context);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_method_calls_on_proxy (GDBusProxy *proxy)
Packit ae235b
{
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * Check that multiple threads can do calls without interferring with
Packit ae235b
   * each other. We do this by creating three threads that call the
Packit ae235b
   * Sleep() method on the server (which handles it asynchronously, e.g.
Packit ae235b
   * it won't block other requests) with different sleep durations and
Packit ae235b
   * a number of times. We do this so each set of calls add up to 4000
Packit ae235b
   * milliseconds.
Packit ae235b
   *
Packit ae235b
   * The dbus test server that this code calls into uses glib timeouts
Packit ae235b
   * to do the sleeping which have only a granularity of 1ms.  It is
Packit ae235b
   * therefore possible to lose as much as 40ms; the test could finish
Packit ae235b
   * in slightly less than 4 seconds.
Packit ae235b
   *
Packit ae235b
   * We run this test twice - first with async calls in each thread, then
Packit ae235b
   * again with sync calls
Packit ae235b
   */
Packit ae235b
Packit ae235b
  for (n = 0; n < 2; n++)
Packit ae235b
    {
Packit ae235b
      gboolean do_async;
Packit ae235b
      GThread *thread1;
Packit ae235b
      GThread *thread2;
Packit ae235b
      GThread *thread3;
Packit ae235b
      SyncThreadData data1;
Packit ae235b
      SyncThreadData data2;
Packit ae235b
      SyncThreadData data3;
Packit ae235b
      GTimeVal start_time;
Packit ae235b
      GTimeVal end_time;
Packit ae235b
      guint elapsed_msec;
Packit ae235b
Packit ae235b
      do_async = (n == 0);
Packit ae235b
Packit ae235b
      g_get_current_time (&start_time);
Packit ae235b
Packit ae235b
      data1.proxy = proxy;
Packit ae235b
      data1.msec = 40;
Packit ae235b
      data1.num = 100;
Packit ae235b
      data1.async = do_async;
Packit ae235b
      thread1 = g_thread_new ("sleep",
Packit ae235b
                              test_sleep_in_thread_func,
Packit ae235b
                              &data1);
Packit ae235b
Packit ae235b
      data2.proxy = proxy;
Packit ae235b
      data2.msec = 20;
Packit ae235b
      data2.num = 200;
Packit ae235b
      data2.async = do_async;
Packit ae235b
      thread2 = g_thread_new ("sleep2",
Packit ae235b
                              test_sleep_in_thread_func,
Packit ae235b
                              &data2);
Packit ae235b
Packit ae235b
      data3.proxy = proxy;
Packit ae235b
      data3.msec = 100;
Packit ae235b
      data3.num = 40;
Packit ae235b
      data3.async = do_async;
Packit ae235b
      thread3 = g_thread_new ("sleep3",
Packit ae235b
                              test_sleep_in_thread_func,
Packit ae235b
                              &data3);
Packit ae235b
Packit ae235b
      g_thread_join (thread1);
Packit ae235b
      g_thread_join (thread2);
Packit ae235b
      g_thread_join (thread3);
Packit ae235b
Packit ae235b
      g_get_current_time (&end_time);
Packit ae235b
Packit ae235b
      elapsed_msec = ((end_time.tv_sec * G_USEC_PER_SEC + end_time.tv_usec) -
Packit ae235b
                      (start_time.tv_sec * G_USEC_PER_SEC + start_time.tv_usec)) / 1000;
Packit ae235b
Packit ae235b
      //g_debug ("Elapsed time for %s = %d msec", n == 0 ? "async" : "sync", elapsed_msec);
Packit ae235b
Packit ae235b
      /* elapsed_msec should be 4000 msec +/- change for overhead/inaccuracy */
Packit ae235b
      g_assert_cmpint (elapsed_msec, >=, 3950);
Packit ae235b
      g_assert_cmpint (elapsed_msec,  <, 30000);
Packit ae235b
Packit ae235b
      if (g_test_verbose ())
Packit ae235b
        g_printerr (" ");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_method_calls_in_thread (void)
Packit ae235b
{
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
Packit ae235b
                               NULL,
Packit ae235b
                               &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (connection,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                                 NULL,                      /* GDBusInterfaceInfo */
Packit ae235b
                                 "com.example.TestService", /* name */
Packit ae235b
                                 "/com/example/TestObject", /* object path */
Packit ae235b
                                 "com.example.Frob",        /* interface */
Packit ae235b
                                 NULL, /* GCancellable */
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  test_method_calls_on_proxy (proxy);
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
  g_object_unref (connection);
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("\n");
Packit ae235b
}
Packit ae235b
Packit ae235b
#define SLEEP_MIN_USEC 1
Packit ae235b
#define SLEEP_MAX_USEC 10
Packit ae235b
Packit ae235b
/* Can run in any thread */
Packit ae235b
static void
Packit ae235b
ensure_connection_works (GDBusConnection *conn)
Packit ae235b
{
Packit ae235b
  GVariant *v;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  v = g_dbus_connection_call_sync (conn, "org.freedesktop.DBus",
Packit ae235b
      "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetId", NULL, NULL, 0, -1,
Packit ae235b
      NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (v != NULL);
Packit ae235b
  g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE ("(s)")));
Packit ae235b
  g_variant_unref (v);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * get_sync_in_thread:
Packit ae235b
 * @data: (type guint): delay in microseconds
Packit ae235b
 *
Packit ae235b
 * Sleep for a short time, then get a session bus connection and call
Packit ae235b
 * a method on it.
Packit ae235b
 *
Packit ae235b
 * Runs in a non-main thread.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the connection
Packit ae235b
 */
Packit ae235b
static gpointer
Packit ae235b
get_sync_in_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  guint delay = GPOINTER_TO_UINT (data);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GDBusConnection *conn;
Packit ae235b
Packit ae235b
  g_usleep (delay);
Packit ae235b
Packit ae235b
  conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  ensure_connection_works (conn);
Packit ae235b
Packit ae235b
  return conn;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_threaded_singleton (void)
Packit ae235b
{
Packit ae235b
  guint i, n;
Packit ae235b
  guint unref_wins = 0;
Packit ae235b
  guint get_wins = 0;
Packit ae235b
Packit ae235b
  if (g_test_thorough ())
Packit ae235b
    n = 100000;
Packit ae235b
  else
Packit ae235b
    n = 5000;
Packit ae235b
Packit ae235b
  for (i = 0; i < n; i++)
Packit ae235b
    {
Packit ae235b
      GThread *thread;
Packit ae235b
      guint j;
Packit ae235b
      guint unref_delay, get_delay;
Packit ae235b
      GDBusConnection *new_conn;
Packit ae235b
Packit ae235b
      /* We want to be the last ref, so let it finish setting up */
Packit ae235b
      for (j = 0; j < 100; j++)
Packit ae235b
        {
Packit ae235b
          guint r = g_atomic_int_get (&G_OBJECT (c)->ref_count);
Packit ae235b
Packit ae235b
          if (r == 1)
Packit ae235b
            break;
Packit ae235b
Packit ae235b
          g_debug ("run %u: refcount is %u, sleeping", i, r);
Packit ae235b
          g_usleep (1000);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (j == 100)
Packit ae235b
        g_error ("connection had too many refs");
Packit ae235b
Packit ae235b
      if (g_test_verbose () && (i % (n/50)) == 0)
Packit ae235b
        g_printerr ("%u%%\n", ((i * 100) / n));
Packit ae235b
Packit ae235b
      /* Delay for a random time on each side of the race, to perturb the
Packit ae235b
       * timing. Ideally, we want each side to win half the races; these
Packit ae235b
       * timings are about right on smcv's laptop.
Packit ae235b
       */
Packit ae235b
      unref_delay = g_random_int_range (SLEEP_MIN_USEC, SLEEP_MAX_USEC);
Packit ae235b
      get_delay = g_random_int_range (SLEEP_MIN_USEC / 2, SLEEP_MAX_USEC / 2);
Packit ae235b
Packit ae235b
      /* One half of the race is to call g_bus_get_sync... */
Packit ae235b
      thread = g_thread_new ("get_sync_in_thread", get_sync_in_thread,
Packit ae235b
          GUINT_TO_POINTER (get_delay));
Packit ae235b
Packit ae235b
      /* ... and the other half is to unref the shared connection, which must
Packit ae235b
       * have exactly one ref at this point
Packit ae235b
       */
Packit ae235b
      g_usleep (unref_delay);
Packit ae235b
      g_object_unref (c);
Packit ae235b
Packit ae235b
      /* Wait for the thread to run; see what it got */
Packit ae235b
      new_conn = g_thread_join (thread);
Packit ae235b
Packit ae235b
      /* If the thread won the race, it will have kept the same connection,
Packit ae235b
       * and it'll have one ref
Packit ae235b
       */
Packit ae235b
      if (new_conn == c)
Packit ae235b
        {
Packit ae235b
          get_wins++;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          unref_wins++;
Packit ae235b
          /* c is invalid now, but new_conn is suitable for the
Packit ae235b
           * next round
Packit ae235b
           */
Packit ae235b
          c = new_conn;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      ensure_connection_works (c);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Unref won %u races; Get won %u races\n", unref_wins, get_wins);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  gint ret;
Packit ae235b
  gchar *path;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
Packit ae235b
  /* this is safe; testserver will exit once the bus goes away */
Packit ae235b
  path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
Packit ae235b
  g_assert (g_spawn_command_line_async (path, NULL));
Packit ae235b
  g_free (path);
Packit ae235b
Packit ae235b
  ensure_gdbus_testserver_up ();
Packit ae235b
Packit ae235b
  /* Create the connection in the main thread */
Packit ae235b
  error = NULL;
Packit ae235b
  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c != NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/delivery-in-thread", test_delivery_in_thread);
Packit ae235b
  g_test_add_func ("/gdbus/method-calls-in-thread", test_method_calls_in_thread);
Packit ae235b
  g_test_add_func ("/gdbus/threaded-singleton", test_threaded_singleton);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  /* tear down bus */
Packit ae235b
  session_bus_down ();
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}