[wing/wip/named-pipe-test-write] named-pipe: add unit test for async read and write



commit cad00bd4f71a1302f36358405613c43250813d92
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Sep 1 09:01:37 2016 +0200

    named-pipe: add unit test for async read and write

 tests/named-pipe.c |  168 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 168 insertions(+), 0 deletions(-)
---
diff --git a/tests/named-pipe.c b/tests/named-pipe.c
index de4b7c2..3133c1a 100644
--- a/tests/named-pipe.c
+++ b/tests/named-pipe.c
@@ -383,6 +383,174 @@ test_client_default_timeout (void)
   g_object_unref (client);
 }
 
+static void
+accepted_read_write_cb (GObject      *source,
+                        GAsyncResult *result,
+                        gpointer      user_data)
+{
+  WingNamedPipeListener *listener = WING_NAMED_PIPE_LISTENER (source);
+  WingNamedPipeConnection **conn = user_data;
+  GError *error = NULL;
+
+  *conn = wing_named_pipe_listener_accept_finish (listener, result, NULL, &error);
+  g_assert_no_error (error);
+}
+
+static void
+connected_read_write_cb (GObject      *source,
+                         GAsyncResult *result,
+                         gpointer      user_data)
+{
+  WingNamedPipeClient *client = WING_NAMED_PIPE_CLIENT (source);
+  WingNamedPipeConnection **conn = user_data;
+  GError *error = NULL;
+
+  conn = wing_named_pipe_client_connect_finish (client, result, &error);
+  g_assert_no_error (error);
+}
+
+typedef struct
+{
+  gchar data[256];
+  gint *times_read;
+} ReadData;
+
+#define WRITE_ITERATIONS 100
+static const gchar *some_text = "This is some data to read and to write";
+
+static void
+on_some_text_read (GObject      *source,
+                   GAsyncResult *result,
+                   gpointer      user_data)
+{
+  ReadData *data = user_data;
+  gssize read;
+  GError *error = NULL;
+
+  read = g_input_stream_read_finish (G_INPUT_STREAM (source), result, &error);
+  g_assert_no_error (error);
+  g_assert_cmp (read, ==, strlen (some_text) + 1);
+
+  *data->times_read--;
+  g_free (data);
+}
+
+static void
+on_some_text_written (GObject      *source,
+                      GAsyncResult *result,
+                      gpointer      user_data)
+{
+  gint *times = user_data;
+  gssize read;
+  GError *error = NULL;
+
+  read = g_output_stream_write_finish (G_OUTPUT_STREAM (source), result, &error);
+  g_assert_no_error (error);
+  g_assert_cmp (read, ==, strlen (some_text) + 1);
+
+  *times--;
+}
+
+static void
+test_read_write_basic (void)
+{
+  WingNamedPipeListener *listener;
+  WingNamedPipeClient *client;
+  WingNamedPipeConnection *conn_server = NULL;
+  WingNamedPipeConnection *conn_client = NULL;
+  GInputStream *in;
+  GOutputStream *out;
+  gint times_to_write_server = WRITE_ITERATIONS;
+  gint times_to_read_server = WRITE_ITERATIONS;
+  gint times_to_write_client = WRITE_ITERATIONS;
+  gint times_to_read_client = WRITE_ITERATIONS;
+  gint i;
+  GError *error = NULL;
+
+  listener = wing_named_pipe_listener_new ();
+
+  wing_named_pipe_listener_add_named_pipe (listener,
+                                           "\\\\.\\pipe\\gtest-named-pipe-name",
+                                           NULL,
+                                           &error);
+  g_assert_no_error (error);
+
+  wing_named_pipe_listener_accept_async (listener,
+                                         NULL,
+                                         accepted_read_write_cb,
+                                         &conn_server);
+
+  client = wing_named_pipe_client_new ();
+  wing_named_pipe_client_connect_async (client,
+                                        "\\\\.\\pipe\\gtest-named-pipe-name",
+                                        NULL,
+                                        connected_read_write_cb,
+                                        &conn_client);
+
+  do
+    g_main_context_iteration (NULL, TRUE);
+  while (conn_server == NULL || conn_client == NULL);
+
+  for (i = 0; i < WRITE_ITERATIONS; i++)
+    {
+      ReadData *data;
+
+      /* Server */
+      out = g_io_stream_get_output_stream (G_IO_STREAM (conn_server));
+      in = g_io_stream_get_input_stream (G_IO_STREAM (conn_server));
+
+      g_output_stream_write_async (out,
+                                   some_text,
+                                   strlen (some_text) + 1,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL,
+                                   on_some_text_written,
+                                   &times_to_write_server);
+
+      data = g_new0 (ReadData, 1);
+      data->times_read = &times_to_read_server;
+      g_input_stream_read_async (in,
+                                 data->data,
+                                 sizeof (data->data),
+                                 G_PRIORITY_DEFAULT,
+                                 NULL,
+                                 on_some_text_read,
+                                 data);
+
+      /* Client */
+      out = g_io_stream_get_output_stream (G_IO_STREAM (conn_client));
+      in = g_io_stream_get_input_stream (G_IO_STREAM (conn_client));
+
+      g_output_stream_write_async (out,
+                                   some_text,
+                                   strlen (some_text) + 1,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL,
+                                   on_some_text_written,
+                                   &times_to_write_client);
+
+      data = g_new0 (ReadData, 1);
+      data->times_read = &times_to_read_client;
+      g_input_stream_read_async (in,
+                                 data->data,
+                                 sizeof (data->data),
+                                 G_PRIORITY_DEFAULT,
+                                 NULL,
+                                 on_some_text_read,
+                                 data);
+    }
+
+  do
+    g_main_context_iteration (NULL, TRUE);
+  while (times_to_write_server != 0 || times_to_write_client != 0 ||
+         times_to_read_server != 0 || times_to_read_client != 0);
+
+  g_object_unref (conn_client);
+  g_object_unref (conn_server);
+  g_object_unref (client);
+  g_object_unref (listener);
+}
+
 int
 main (int   argc,
       char *argv[])


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