Remove redundant include
[platform/upstream/glib.git] / gio / tests / unix-streams.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc
3  *
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.
7  *
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.
11  *
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.
20  */
21
22 #include <gio/gio.h>
23 #include <gio/gunixinputstream.h>
24 #include <gio/gunixoutputstream.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #define DATA "abcdefghijklmnopqrstuvwxyz"
31
32 int writer_pipe[2], reader_pipe[2];
33 GCancellable *writer_cancel, *reader_cancel, *main_cancel;
34 GMainLoop *loop;
35
36 static gboolean
37 cancel_main (gpointer data)
38 {
39   GCancellable *main_cancel = data;
40
41   g_cancellable_cancel (main_cancel);
42
43   return FALSE;
44 }
45
46
47 static gpointer
48 writer_thread (gpointer user_data)
49 {
50   GOutputStream *out;
51   gssize nwrote, offset;
52   GError *err = NULL;
53
54   out = g_unix_output_stream_new (writer_pipe[1], TRUE);
55
56   do
57     {
58       g_usleep (10);
59
60       offset = 0;
61       while (offset < (gssize) sizeof (DATA))
62         {
63           nwrote = g_output_stream_write (out, DATA + offset,
64                                           sizeof (DATA) - offset,
65                                           writer_cancel, &err);
66           if (nwrote <= 0 || err != NULL)
67             break;
68           offset += nwrote;
69         }
70
71       g_assert (nwrote > 0 || err != NULL);
72     }
73   while (err == NULL);
74
75   if (g_cancellable_is_cancelled (writer_cancel))
76     {
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.
84        *
85        * Work around by deferring the cancellation to a timeout.
86        */
87       g_timeout_add (0, cancel_main, main_cancel);
88       g_object_unref (out);
89       return NULL;
90     }
91
92   g_warning ("writer: %s", err->message);
93   g_assert_not_reached ();
94 }
95
96 static gpointer
97 reader_thread (gpointer user_data)
98 {
99   GInputStream *in;
100   gssize nread = 0, total;
101   GError *err = NULL;
102   char buf[sizeof (DATA)];
103
104   in = g_unix_input_stream_new (reader_pipe[0], TRUE);
105
106   do
107     {
108       total = 0;
109       while (total < (gssize) sizeof (DATA))
110         {
111           nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
112                                        reader_cancel, &err);
113           if (nread <= 0 || err != NULL)
114             break;
115           total += nread;
116         }
117
118       if (err)
119         break;
120
121       if (nread == 0)
122         {
123           g_assert (err == NULL);
124           /* pipe closed */
125           g_object_unref (in);
126           return NULL;
127         }
128
129       g_assert_cmpstr (buf, ==, DATA);
130       g_assert (!g_cancellable_is_cancelled (reader_cancel));
131     }
132   while (err == NULL);
133
134   g_warning ("reader: %s", err->message);
135   g_assert_not_reached ();
136 }
137
138 char main_buf[sizeof (DATA)];
139 gssize main_len, main_offset;
140
141 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
142 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
143
144 static void
145 do_main_cancel (GOutputStream *out)
146 {
147   g_output_stream_close (out, NULL, NULL);
148   g_main_loop_quit (loop);
149 }
150
151 static void
152 readable (GObject *source, GAsyncResult *res, gpointer user_data)
153 {
154   GInputStream *in = G_INPUT_STREAM (source);
155   GOutputStream *out = user_data;
156   GError *err = NULL;
157
158   main_len = g_input_stream_read_finish (in, res, &err);
159
160   if (g_cancellable_is_cancelled (main_cancel))
161     {
162       do_main_cancel (out);
163       return;
164     }
165
166   g_assert (err == NULL);
167
168   main_offset = 0;
169   g_output_stream_write_async (out, main_buf, main_len,
170                                G_PRIORITY_DEFAULT, main_cancel,
171                                writable, in);
172 }
173
174 static void
175 writable (GObject *source, GAsyncResult *res, gpointer user_data)
176 {
177   GOutputStream *out = G_OUTPUT_STREAM (source);
178   GInputStream *in = user_data;
179   GError *err = NULL;
180   gssize nwrote;
181
182   nwrote = g_output_stream_write_finish (out, res, &err);
183
184   if (g_cancellable_is_cancelled (main_cancel))
185     {
186       do_main_cancel (out);
187       return;
188     }
189
190   g_assert (err == NULL);
191   g_assert_cmpint (nwrote, <=, main_len - main_offset);
192
193   main_offset += nwrote;
194   if (main_offset == main_len)
195     {
196       g_input_stream_read_async (in, main_buf, sizeof (main_buf),
197                                  G_PRIORITY_DEFAULT, main_cancel,
198                                  readable, out);
199     }
200   else
201     {
202       g_output_stream_write_async (out, main_buf + main_offset,
203                                    main_len - main_offset,
204                                    G_PRIORITY_DEFAULT, main_cancel,
205                                    writable, in);
206     }
207 }
208
209 static gboolean
210 timeout (gpointer cancellable)
211 {
212   g_cancellable_cancel (cancellable);
213   return FALSE;
214 }
215
216 static void
217 test_pipe_io (void)
218 {
219   GThread *writer, *reader;
220   GInputStream *in;
221   GOutputStream *out;
222
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.
231    */
232
233   g_assert (pipe (writer_pipe) == 0 && pipe (reader_pipe) == 0);
234
235   writer_cancel = g_cancellable_new ();
236   reader_cancel = g_cancellable_new ();
237   main_cancel = g_cancellable_new ();
238
239   writer = g_thread_create (writer_thread, NULL, TRUE, NULL);
240   reader = g_thread_create (reader_thread, NULL, TRUE, NULL);
241
242   in = g_unix_input_stream_new (writer_pipe[0], TRUE);
243   out = g_unix_output_stream_new (reader_pipe[1], TRUE);
244
245   g_input_stream_read_async (in, main_buf, sizeof (main_buf),
246                              G_PRIORITY_DEFAULT, main_cancel,
247                              readable, out);
248
249   g_timeout_add (500, timeout, writer_cancel);
250
251   loop = g_main_loop_new (NULL, TRUE);
252   g_main_loop_run (loop);
253   g_main_loop_unref (loop);
254
255   g_thread_join (reader);
256   g_thread_join (writer);
257
258   g_object_unref (main_cancel);
259   g_object_unref (reader_cancel);
260   g_object_unref (writer_cancel);
261   g_object_unref (in);
262   g_object_unref (out);
263 }
264
265 int
266 main (int   argc,
267       char *argv[])
268 {
269   g_thread_init (NULL);
270   g_type_init ();
271   g_test_init (&argc, &argv, NULL);
272
273   g_test_add_func ("/unix-streams/pipe-io-test", test_pipe_io);
274
275   return g_test_run();
276 }