[Spice-devel] [spice-gtk PATCH v2 5/5] tests: pipe using _write_all_async function

Victor Toso victortoso at redhat.com
Tue May 19 07:34:37 PDT 2015


---
 tests/pipe.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/tests/pipe.c b/tests/pipe.c
index 6dbd7cf..62e3a7b 100644
--- a/tests/pipe.c
+++ b/tests/pipe.c
@@ -16,6 +16,10 @@ typedef struct _Fixture {
     GOutputStream *op2;
 
     gchar buf[16];
+    gchar *data;
+    guint16 data_len;
+    guint16 read_size;
+    guint16 total_read;
 
     GMainLoop *loop;
     GCancellable *cancellable;
@@ -75,6 +79,7 @@ fixture_tear_down(Fixture *fixture,
     g_clear_object(&fixture->ip1);
     g_clear_object(&fixture->ip2);
 
+    g_clear_pointer(&fixture->data, g_free);
     g_clear_object(&fixture->cancellable);
     g_source_remove(fixture->timeout);
     g_main_loop_unref(fixture->loop);
@@ -274,6 +279,72 @@ test_pipe_readcancel(Fixture *f, gconstpointer user_data)
     g_main_loop_run (f->loop);
 }
 
+static gchar *
+get_test_data(gint n)
+{
+    GString *s = g_string_sized_new(n);
+    const gchar *data = "01234567abcdefgh";
+    gint i, q;
+
+    q = n / 16;
+    for (i = 0; i < q; i++)
+        s = g_string_append(s, data);
+
+    s = g_string_append_len(s, data, (n % 16));
+    return g_string_free(s, FALSE);
+}
+
+static void
+write_all_cb(GObject *source, GAsyncResult *result, gpointer user_data)
+{
+    Fixture *f = user_data;
+    GError *error = NULL;
+    gsize nbytes;
+
+    g_output_stream_write_all_finish(G_OUTPUT_STREAM(source), result, &nbytes, &error);
+    g_assert_no_error(error);
+    g_assert_cmpint(nbytes, ==, f->data_len);
+    g_clear_error(&error);
+
+    g_main_loop_quit (f->loop);
+}
+
+static void
+read_chunk_cb(GObject *source, GAsyncResult *result, gpointer user_data)
+{
+    Fixture *f = user_data;
+    GError *error = NULL;
+    gssize nbytes;
+    gboolean data_match;
+
+    nbytes = g_input_stream_read_finish(G_INPUT_STREAM(source), result, &error);
+    g_assert_no_error(error);
+    g_assert_cmpint(nbytes, >, 0);
+    data_match = (g_ascii_strncasecmp(f->data + f->total_read, f->buf, nbytes) == 0);
+    g_assert_true(data_match);
+
+    f->total_read += nbytes;
+    if (f->total_read != f->data_len) {
+        g_input_stream_read_async(f->ip2, f->buf, f->read_size, G_PRIORITY_DEFAULT,
+                                  f->cancellable, read_chunk_cb, f);
+    }
+}
+
+static void
+test_pipe_write_all_64_read_chunks_16(Fixture *f, gconstpointer user_data)
+{
+    f->data_len = 64;
+    f->data = get_test_data(f->data_len);
+    f->read_size = 16;
+    f->total_read = 0;
+
+    g_output_stream_write_all_async(f->op1, f->data, f->data_len, G_PRIORITY_DEFAULT,
+                                    f->cancellable, write_all_cb, f);
+    g_input_stream_read_async(f->ip2, f->buf, f->read_size, G_PRIORITY_DEFAULT,
+                              f->cancellable, read_chunk_cb, f);
+    g_main_loop_run (f->loop);
+}
+
 int main(int argc, char* argv[])
 {
     setlocale(LC_ALL, "");
@@ -304,6 +375,10 @@ int main(int argc, char* argv[])
                fixture_set_up, test_pipe_write8read16,
                fixture_tear_down);
 
+    g_test_add("/pipe/write-all64-read-chunks16", Fixture, NULL,
+               fixture_set_up, test_pipe_write_all_64_read_chunks_16,
+               fixture_tear_down);
+
     g_test_add("/pipe/readclosestream", Fixture, NULL,
                fixture_set_up, test_pipe_readclosestream,
                fixture_tear_down);
-- 
2.4.1



More information about the Spice-devel mailing list