Blame gio/tests/gdbus-proxy-threads.c

Packit ae235b
/* Test case for GNOME #651133
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2011 Nokia Corporation
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: Simon McVittie <simon.mcvittie@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <config.h>
Packit ae235b
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#include "gdbus-tests.h"
Packit ae235b
Packit ae235b
#ifdef HAVE_DBUS1
Packit ae235b
# include <dbus/dbus-shared.h>
Packit ae235b
#else
Packit ae235b
# define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
Packit ae235b
# define DBUS_PATH_DBUS "/org/freedesktop/DBus"
Packit ae235b
# define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
Packit ae235b
# define DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER 1
Packit ae235b
# define DBUS_RELEASE_NAME_REPLY_RELEASED 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#define MY_NAME "com.example.Test.Myself"
Packit ae235b
/* This many threads create and destroy GDBusProxy instances, in addition
Packit ae235b
 * to the main thread processing their NameOwnerChanged signals.
Packit ae235b
 * N_THREADS_MAX is used with "-m slow", N_THREADS otherwise.
Packit ae235b
 */
Packit ae235b
#define N_THREADS_MAX 10
Packit ae235b
#define N_THREADS 2
Packit ae235b
/* This many GDBusProxy instances are created by each thread. */
Packit ae235b
#define N_REPEATS 100
Packit ae235b
/* The main thread requests/releases a name this many times as rapidly as
Packit ae235b
 * possible, before performing one "slow" cycle that waits for each method
Packit ae235b
 * call result (and therefore, due to D-Bus total ordering, all previous
Packit ae235b
 * method calls) to prevent requests from piling up infinitely. The more calls
Packit ae235b
 * are made rapidly, the better we reproduce bugs.
Packit ae235b
 */
Packit ae235b
#define N_RAPID_CYCLES 50
Packit ae235b
Packit ae235b
static GMainLoop *loop;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
run_proxy_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection = data;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  g_assert (g_main_context_get_thread_default () == NULL);
Packit ae235b
Packit ae235b
  for (i = 0; i < N_REPEATS; i++)
Packit ae235b
    {
Packit ae235b
      GDBusProxy *proxy;
Packit ae235b
      GError *error = NULL;
Packit ae235b
      GVariant *ret;
Packit ae235b
Packit ae235b
      if (g_test_verbose ())
Packit ae235b
        g_printerr (".");
Packit ae235b
Packit ae235b
      proxy = g_dbus_proxy_new_sync (connection,
Packit ae235b
                                     G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
Packit ae235b
                                     G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
Packit ae235b
                                     NULL,
Packit ae235b
                                     MY_NAME,
Packit ae235b
                                     "/com/example/TestObject",
Packit ae235b
                                     "com.example.Frob",
Packit ae235b
                                     NULL,
Packit ae235b
                                     &error);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
      g_assert (proxy != NULL);
Packit ae235b
      g_dbus_proxy_set_default_timeout (proxy, G_MAXINT);
Packit ae235b
Packit ae235b
      ret = g_dbus_proxy_call_sync (proxy, "StupidMethod", NULL,
Packit ae235b
                                    G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
Packit ae235b
                                    NULL, NULL);
Packit ae235b
      /*
Packit ae235b
       * we expect this to fail - if we have the name at the moment, we called
Packit ae235b
       * an unimplemented method, and if not, there was nothing to call
Packit ae235b
       */
Packit ae235b
      g_assert (ret == NULL);
Packit ae235b
Packit ae235b
      /*
Packit ae235b
       * this races with the NameOwnerChanged signal being emitted in an
Packit ae235b
       * idle
Packit ae235b
       */
Packit ae235b
      g_object_unref (proxy);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void release_name (GDBusConnection *connection, gboolean wait);
Packit ae235b
Packit ae235b
static void
Packit ae235b
request_name_cb (GObject *source,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer user_data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection = G_DBUS_CONNECTION (source);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GVariant *var;
Packit ae235b
Packit ae235b
  var = g_dbus_connection_call_finish (connection, res, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
Packit ae235b
                    ==, DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER);
Packit ae235b
Packit ae235b
  release_name (connection, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
request_name (GDBusConnection *connection,
Packit ae235b
              gboolean         wait)
Packit ae235b
{
Packit ae235b
  g_dbus_connection_call (connection,
Packit ae235b
                          DBUS_SERVICE_DBUS,
Packit ae235b
                          DBUS_PATH_DBUS,
Packit ae235b
                          DBUS_INTERFACE_DBUS,
Packit ae235b
                          "RequestName",
Packit ae235b
                          g_variant_new ("(su)", MY_NAME, 0),
Packit ae235b
                          G_VARIANT_TYPE ("(u)"),
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          wait ? request_name_cb : NULL,
Packit ae235b
                          NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
release_name_cb (GObject *source,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer user_data)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection = G_DBUS_CONNECTION (source);
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GVariant *var;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  var = g_dbus_connection_call_finish (connection, res, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpuint (g_variant_get_uint32 (g_variant_get_child_value (var, 0)),
Packit ae235b
                    ==, DBUS_RELEASE_NAME_REPLY_RELEASED);
Packit ae235b
Packit ae235b
  /* generate some rapid NameOwnerChanged signals to try to trigger crashes */
Packit ae235b
  for (i = 0; i < N_RAPID_CYCLES; i++)
Packit ae235b
    {
Packit ae235b
      request_name (connection, FALSE);
Packit ae235b
      release_name (connection, FALSE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* wait for dbus-daemon to catch up */
Packit ae235b
  request_name (connection, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
release_name (GDBusConnection *connection,
Packit ae235b
              gboolean         wait)
Packit ae235b
{
Packit ae235b
  g_dbus_connection_call (connection,
Packit ae235b
                          DBUS_SERVICE_DBUS,
Packit ae235b
                          DBUS_PATH_DBUS,
Packit ae235b
                          DBUS_INTERFACE_DBUS,
Packit ae235b
                          "ReleaseName",
Packit ae235b
                          g_variant_new ("(s)", MY_NAME),
Packit ae235b
                          G_VARIANT_TYPE ("(u)"),
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          wait ? release_name_cb : NULL,
Packit ae235b
                          NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_proxy (void)
Packit ae235b
{
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GThread *proxy_threads[N_THREADS_MAX];
Packit ae235b
  int i;
Packit ae235b
  int n_threads;
Packit ae235b
Packit ae235b
  if (g_test_slow ())
Packit ae235b
    n_threads = N_THREADS_MAX;
Packit ae235b
  else
Packit ae235b
    n_threads = N_THREADS;
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, TRUE);
Packit ae235b
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
Packit ae235b
  request_name (connection, TRUE);
Packit ae235b
Packit ae235b
  for (i = 0; i < n_threads; i++)
Packit ae235b
    {
Packit ae235b
      proxy_threads[i] = g_thread_new ("run-proxy",
Packit ae235b
                                       run_proxy_thread, connection);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  for (i = 0; i < n_threads; i++)
Packit ae235b
    {
Packit ae235b
      g_thread_join (proxy_threads[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (connection);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  /* TODO: should call session_bus_down() but that requires waiting
Packit ae235b
   * for all the oustanding method calls to complete...
Packit ae235b
   */
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("\n");
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_dbus_unset ();
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/proxy/vs-threads", test_proxy);
Packit ae235b
Packit ae235b
  return g_test_run();
Packit ae235b
}