diff options
author | Victor Toso <victortoso@redhat.com> | 2015-05-19 15:12:59 +0200 |
---|---|---|
committer | Victor Toso <victortoso@redhat.com> | 2015-06-05 09:22:32 +0200 |
commit | da01ae66221e88be3f40055e90d6a7ce59c35fae (patch) | |
tree | a069f1907d67dc86fdbc1e2cfdb4f088eac48c24 /tests | |
parent | e5d2047812412bc7d5e11cfb63a4d8b55551a408 (diff) |
tests: pipe using _write_all_async function
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pipe.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/pipe.c b/tests/pipe.c index ec11b6f..e4757b6 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; @@ -68,6 +72,7 @@ fixture_tear_down(Fixture *fixture, g_clear_object(&fixture->p1); g_clear_object(&fixture->p2); + g_clear_pointer(&fixture->data, g_free); g_clear_object(&fixture->cancellable); g_source_remove(fixture->timeout); g_main_loop_unref(fixture->loop); @@ -253,6 +258,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, ""); @@ -283,6 +354,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); |