1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2018 Igalia S.L.
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.1 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, see <http://www.gnu.org/licenses/>.
22 on_connected (GObject *source_object,
26 GSocketConnection *conn;
29 conn = g_socket_client_connect_to_uri_finish (G_SOCKET_CLIENT (source_object), result, &error);
30 g_assert_no_error (error);
32 g_object_unref (conn);
33 g_main_loop_quit (user_data);
37 test_happy_eyeballs (void)
39 GSocketClient *client;
40 GSocketService *service;
45 loop = g_main_loop_new (NULL, FALSE);
47 service = g_socket_service_new ();
48 port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
49 g_assert_no_error (error);
50 g_socket_service_start (service);
52 /* All of the magic here actually happens in slow-connect-preload.c
53 * which as you would guess is preloaded. So this is just making a
54 * normal connection that happens to take 600ms each time. This will
55 * trigger the logic to make multiple parallel connections.
57 client = g_socket_client_new ();
58 g_socket_client_connect_to_host_async (client, "localhost", port, NULL, on_connected, loop);
59 g_main_loop_run (loop);
61 g_main_loop_unref (loop);
62 g_object_unref (service);
63 g_object_unref (client);
67 on_connected_cancelled (GObject *source_object,
71 GSocketConnection *conn;
74 conn = g_socket_client_connect_to_uri_finish (G_SOCKET_CLIENT (source_object), result, &error);
75 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
79 g_main_loop_quit (user_data);
83 on_timer (GCancellable *cancel)
85 g_cancellable_cancel (cancel);
86 return G_SOURCE_REMOVE;
90 on_event (GSocketClient *client,
91 GSocketClientEvent event,
92 GSocketConnectable *connectable,
93 GIOStream *connection,
94 gboolean *got_completed_event)
96 if (event == G_SOCKET_CLIENT_COMPLETE)
98 *got_completed_event = TRUE;
99 g_assert_null (connection);
104 test_happy_eyeballs_cancel_delayed (void)
106 GSocketClient *client;
107 GSocketService *service;
108 GError *error = NULL;
111 GCancellable *cancel;
112 gboolean got_completed_event = FALSE;
114 /* This just tests that cancellation works as expected, still emits the completed signal,
115 * and never returns a connection */
117 loop = g_main_loop_new (NULL, FALSE);
119 service = g_socket_service_new ();
120 port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
121 g_assert_no_error (error);
122 g_socket_service_start (service);
124 client = g_socket_client_new ();
125 cancel = g_cancellable_new ();
126 g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
127 g_timeout_add (1, (GSourceFunc) on_timer, cancel);
128 g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
129 g_main_loop_run (loop);
131 g_assert_true (got_completed_event);
132 g_main_loop_unref (loop);
133 g_object_unref (service);
134 g_object_unref (client);
135 g_object_unref (cancel);
139 test_happy_eyeballs_cancel_instant (void)
141 GSocketClient *client;
142 GSocketService *service;
143 GError *error = NULL;
146 GCancellable *cancel;
147 gboolean got_completed_event = FALSE;
149 /* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but
150 * with different timing since it sends an already cancelled cancellable */
152 loop = g_main_loop_new (NULL, FALSE);
154 service = g_socket_service_new ();
155 port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
156 g_assert_no_error (error);
157 g_socket_service_start (service);
159 client = g_socket_client_new ();
160 cancel = g_cancellable_new ();
161 g_cancellable_cancel (cancel);
162 g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
163 g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
164 g_main_loop_run (loop);
166 g_assert_true (got_completed_event);
167 g_main_loop_unref (loop);
168 g_object_unref (service);
169 g_object_unref (client);
170 g_object_unref (cancel);
174 main (int argc, char *argv[])
176 g_test_init (&argc, &argv, NULL);
178 g_test_add_func ("/socket-client/happy-eyeballs/slow", test_happy_eyeballs);
179 g_test_add_func ("/socket-client/happy-eyeballs/cancellation/instant", test_happy_eyeballs_cancel_instant);
180 g_test_add_func ("/socket-client/happy-eyeballs/cancellation/delayed", test_happy_eyeballs_cancel_delayed);
183 return g_test_run ();