[kdbus] sync with kdbus (kdbus.h - commit: b024fb43c66b)
[platform/upstream/glib.git] / gio / tests / async-close-output-stream.c
1 /* GLib testing framework examples and tests
2  * Authors: Jesse van den Kieboom <jessevdk@gnome.org>
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 <glib/glib.h>
23 #include <gio/gio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define DATA_TO_WRITE "Hello world\n"
28
29 typedef struct
30 {
31   GOutputStream *conv_stream;
32   GOutputStream *data_stream;
33   gchar *expected_output;
34   gsize expected_size;
35   GMainLoop *main_loop;
36 } SetupData;
37
38 static void
39 create_streams (SetupData *data)
40 {
41   GConverter *converter;
42
43   converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, -1));
44
45   data->data_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
46   data->conv_stream = g_converter_output_stream_new (data->data_stream,
47                                                      converter);
48
49   g_object_unref (converter);
50 }
51
52 static void
53 destroy_streams (SetupData *data)
54 {
55   g_object_unref (data->data_stream);
56   g_object_unref (data->conv_stream);
57 }
58
59 static void
60 write_data_to_stream (SetupData *data)
61 {
62   gsize bytes_written;
63   GError *error = NULL;
64
65   /* just write the data synchronously */
66   g_output_stream_write_all (data->conv_stream,
67                              DATA_TO_WRITE,
68                              sizeof (DATA_TO_WRITE),
69                              &bytes_written,
70                              NULL,
71                              &error);
72
73   g_assert_no_error (error);
74   g_assert_cmpint (sizeof (DATA_TO_WRITE), ==, bytes_written);
75 }
76
77 static void
78 setup_data (SetupData     *data,
79             gconstpointer  user_data)
80 {
81   data->main_loop = g_main_loop_new (NULL, FALSE);
82   create_streams (data);
83 }
84
85 static void
86 teardown_data (SetupData     *data,
87                gconstpointer  user_data)
88 {
89   /* cleanup */
90   g_main_loop_unref (data->main_loop);
91
92   destroy_streams (data);
93
94   g_free (data->expected_output);
95 }
96
97 static void
98 compare_output (SetupData *data)
99 {
100   gsize size;
101   gpointer written;
102
103   size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
104
105   /* compare the size of the data */
106   g_assert_cmpint (size, ==, data->expected_size);
107
108   written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream));
109
110   /* compare the data itself */
111   g_assert (memcmp (written, data->expected_output, size) == 0);
112 }
113
114 static void
115 async_close_ready (GOutputStream *stream,
116                    GAsyncResult  *result,
117                    SetupData     *data)
118 {
119   GError *error = NULL;
120
121   /* finish the close */
122   g_output_stream_close_finish (stream, result, &error);
123
124   g_assert_no_error (error);
125
126   /* compare the output with the desired output */
127   compare_output (data);
128
129   g_main_loop_quit (data->main_loop);
130 }
131
132 static void
133 prepare_data (SetupData *data,
134               gboolean   manual_flush)
135 {
136   GError *error = NULL;
137   gpointer written;
138
139   write_data_to_stream (data);
140
141   if (manual_flush)
142     {
143       g_output_stream_flush (data->conv_stream, NULL, &error);
144       g_assert_no_error (error);
145     }
146
147   g_output_stream_close (data->conv_stream, NULL, &error);
148
149   g_assert_no_error (error);
150
151   written = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->data_stream));
152
153   data->expected_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->data_stream));
154
155   g_assert_cmpint (data->expected_size, >, 0);
156
157   data->expected_output = g_memdup (written, (guint)data->expected_size);
158
159   /* then recreate the streams and prepare them for the asynchronous close */
160   destroy_streams (data);
161   create_streams (data);
162
163   write_data_to_stream (data);
164 }
165
166 static void
167 test_without_flush (SetupData     *data,
168                     gconstpointer  user_data)
169 {
170   prepare_data (data, FALSE);
171
172   g_test_bug ("617937");
173
174   /* just close asynchronously */
175   g_output_stream_close_async (data->conv_stream,
176                                G_PRIORITY_DEFAULT,
177                                NULL,
178                                (GAsyncReadyCallback)async_close_ready,
179                                data);
180
181   g_main_loop_run (data->main_loop);
182 }
183
184 static void
185 test_with_flush (SetupData *data, gconstpointer user_data)
186 {
187   GError *error = NULL;
188
189   g_test_bug ("617937");
190
191   prepare_data (data, TRUE);
192
193   g_output_stream_flush (data->conv_stream, NULL, &error);
194
195   g_assert_no_error (error);
196
197   /* then close asynchronously */
198   g_output_stream_close_async (data->conv_stream,
199                                G_PRIORITY_DEFAULT,
200                                NULL,
201                                (GAsyncReadyCallback)async_close_ready,
202                                data);
203
204   g_main_loop_run (data->main_loop);
205 }
206
207 static void
208 async_flush_ready (GOutputStream *stream,
209                    GAsyncResult  *result,
210                    SetupData     *data)
211 {
212   GError *error = NULL;
213
214   g_output_stream_flush_finish (stream, result, &error);
215
216   g_assert_no_error (error);
217
218   /* then close async after the flush */
219   g_output_stream_close_async (data->conv_stream,
220                                G_PRIORITY_DEFAULT,
221                                NULL,
222                                (GAsyncReadyCallback)async_close_ready,
223                                data);
224 }
225
226 static void
227 test_with_async_flush (SetupData     *data,
228                        gconstpointer  user_data)
229 {
230   g_test_bug ("617937");
231
232   prepare_data (data, TRUE);
233
234   /* first flush async */
235   g_output_stream_flush_async (data->conv_stream,
236                                G_PRIORITY_DEFAULT,
237                                NULL,
238                                (GAsyncReadyCallback)async_flush_ready,
239                                data);
240
241   g_main_loop_run (data->main_loop);
242 }
243
244 int
245 main (int   argc,
246       char *argv[])
247 {
248   SetupData *data;
249
250   g_test_init (&argc, &argv, NULL);
251
252   g_test_bug_base ("http://bugzilla.gnome.org/");
253
254   data = g_slice_new (SetupData);
255
256   /* test closing asynchronously without flushing manually */
257   g_test_add ("/close-async/without-flush",
258               SetupData,
259               data,
260               setup_data,
261               test_without_flush,
262               teardown_data);
263
264   /* test closing asynchronously with a synchronous manually flush */
265   g_test_add ("/close-async/with-flush",
266               SetupData,
267               data,
268               setup_data,
269               test_with_flush,
270               teardown_data);
271
272   /* test closing asynchronously with an asynchronous manually flush */
273   g_test_add ("/close-async/with-async-flush",
274               SetupData,
275               data,
276               setup_data,
277               test_with_async_flush,
278               teardown_data);
279
280   g_slice_free (SetupData, data);
281
282   return g_test_run();
283 }