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 cancel_main (gpointer data)
40 GCancellable *main_cancel = data;
42 g_cancellable_cancel (main_cancel);
49 writer_thread (gpointer user_data)
52 gssize nwrote, offset;
55 out = g_unix_output_stream_new (writer_pipe[1], TRUE);
62 while (offset < (gssize) sizeof (DATA))
64 nwrote = g_output_stream_write (out, DATA + offset,
65 sizeof (DATA) - offset,
67 if (nwrote <= 0 || err != NULL)
72 g_assert (nwrote > 0 || err != NULL);
76 if (g_cancellable_is_cancelled (writer_cancel))
78 /* FIXME: directly calling g_cancellable_cancel (main_cancel) here
79 * leads to sporadic deadlock, because it will try to wake up the
80 * main context, for which it needs to acquire the main context lock.
81 * This lock may be held by the main loop running in the main thread,
82 * and it may be held while the main thread is blocking in
83 * fd_source_finalize -> g_cancellable_disconnect
84 * until the ::cancelled callbacks have run.
86 * Work around by deferring the cancellation to a timeout.
88 g_timeout_add (0, cancel_main, main_cancel);
93 g_warning ("writer: %s", err->message);
94 g_assert_not_reached ();
98 reader_thread (gpointer user_data)
101 gssize nread = 0, total;
103 char buf[sizeof (DATA)];
105 in = g_unix_input_stream_new (reader_pipe[0], TRUE);
110 while (total < (gssize) sizeof (DATA))
112 nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
113 reader_cancel, &err);
114 if (nread <= 0 || err != NULL)
124 g_assert (err == NULL);
130 g_assert_cmpstr (buf, ==, DATA);
131 g_assert (!g_cancellable_is_cancelled (reader_cancel));
135 g_warning ("reader: %s", err->message);
136 g_assert_not_reached ();
139 char main_buf[sizeof (DATA)];
140 gssize main_len, main_offset;
142 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
143 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
146 do_main_cancel (GOutputStream *out)
148 g_output_stream_close (out, NULL, NULL);
149 g_main_loop_quit (loop);
153 readable (GObject *source, GAsyncResult *res, gpointer user_data)
155 GInputStream *in = G_INPUT_STREAM (source);
156 GOutputStream *out = user_data;
159 main_len = g_input_stream_read_finish (in, res, &err);
161 if (g_cancellable_is_cancelled (main_cancel))
163 do_main_cancel (out);
167 g_assert (err == NULL);
170 g_output_stream_write_async (out, main_buf, main_len,
171 G_PRIORITY_DEFAULT, main_cancel,
176 writable (GObject *source, GAsyncResult *res, gpointer user_data)
178 GOutputStream *out = G_OUTPUT_STREAM (source);
179 GInputStream *in = user_data;
183 nwrote = g_output_stream_write_finish (out, res, &err);
185 if (g_cancellable_is_cancelled (main_cancel))
187 do_main_cancel (out);
191 g_assert (err == NULL);
192 g_assert_cmpint (nwrote, <=, main_len - main_offset);
194 main_offset += nwrote;
195 if (main_offset == main_len)
197 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
198 G_PRIORITY_DEFAULT, main_cancel,
203 g_output_stream_write_async (out, main_buf + main_offset,
204 main_len - main_offset,
205 G_PRIORITY_DEFAULT, main_cancel,
211 timeout (gpointer cancellable)
213 g_cancellable_cancel (cancellable);
220 GThread *writer, *reader;
224 /* Split off two (additional) threads, a reader and a writer. From
225 * the writer thread, write data synchronously in small chunks,
226 * which gets read asynchronously by the main thread and then
227 * written asynchronously to the reader thread, which reads it
228 * synchronously. Eventually a timeout in the main thread will cause
229 * it to cancel the writer thread, which will in turn cancel the
230 * read op in the main thread, which will then close the pipe to
231 * the reader thread, causing the read op to fail.
234 g_assert (pipe (writer_pipe) == 0 && pipe (reader_pipe) == 0);
236 writer_cancel = g_cancellable_new ();
237 reader_cancel = g_cancellable_new ();
238 main_cancel = g_cancellable_new ();
240 writer = g_thread_create (writer_thread, NULL, TRUE, NULL);
241 reader = g_thread_create (reader_thread, NULL, TRUE, NULL);
243 in = g_unix_input_stream_new (writer_pipe[0], TRUE);
244 out = g_unix_output_stream_new (reader_pipe[1], TRUE);
246 g_input_stream_read_async (in, main_buf, sizeof (main_buf),
247 G_PRIORITY_DEFAULT, main_cancel,
250 g_timeout_add (500, timeout, writer_cancel);
252 loop = g_main_loop_new (NULL, TRUE);
253 g_main_loop_run (loop);
254 g_main_loop_unref (loop);
256 g_thread_join (reader);
257 g_thread_join (writer);
259 g_object_unref (main_cancel);
260 g_object_unref (reader_cancel);
261 g_object_unref (writer_cancel);
263 g_object_unref (out);
270 g_thread_init (NULL);
272 g_test_init (&argc, &argv, NULL);
274 g_test_add_func ("/unix-streams/pipe-io-test", test_pipe_io);