1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #include <glib/glib.h>
24 #include <gio/gunixinputstream.h>
25 #include <gio/gunixoutputstream.h>
31 #define DATA "abcdefghijklmnopqrstuvwxyz"
33 int writer_pipe[2], reader_pipe[2];
34 GCancellable *writer_cancel, *reader_cancel, *main_cancel;
38 writer_thread (gpointer user_data)
41 gssize nwrote, offset;
44 out = g_unix_output_stream_new (writer_pipe[1], TRUE);
51 while (offset < (gssize) sizeof (DATA))
53 nwrote = g_output_stream_write (out, DATA + offset,
54 sizeof (DATA) - offset,
56 if (nwrote <= 0 || err != NULL)
61 g_assert (nwrote > 0 || err != NULL);
65 if (g_cancellable_is_cancelled (writer_cancel))
67 g_cancellable_cancel (main_cancel);
72 g_warning ("writer: %s", err->message);
73 g_assert_not_reached ();
77 reader_thread (gpointer user_data)
80 gssize nread = 0, total;
82 char buf[sizeof (DATA)];
84 in = g_unix_input_stream_new (reader_pipe[0], TRUE);
89 while (total < (gssize) sizeof (DATA))
91 nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
93 if (nread <= 0 || err != NULL)
103 g_assert (err == NULL);
109 g_assert_cmpstr (buf, ==, DATA);
110 g_assert (!g_cancellable_is_cancelled (reader_cancel));
114 g_warning ("reader: %s", err->message);
115 g_assert_not_reached ();
118 char main_buf[sizeof (DATA)];
119 gssize main_len, main_offset;
121 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
122 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
125 do_main_cancel (GOutputStream *out)
127 g_output_stream_close (out, NULL, NULL);
128 g_main_loop_quit (loop);
132 readable (GObject *source, GAsyncResult *res, gpointer user_data)
134 GInputStream *in = G_INPUT_STREAM (source);
135 GOutputStream *out = user_data;
138 main_len = g_input_stream_read_finish (in, res, &err);
140 if (g_cancellable_is_cancelled (main_cancel))
142 do_main_cancel (out);
146 g_assert (err == NULL);
149 g_output_stream_write_async (out, main_buf, main_len,
150 G_PRIORITY_DEFAULT, main_cancel,
155 writable (GObject *source, GAsyncResult *res, gpointer user_data)
157 GOutputStream *out = G_OUTPUT_STREAM (source);
158 GInputStream *in = user_data;
162 nwrote = g_output_stream_write_finish (out, res, &err);
164 if (g_cancellable_is_cancelled (main_cancel))
166 do_main_cancel (out);
170 g_assert (err == NULL);
171 g_assert_cmpint (nwrote, <=, main_len - main_offset);
173 main_offset += nwrote;
174 if (main_offset == main_len)
176 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
177 G_PRIORITY_DEFAULT, main_cancel,
182 g_output_stream_write_async (out, main_buf + main_offset,
183 main_len - main_offset,
184 G_PRIORITY_DEFAULT, main_cancel,
190 timeout (gpointer cancellable)
192 g_cancellable_cancel (cancellable);
199 GThread *writer, *reader;
203 /* Split off two (additional) threads, a reader and a writer. From
204 * the writer thread, write data synchronously in small chunks,
205 * which gets read asynchronously by the main thread and then
206 * written asynchronously to the reader thread, which reads it
207 * synchronously. Eventually a timeout in the main thread will cause
208 * it to cancel the writer thread, which will in turn cancel the
209 * read op in the main thread, which will then close the pipe to
210 * the reader thread, causing the read op to fail.
213 g_assert (pipe (writer_pipe) == 0 && pipe (reader_pipe) == 0);
215 writer_cancel = g_cancellable_new ();
216 reader_cancel = g_cancellable_new ();
217 main_cancel = g_cancellable_new ();
219 writer = g_thread_create (writer_thread, NULL, TRUE, NULL);
220 reader = g_thread_create (reader_thread, NULL, TRUE, NULL);
222 in = g_unix_input_stream_new (writer_pipe[0], TRUE);
223 out = g_unix_output_stream_new (reader_pipe[1], TRUE);
225 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
226 G_PRIORITY_DEFAULT, main_cancel,
229 g_timeout_add (500, timeout, writer_cancel);
231 loop = g_main_loop_new (NULL, TRUE);
232 g_main_loop_run (loop);
233 g_main_loop_unref (loop);
235 g_thread_join (reader);
236 g_thread_join (writer);
238 g_object_unref (main_cancel);
239 g_object_unref (reader_cancel);
240 g_object_unref (writer_cancel);
242 g_object_unref (out);
249 g_thread_init (NULL);
251 g_test_init (&argc, &argv, NULL);
253 g_test_add_func ("/unix-streams/pipe-io-test", test_pipe_io);