1 /* GDBus regression test - close a stream when a message remains to be written
3 * Copyright © 2006-2010 Red Hat, Inc.
4 * Copyright © 2011 Nokia Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
34 # include <glib/glib-unix.h>
35 # include <gio/gunixinputstream.h>
36 # include <gio/gunixoutputstream.h>
37 # include <gio/gunixconnection.h>
39 # error This test is currently Unix-specific due to use of g_unix_open_pipe()
42 #include "gdbus-tests.h"
44 #define CLOSE_TIME_MS 1
45 #define N_REPEATS_SLOW 5000
48 /* ---------- MyIOStream ------------------------------------------------- */
50 #define MY_TYPE_IO_STREAM (my_io_stream_get_type ())
51 #define MY_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_IO_STREAM, MyIOStream))
52 #define MY_IS_IO_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_IO_STREAM))
56 GIOStream parent_instance;
57 GInputStream *input_stream;
58 GOutputStream *output_stream;
63 GIOStreamClass parent_class;
66 static GType my_io_stream_get_type (void) G_GNUC_CONST;
68 G_DEFINE_TYPE (MyIOStream, my_io_stream, G_TYPE_IO_STREAM)
71 my_io_stream_finalize (GObject *object)
73 MyIOStream *stream = MY_IO_STREAM (object);
74 g_object_unref (stream->input_stream);
75 g_object_unref (stream->output_stream);
76 G_OBJECT_CLASS (my_io_stream_parent_class)->finalize (object);
80 my_io_stream_init (MyIOStream *stream)
85 my_io_stream_get_input_stream (GIOStream *_stream)
87 MyIOStream *stream = MY_IO_STREAM (_stream);
88 return stream->input_stream;
91 static GOutputStream *
92 my_io_stream_get_output_stream (GIOStream *_stream)
94 MyIOStream *stream = MY_IO_STREAM (_stream);
95 return stream->output_stream;
99 my_io_stream_class_init (MyIOStreamClass *klass)
101 GObjectClass *gobject_class;
102 GIOStreamClass *giostream_class;
104 gobject_class = G_OBJECT_CLASS (klass);
105 gobject_class->finalize = my_io_stream_finalize;
107 giostream_class = G_IO_STREAM_CLASS (klass);
108 giostream_class->get_input_stream = my_io_stream_get_input_stream;
109 giostream_class->get_output_stream = my_io_stream_get_output_stream;
113 my_io_stream_new (GInputStream *input_stream,
114 GOutputStream *output_stream)
117 g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), NULL);
118 g_return_val_if_fail (G_IS_OUTPUT_STREAM (output_stream), NULL);
119 stream = MY_IO_STREAM (g_object_new (MY_TYPE_IO_STREAM, NULL));
120 stream->input_stream = g_object_ref (input_stream);
121 stream->output_stream = g_object_ref (output_stream);
122 return G_IO_STREAM (stream);
125 /* ---------- MySlowCloseOutputStream ------------------------------------ */
129 GFilterOutputStream parent_instance;
130 } MySlowCloseOutputStream;
134 GFilterOutputStreamClass parent_class;
135 } MySlowCloseOutputStreamClass;
137 #define MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM \
138 (my_slow_close_output_stream_get_type ())
139 #define MY_OUTPUT_STREAM(o) \
140 (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM, \
141 MySlowCloseOutputStream))
142 #define MY_IS_SLOW_CLOSE_OUTPUT_STREAM(o) \
143 (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM))
145 static GType my_slow_close_output_stream_get_type (void) G_GNUC_CONST;
147 G_DEFINE_TYPE (MySlowCloseOutputStream, my_slow_close_output_stream,
148 G_TYPE_FILTER_OUTPUT_STREAM)
151 my_slow_close_output_stream_init (MySlowCloseOutputStream *stream)
156 my_slow_close_output_stream_close (GOutputStream *stream,
157 GCancellable *cancellable,
160 g_usleep (CLOSE_TIME_MS * 1000);
161 return G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
162 close_fn (stream, cancellable, error);
166 GOutputStream *stream;
168 GCancellable *cancellable;
169 GAsyncReadyCallback callback;
174 delayed_close_free (gpointer data)
176 DelayedClose *df = data;
178 g_object_unref (df->stream);
179 g_object_unref (df->cancellable);
184 delayed_close_cb (gpointer data)
186 DelayedClose *df = data;
188 G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
189 close_async (df->stream, df->io_priority, df->cancellable, df->callback,
196 my_slow_close_output_stream_close_async (GOutputStream *stream,
198 GCancellable *cancellable,
199 GAsyncReadyCallback callback,
205 df = g_new0 (DelayedClose, 1);
206 df->stream = g_object_ref (stream);
207 df->io_priority = io_priority;
208 df->cancellable = (cancellable != NULL ? g_object_ref (cancellable) : NULL);
209 df->callback = callback;
210 df->user_data = user_data;
212 later = g_timeout_source_new (CLOSE_TIME_MS);
213 g_source_set_callback (later, delayed_close_cb, df, delayed_close_free);
214 g_source_attach (later, g_main_context_get_thread_default ());
218 my_slow_close_output_stream_close_finish (GOutputStream *stream,
219 GAsyncResult *result,
222 return G_OUTPUT_STREAM_CLASS (my_slow_close_output_stream_parent_class)->
223 close_finish (stream, result, error);
227 my_slow_close_output_stream_class_init (MySlowCloseOutputStreamClass *klass)
229 GOutputStreamClass *ostream_class;
231 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
232 ostream_class->close_fn = my_slow_close_output_stream_close;
233 ostream_class->close_async = my_slow_close_output_stream_close_async;
234 ostream_class->close_finish = my_slow_close_output_stream_close_finish;
238 my_io_stream_new_for_fds (gint fd_in, gint fd_out)
241 GInputStream *input_stream;
242 GOutputStream *real_output_stream;
243 GOutputStream *output_stream;
245 input_stream = g_unix_input_stream_new (fd_in, TRUE);
246 real_output_stream = g_unix_output_stream_new (fd_out, TRUE);
247 output_stream = g_object_new (MY_TYPE_SLOW_CLOSE_OUTPUT_STREAM,
248 "base-stream", real_output_stream,
250 stream = my_io_stream_new (input_stream, output_stream);
251 g_object_unref (input_stream);
252 g_object_unref (output_stream);
253 g_object_unref (real_output_stream);
257 /* ---------- Tests ------------------------------------------------------ */
260 gint server_to_client[2];
261 gint client_to_server[2];
262 GIOStream *server_iostream;
263 GDBusConnection *server_conn;
265 GDBusConnection *connection;
272 gconstpointer context)
274 f->guid = g_dbus_generate_guid ();
278 teardown (Fixture *f,
279 gconstpointer context)
281 g_clear_object (&f->server_iostream);
282 g_clear_object (&f->server_conn);
283 g_clear_object (&f->iostream);
284 g_clear_object (&f->connection);
285 g_clear_error (&f->error);
290 on_new_conn (GObject *source,
294 GDBusConnection **connection = user_data;
295 GError *error = NULL;
297 *connection = g_dbus_connection_new_for_address_finish (res, &error);
298 g_assert_no_error (error);
302 test_once (Fixture *f,
303 gconstpointer context)
305 GDBusMessage *message;
308 pipe_res = g_unix_open_pipe (f->server_to_client, FD_CLOEXEC, &f->error);
310 pipe_res = g_unix_open_pipe (f->client_to_server, FD_CLOEXEC, &f->error);
313 f->server_iostream = my_io_stream_new_for_fds (f->client_to_server[0],
314 f->server_to_client[1]);
315 f->iostream = my_io_stream_new_for_fds (f->server_to_client[0],
316 f->client_to_server[1]);
318 g_dbus_connection_new (f->server_iostream,
320 (G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
321 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS),
322 NULL /* auth observer */,
323 NULL /* cancellable */,
324 on_new_conn, &f->server_conn);
326 g_dbus_connection_new (f->iostream,
328 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
329 NULL /* auth observer */,
330 NULL /* cancellable */,
331 on_new_conn, &f->connection);
333 while (f->server_conn == NULL || f->connection == NULL)
334 g_main_context_iteration (NULL, TRUE);
337 * queue a message - it'll sometimes be sent while the close is pending,
340 message = g_dbus_message_new_signal ("/", "com.example.Foo", "Bar");
341 g_dbus_connection_send_message (f->connection, message, 0, NULL, &f->error);
342 g_assert_no_error (f->error);
343 g_object_unref (message);
345 /* close the connection (deliberately or via last-unref) */
346 if (g_strcmp0 (context, "unref") == 0)
348 g_clear_object (&f->connection);
352 g_dbus_connection_close_sync (f->connection, NULL, &f->error);
353 g_assert_no_error (f->error);
356 /* either way, wait for the connection to close */
357 while (!g_dbus_connection_is_closed (f->server_conn))
358 g_main_context_iteration (NULL, TRUE);
360 /* clean up before the next run */
361 g_clear_object (&f->iostream);
362 g_clear_object (&f->server_iostream);
363 g_clear_object (&f->connection);
364 g_clear_object (&f->server_conn);
365 g_clear_error (&f->error);
369 test_many_times (Fixture *f,
370 gconstpointer context)
375 n_repeats = N_REPEATS_SLOW;
377 n_repeats = N_REPEATS;
379 for (i = 0; i < n_repeats; i++)
380 test_once (f, context);
387 g_test_init (&argc, &argv, NULL);
389 g_test_add ("/gdbus/close-pending", Fixture, "close",
390 setup, test_many_times, teardown);
391 g_test_add ("/gdbus/unref-pending", Fixture, "unref",
392 setup, test_many_times, teardown);