Blame gio/tests/gdbus-connection-flush.c

Packit ae235b
/* Test case for GNOME #662395
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 "test-io-stream.h"
Packit ae235b
#include "test-pipe-unix.h"
Packit ae235b
Packit ae235b
#define MY_TYPE_OUTPUT_STREAM \
Packit ae235b
        (my_output_stream_get_type ())
Packit ae235b
#define MY_OUTPUT_STREAM(o) \
Packit ae235b
        (G_TYPE_CHECK_INSTANCE_CAST ((o), \
Packit ae235b
                                     MY_TYPE_OUTPUT_STREAM, \
Packit ae235b
                                     MyOutputStream))
Packit ae235b
#define MY_IS_OUTPUT_STREAM(o) \
Packit ae235b
        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_OUTPUT_STREAM))
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (write);
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
    GFilterOutputStream parent;
Packit ae235b
Packit ae235b
    volatile gint started;
Packit ae235b
    volatile gint finished;
Packit ae235b
    volatile gint flushed;
Packit ae235b
Packit ae235b
    GOutputStream *real_output;
Packit ae235b
} MyOutputStream;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
    GFilterOutputStreamClass parent;
Packit ae235b
} MyOutputStreamClass;
Packit ae235b
Packit ae235b
static GType my_output_stream_get_type (void) G_GNUC_CONST;
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (MyOutputStream, my_output_stream, G_TYPE_FILTER_OUTPUT_STREAM)
Packit ae235b
Packit ae235b
/* Called from GDBusWorker thread */
Packit ae235b
static gssize
Packit ae235b
my_output_stream_write (GOutputStream  *os,
Packit ae235b
                        const void     *buffer,
Packit ae235b
                        gsize           count,
Packit ae235b
                        GCancellable   *cancellable,
Packit ae235b
                        GError        **error)
