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.
23 #include <gio/gunixinputstream.h>
24 #include <gio/gunixoutputstream.h>
30 #define DATA "abcdefghijklmnopqrstuvwxyz"
32 int writer_pipe[2], reader_pipe[2];
33 GCancellable *writer_cancel, *reader_cancel, *main_cancel;
37 cancel_main (gpointer data)
39 GCancellable *main_cancel = data;
41 g_cancellable_cancel (main_cancel);
48 writer_thread (gpointer user_data)
51 gssize nwrote, offset;
54 out = g_unix_output_stream_new (writer_pipe[1], TRUE);
61 while (offset < (gssize) sizeof (DATA))
63 nwrote = g_output_stream_write (out, DATA + offset,
64 sizeof (DATA) - offset,
66 if (nwrote <= 0 || err != NULL)
71 g_assert (nwrote > 0 || err != NULL);
75 if (g_cancellable_is_cancelled (writer_cancel))
77 /* FIXME: directly calling g_cancellable_cancel (main_cancel) here
78 * leads to sporadic deadlock, because it will try to wake up the
79 * main context, for which it needs to acquire the main context lock.
80 * This lock may be held by the main loop running in the main thread,
81 * and it may be held while the main thread is blocking in
82 * fd_source_finalize -> g_cancellable_disconnect
83 * until the ::cancelled callbacks have run.
85 * Work around by deferring the cancellation to a timeout.
87 g_timeout_add (0, cancel_main, main_cancel);
92 g_warning ("writer: %s", err->message);
93 g_assert_not_reached ();
97 reader_thread (gpointer user_data)
100 gssize nread = 0, total;
102 char buf[sizeof (DATA)];
104 in = g_unix_input_stream_new (reader_pipe[0], TRUE);
109 while (total < (gssize) sizeof (DATA))
111 nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
112 reader_cancel, &err);
113 if (nread <= 0 || err != NULL)
123 g_assert (err == NULL);
129 g_assert_cmpstr (buf, ==, DATA);
130 g_assert (!g_cancellable_is_cancelled (reader_cancel));
134 g_warning ("reader: %s", err->message);
135 g_assert_not_reached ();
138 char main_buf[sizeof (DATA)];
139 gssize main_len, main_offset;
141 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
142 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
145 do_main_cancel (GOutputStream *out)
147 g_output_stream_close (out, NULL, NULL);
148 g_main_loop_quit (loop);
152 readable (GObject *source, GAsyncResult *res, gpointer user_data)
154 GInputStream *in = G_INPUT_STREAM (source);
155 GOutputStream *out = user_data;
158 main_len = g_input_stream_read_finish (in, res, &err);
160 if (g_cancellable_is_cancelled (main_cancel))
162 do_main_cancel (out);
166 g_assert (err == NULL);
169 g_output_stream_write_async (out, main_buf, main_len,
170 G_PRIORITY_DEFAULT, main_cancel,
175 writable (GObject *source, GAsyncResult *res, gpointer user_data)
177 GOutputStream *out = G_OUTPUT_STREAM (source);
178 GInputStream *in = user_data;
182 nwrote = g_output_stream_write_finish (out, res, &err);
184 if (g_cancellable_is_cancelled (main_cancel))
186 do_main_cancel (out);
190 g_assert (err == NULL);
191 g_assert_cmpint (nwrote, <=, main_len - main_offset);
193 main_offset += nwrote;
194 if (main_offset == main_len)
196 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
197 G_PRIORITY_DEFAULT, main_cancel,
202 g_output_stream_write_async (out, main_buf + main_offset,
203 main_len - main_offset,
204 G_PRIORITY_DEFAULT, main_cancel,
210 timeout (gpointer cancellable)
212 g_cancellable_cancel (cancellable);
219 GThread *writer, *reader;
223 /* Split off two (additional) threads, a reader and a writer. From
224 * the writer thread, write data synchronously in small chunks,
225 * which gets read asynchronously by the main thread and then
226 * written asynchronously to the reader thread, which reads it
227 * synchronously. Eventually a timeout in the main thread will cause
228 * it to cancel the writer thread, which will in turn cancel the
229 * read op in the main thread, which will then close the pipe to
230 * the reader thread, causing the read op to fail.
233 g_assert (pipe (writer_pipe) == 0 && pipe (reader_pipe) == 0);
235 writer_cancel = g_cancellable_new ();
236 reader_cancel = g_cancellable_new ();
237 main_cancel = g_cancellable_new ();
239 writer = g_thread_create (writer_thread, NULL, TRUE, NULL);
240 reader = g_thread_create (reader_thread, NULL, TRUE, NULL);
242 in = g_unix_input_stream_new (writer_pipe[0], TRUE);
243 out = g_unix_output_stream_new (reader_pipe[1], TRUE);
245 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
246 G_PRIORITY_DEFAULT, main_cancel,
249 g_timeout_add (500, timeout, writer_cancel);
251 loop = g_main_loop_new (NULL, TRUE);
252 g_main_loop_run (loop);
253 g_main_loop_unref (loop);
255 g_thread_join (reader);
256 g_thread_join (writer);
258 g_object_unref (main_cancel);
259 g_object_unref (reader_cancel);
260 g_object_unref (writer_cancel);
262 g_object_unref (out);
269 g_thread_init (NULL);
271 g_test_init (&argc, &argv, NULL);
273 g_test_add_func ("/unix-streams/pipe-io-test", test_pipe_io);