1 /* GLib testing framework examples and tests
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include <gio/gunixinputstream.h>
25 #include <gio/gunixoutputstream.h>
29 GPollableInputStream *in;
33 poll_source_callback (GPollableInputStream *in,
39 gboolean *success = user_data;
41 nread = g_pollable_input_stream_read_nonblocking (in, buf, 2, NULL, &error);
42 g_assert_no_error (error);
43 g_assert_cmpint (nread, ==, 2);
44 g_assert_cmpstr (buf, ==, "x");
51 check_source_readability_callback (gpointer user_data)
53 gboolean expected = GPOINTER_TO_INT (user_data);
56 readable = g_pollable_input_stream_is_readable (in);
57 g_assert_cmpint (readable, ==, expected);
62 write_callback (gpointer user_data)
68 nwrote = g_output_stream_write (out, buf, 2, NULL, &error);
69 g_assert_no_error (error);
70 g_assert_cmpint (nwrote, ==, 2);
72 check_source_readability_callback (GINT_TO_POINTER (TRUE));
78 check_source_and_quit_callback (gpointer user_data)
80 check_source_readability_callback (user_data);
81 g_main_loop_quit (loop);
93 gboolean success = FALSE;
95 readable = g_pollable_input_stream_is_readable (in);
98 nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error);
99 g_assert_cmpint (nread, ==, -1);
100 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
101 g_clear_error (&error);
103 /* Create 4 sources, in decreasing order of priority:
104 * 1. poll source on @in
105 * 2. idle source that checks if @in is readable once
106 * (it won't be) and then removes itself
107 * 3. idle source that writes a byte to @out, checks that
108 * @in is now readable, and removes itself
109 * 4. idle source that checks if @in is readable once
110 * (it won't be, since the poll source will fire before
111 * this one does) and then quits the loop.
113 * If the poll source triggers before it should, then it will get a
114 * %G_IO_ERROR_WOULD_BLOCK, and if check() fails in either
115 * direction, we will catch it at some point.
118 poll_source = g_pollable_input_stream_create_source (in, NULL);
119 g_source_set_priority (poll_source, 1);
120 g_source_set_callback (poll_source, (GSourceFunc) poll_source_callback, &success, NULL);
121 g_source_attach (poll_source, NULL);
122 g_source_unref (poll_source);
124 g_idle_add_full (2, check_source_readability_callback, GINT_TO_POINTER (FALSE), NULL);
125 g_idle_add_full (3, write_callback, NULL, NULL);
126 g_idle_add_full (4, check_source_and_quit_callback, GINT_TO_POINTER (FALSE), NULL);
128 loop = g_main_loop_new (NULL, FALSE);
129 g_main_loop_run (loop);
130 g_main_loop_unref (loop);
132 g_assert_cmpint (success, ==, TRUE);
137 test_pollable_unix (void)
139 int pipefds[2], status;
141 status = pipe (pipefds);
142 g_assert_cmpint (status, ==, 0);
144 in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
145 out = g_unix_output_stream_new (pipefds[1], TRUE);
150 g_object_unref (out);
155 client_connected (GObject *source,
156 GAsyncResult *result,
159 GSocketClient *client = G_SOCKET_CLIENT (source);
160 GSocketConnection **conn = user_data;
161 GError *error = NULL;
163 *conn = g_socket_client_connect_finish (client, result, &error);
164 g_assert_no_error (error);
168 server_connected (GObject *source,
169 GAsyncResult *result,
172 GSocketListener *listener = G_SOCKET_LISTENER (source);
173 GSocketConnection **conn = user_data;
174 GError *error = NULL;
176 *conn = g_socket_listener_accept_finish (listener, result, NULL, &error);
177 g_assert_no_error (error);
181 test_pollable_socket (void)
184 GSocketAddress *saddr, *effective_address;
185 GSocketListener *listener;
186 GSocketClient *client;
187 GError *error = NULL;
188 GSocketConnection *client_conn = NULL, *server_conn = NULL;
190 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
191 saddr = g_inet_socket_address_new (iaddr, 0);
192 g_object_unref (iaddr);
194 listener = g_socket_listener_new ();
195 g_socket_listener_add_address (listener, saddr,
196 G_SOCKET_TYPE_STREAM,
197 G_SOCKET_PROTOCOL_TCP,
201 g_assert_no_error (error);
202 g_object_unref (saddr);
204 client = g_socket_client_new ();
206 g_socket_client_connect_async (client,
207 G_SOCKET_CONNECTABLE (effective_address),
208 NULL, client_connected, &client_conn);
209 g_socket_listener_accept_async (listener, NULL,
210 server_connected, &server_conn);
212 while (!client_conn || !server_conn)
213 g_main_context_iteration (NULL, TRUE);
215 in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn)));
216 out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn));
220 g_object_unref (client_conn);
221 g_object_unref (server_conn);
222 g_object_unref (client);
223 g_object_unref (listener);
231 g_test_init (&argc, &argv, NULL);
234 g_test_add_func ("/pollable/unix", test_pollable_unix);
236 g_test_add_func ("/pollable/socket", test_pollable_socket);