Packit ae235b
{
Packit ae235b
  MyOutputStream *self = MY_OUTPUT_STREAM (os);
Packit ae235b
  GFilterOutputStream *filter = G_FILTER_OUTPUT_STREAM (os);
Packit ae235b
  GOutputStream *real = g_filter_output_stream_get_base_stream (filter);
Packit ae235b
  gssize ret;
Packit ae235b
Packit ae235b
  g_atomic_int_add (&self->started, count);
Packit ae235b
  /* Other threads can make writing block forever by taking this lock */
Packit ae235b
  G_LOCK (write);
Packit ae235b
  ret = g_output_stream_write (real, buffer, count, cancellable, error);
Packit ae235b
  G_UNLOCK (write);
Packit ae235b
  g_atomic_int_add (&self->finished, count);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Called from GDBusWorker thread */
Packit ae235b
static gboolean
Packit ae235b
my_output_stream_flush (GOutputStream             *os,
Packit ae235b
                        GCancellable              *cancellable,
Packit ae235b
                        GError                   **error)
Packit ae235b
{
Packit ae235b
  MyOutputStream *self = MY_OUTPUT_STREAM (os);
Packit ae235b
  GFilterOutputStream *filter = G_FILTER_OUTPUT_STREAM (os);
Packit ae235b
  GOutputStream *real = g_filter_output_stream_get_base_stream (filter);
Packit ae235b
  gint started, finished;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  /* These should be equal because you're not allowed to flush with a
Packit ae235b
   * write pending, and GOutputStream enforces that for its subclasses
Packit ae235b
   */
Packit ae235b
  started = g_atomic_int_get (&self->started);
Packit ae235b
  finished = g_atomic_int_get (&self->finished);
Packit ae235b
  g_assert_cmpint (started, ==, finished);
Packit ae235b
Packit ae235b
  ret = g_output_stream_flush (real, cancellable, error);
Packit ae235b
Packit ae235b
  /* As above, this shouldn't have changed during the flush */
Packit ae235b
  finished = g_atomic_int_get (&self->finished);
Packit ae235b
  g_assert_cmpint (started, ==, finished);
Packit ae235b
Packit ae235b
  /* Checkpoint reached */
Packit ae235b
  g_atomic_int_set (&self->flushed, finished);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Called from any thread; thread-safe */
Packit ae235b
static gint
Packit ae235b
my_output_stream_get_bytes_started (GOutputStream *os)
Packit ae235b
{
Packit ae235b
  MyOutputStream *self = MY_OUTPUT_STREAM (os);
Packit ae235b
Packit ae235b
  return g_atomic_int_get (&self->started);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Called from any thread; thread-safe */
Packit ae235b
static gint
Packit ae235b
my_output_stream_get_bytes_finished (GOutputStream *os)
Packit ae235b
{
Packit ae235b
  MyOutputStream *self = MY_OUTPUT_STREAM (os);
Packit ae235b
Packit ae235b
  return g_atomic_int_get (&self->finished);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Called from any thread; thread-safe */
Packit ae235b
static gint
Packit ae235b
my_output_stream_get_bytes_flushed (GOutputStream *os)
Packit ae235b
{
Packit ae235b
  MyOutputStream *self = MY_OUTPUT_STREAM (os);
Packit ae235b
Packit ae235b
  return g_atomic_int_get (&self->flushed);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
my_output_stream_init (MyOutputStream *self)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
my_output_stream_class_init (MyOutputStreamClass *cls)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *ostream_class = (GOutputStreamClass *) cls;
Packit ae235b
Packit ae235b
  ostream_class->write_fn = my_output_stream_write;
Packit ae235b
  ostream_class->flush = my_output_stream_flush;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
    GError *error;
Packit ae235b
    gchar *guid;
Packit ae235b
    gboolean flushed;
Packit ae235b
Packit ae235b
    GIOStream *client_stream;
Packit ae235b
    GInputStream *client_istream;
Packit ae235b
    GOutputStream *client_ostream;
Packit ae235b
    GOutputStream *client_real_ostream;
Packit ae235b
    GDBusConnection *client_conn;
Packit ae235b
Packit ae235b
    GIOStream *server_stream;
Packit ae235b
    GInputStream *server_istream;
Packit ae235b
    GOutputStream *server_ostream;
Packit ae235b
    GDBusConnection *server_conn;
Packit ae235b
} Fixture;
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup_client_cb (GObject      *source,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Fixture *f = user_data;
Packit ae235b
Packit ae235b
  f->client_conn = g_dbus_connection_new_finish (res, &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (G_IS_DBUS_CONNECTION (f->client_conn));
Packit ae235b
  g_assert (f->client_conn == G_DBUS_CONNECTION (source));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup_server_cb (GObject      *source,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Fixture *f = user_data;
Packit ae235b
Packit ae235b
  f->server_conn = g_dbus_connection_new_finish (res, &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (G_IS_DBUS_CONNECTION (f->server_conn));
Packit ae235b
  g_assert (f->server_conn == G_DBUS_CONNECTION (source));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
setup (Fixture       *f,
Packit ae235b
       gconstpointer  test_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  gboolean ok;
Packit ae235b
Packit ae235b
  f->guid = g_dbus_generate_guid ();
Packit ae235b
Packit ae235b
  ok = test_pipe (&f->server_istream, &f->client_real_ostream, &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (G_IS_OUTPUT_STREAM (f->client_real_ostream));
Packit ae235b
  g_assert (G_IS_INPUT_STREAM (f->server_istream));
Packit ae235b
  g_assert (ok);
Packit ae235b
Packit ae235b
  f->client_ostream = g_object_new (MY_TYPE_OUTPUT_STREAM,
Packit ae235b
                                    "base-stream", f->client_real_ostream,
Packit ae235b
                                    "close-base-stream", TRUE,
Packit ae235b
                                    NULL);
Packit ae235b
  g_assert (G_IS_OUTPUT_STREAM (f->client_ostream));
Packit ae235b
Packit ae235b
  ok = test_pipe (&f->client_istream, &f->server_ostream, &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (G_IS_OUTPUT_STREAM (f->server_ostream));
Packit ae235b
  g_assert (G_IS_INPUT_STREAM (f->client_istream));
Packit ae235b
  g_assert (ok);
Packit ae235b
Packit ae235b
  f->client_stream = test_io_stream_new (f->client_istream, f->client_ostream);
Packit ae235b
  f->server_stream = test_io_stream_new (f->server_istream, f->server_ostream);
Packit ae235b
Packit ae235b
  g_dbus_connection_new (f->client_stream, NULL,
Packit ae235b
                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
Packit ae235b
                         NULL, NULL, setup_client_cb, f);
Packit ae235b
  g_dbus_connection_new (f->server_stream, f->guid,
Packit ae235b
                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
Packit ae235b
                         NULL, NULL, setup_server_cb, f);
Packit ae235b
Packit ae235b
  while (f->client_conn == NULL || f->server_conn == NULL)
Packit ae235b
    g_main_context_iteration (NULL, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
flush_cb (GObject      *source,
Packit ae235b
          GAsyncResult *res,
Packit ae235b
          gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Fixture *f = user_data;
Packit ae235b
  gboolean ok;
Packit ae235b
Packit ae235b
  g_assert (G_IS_DBUS_CONNECTION (source));
Packit ae235b
  g_assert (G_IS_DBUS_CONNECTION (f->client_conn));
Packit ae235b
  g_assert_cmpuint ((guintptr) f->client_conn, ==, (guintptr) G_DBUS_CONNECTION (source));
Packit ae235b
Packit ae235b
  ok = g_dbus_connection_flush_finish (f->client_conn, res, &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (ok);
Packit ae235b
Packit ae235b
  f->flushed = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_flush_busy (Fixture       *f,
Packit ae235b
                 gconstpointer  test_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  gint initial, started;
Packit ae235b
  gboolean ok;
Packit ae235b
Packit ae235b
  initial = my_output_stream_get_bytes_started (f->client_ostream);
Packit ae235b
  /* make sure the actual write will block */
Packit ae235b
  G_LOCK (write);
Packit ae235b
Packit ae235b
  ok = g_dbus_connection_emit_signal (f->client_conn, NULL, "/",
Packit ae235b
                                      "com.example.Foo", "SomeSignal", NULL,
Packit ae235b
                                      &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (ok);
Packit ae235b
Packit ae235b
  /* wait for at least part of the message to have started writing -
Packit ae235b
   * the write will block indefinitely in the worker thread
Packit ae235b
   */
Packit ae235b
  do {
Packit ae235b
    started = my_output_stream_get_bytes_started (f->client_ostream);
Packit ae235b
    g_thread_yield ();
Packit ae235b
  } while (initial >= started);
Packit ae235b
Packit ae235b
  /* we haven't flushed anything */
Packit ae235b
  g_assert_cmpint (my_output_stream_get_bytes_flushed (f->client_ostream),
Packit ae235b
                   <=, initial);
Packit ae235b
Packit ae235b
  /* start to flush: it can't happen til the write finishes */
Packit ae235b
  g_dbus_connection_flush (f->client_conn, NULL, flush_cb, f);
Packit ae235b
Packit ae235b
  /* we still haven't actually flushed anything */
Packit ae235b
  g_assert_cmpint (my_output_stream_get_bytes_flushed (f->client_ostream),
Packit ae235b
                   <=, initial);
Packit ae235b
Packit ae235b
  /* let the write finish */
Packit ae235b
  G_UNLOCK (write);
Packit ae235b
Packit ae235b
  /* wait for the flush to happen */
Packit ae235b
  while (!f->flushed)
Packit ae235b
    g_main_context_iteration (NULL, TRUE);
Packit ae235b
Packit ae235b
  /* now we have flushed at least what we'd written - but before fixing
Packit ae235b
   * GNOME#662395 this assertion would fail
Packit ae235b
   */
Packit ae235b
  g_assert_cmpint (my_output_stream_get_bytes_flushed (f->client_ostream),
Packit ae235b
                   >=, started);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_flush_idle (Fixture       *f,
Packit ae235b
                 gconstpointer  test_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  gint initial, finished;
Packit ae235b
  gboolean ok;
Packit ae235b
Packit ae235b
  initial = my_output_stream_get_bytes_finished (f->client_ostream);
Packit ae235b
Packit ae235b
  ok = g_dbus_connection_emit_signal (f->client_conn, NULL, "/",
Packit ae235b
                                      "com.example.Foo", "SomeSignal", NULL,
Packit ae235b
                                      &f->error);
Packit ae235b
  g_assert_no_error (f->error);
Packit ae235b
  g_assert (ok);
Packit ae235b
Packit ae235b
  /* wait for at least part of the message to have been written */
Packit ae235b
  do {
Packit ae235b
    finished = my_output_stream_get_bytes_finished (f->client_ostream);
Packit ae235b
    g_thread_yield ();
Packit ae235b
  } while (initial >= finished);
Packit ae235b
Packit ae235b
  /* we haven't flushed anything */
Packit ae235b
  g_assert_cmpint (my_output_stream_get_bytes_flushed (f->client_ostream),
Packit ae235b
                   <=, initial);
Packit ae235b
Packit ae235b
  /* flush with fully-written, but unflushed, messages */
Packit ae235b
  ok = g_dbus_connection_flush_sync (f->client_conn, NULL, &f->error);
Packit ae235b
Packit ae235b
  /* now we have flushed at least what we'd written - but before fixing
Packit ae235b
   * GNOME#662395 this assertion would fail
Packit ae235b
   */
Packit ae235b
  g_assert_cmpint (my_output_stream_get_bytes_flushed (f->client_ostream),
Packit ae235b
                   >=, finished);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
teardown (Fixture       *f,
Packit ae235b
          gconstpointer  test_data G_GNUC_UNUSED)
Packit ae235b
{
Packit ae235b
  g_clear_error (&f->error);
Packit ae235b
Packit ae235b
  g_clear_object (&f->client_stream);
Packit ae235b
  g_clear_object (&f->client_istream);
Packit ae235b
  g_clear_object (&f->client_ostream);
Packit ae235b
  g_clear_object (&f->client_real_ostream);
Packit ae235b
  g_clear_object (&f->client_conn);
Packit ae235b
Packit ae235b
  g_clear_object (&f->server_stream);
Packit ae235b
  g_clear_object (&f->server_istream);
Packit ae235b
  g_clear_object (&f->server_ostream);
Packit ae235b
  g_clear_object (&f->server_conn);
Packit ae235b
Packit ae235b
  g_free (f->guid);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add ("/gdbus/connection/flush/busy", Fixture, NULL,
Packit ae235b
              setup, test_flush_busy, teardown);
Packit ae235b
  g_test_add ("/gdbus/connection/flush/idle", Fixture, NULL,
Packit ae235b
              setup, test_flush_idle, teardown);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}