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);
84 GCancellable *cancellable;
89 on_event (GSocketClient *client,
90 GSocketClientEvent event,
91 GSocketConnectable *connectable,
92 GIOStream *connection,
93 EventCallbackData *data)
95 if (data->cancellable && event == G_SOCKET_CLIENT_CONNECTED)
97 g_cancellable_cancel (data->cancellable);
99 else if (event == G_SOCKET_CLIENT_COMPLETE)
101 data->completed = TRUE;
102 g_assert_null (connection);
107 test_happy_eyeballs_cancel_delayed (void)
109 GSocketClient *client;
110 GSocketService *service;
111 GError *error = NULL;
114 EventCallbackData data = { NULL, FALSE };
116 /* This just tests that cancellation works as expected, still emits the completed signal,
117 * and never returns a connection */
119 loop = g_main_loop_new (NULL, FALSE);
121 service = g_socket_service_new ();
122 port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
123 g_assert_no_error (error);
124 g_socket_service_start (service);
126 client = g_socket_client_new ();
127 data.cancellable = g_cancellable_new ();
128 g_socket_client_connect_to_host_async (client, "localhost", port, data.cancellable, on_connected_cancelled, loop);
129 g_signal_connect (client, "event", G_CALLBACK (on_event), &data);
130 g_main_loop_run (loop);
132 g_assert_true (data.completed);
133 g_main_loop_unref (loop);
134 g_object_unref (service);
135 g_object_unref (client);
136 g_object_unref (data.cancellable);
140 test_happy_eyeballs_cancel_instant (void)
142 GSocketClient *client;
143 GSocketService *service;
144 GError *error = NULL;
147 GCancellable *cancel;
148 EventCallbackData data = { NULL, FALSE };
150 /* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but
151 * with different timing since it sends an already cancelled cancellable */
153 loop = g_main_loop_new (NULL, FALSE);
155 service = g_socket_service_new ();
156 port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
157 g_assert_no_error (error);
158 g_socket_service_start (service);
160 client = g_socket_client_new ();
161 cancel = g_cancellable_new ();
162 g_cancellable_cancel (cancel);
163 g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
164 g_signal_connect (client, "event", G_CALLBACK (on_event), &data);
165 g_main_loop_run (loop);
167 g_assert_true (data.completed);
168 g_main_loop_unref (loop);
169 g_object_unref (service);
170 g_object_unref (client);
171 g_object_unref (cancel);
175 main (int argc, char *argv[])
177 g_test_init (&argc, &argv, NULL);
179 g_test_add_func ("/socket-client/happy-eyeballs/slow", test_happy_eyeballs);
180 g_test_add_func ("/socket-client/happy-eyeballs/cancellation/instant", test_happy_eyeballs_cancel_instant);
181 g_test_add_func ("/socket-client/happy-eyeballs/cancellation/delayed", test_happy_eyeballs_cancel_delayed);
184 return g_test_run ();