• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2018 Igalia S.L.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 #include <gio/gio.h>
20 
21 static void
on_connected(GObject * source_object,GAsyncResult * result,gpointer user_data)22 on_connected (GObject      *source_object,
23               GAsyncResult *result,
24               gpointer      user_data)
25 {
26   GSocketConnection *conn;
27   GError *error = NULL;
28 
29   conn = g_socket_client_connect_to_uri_finish (G_SOCKET_CLIENT (source_object), result, &error);
30   g_assert_no_error (error);
31 
32   g_object_unref (conn);
33   g_main_loop_quit (user_data);
34 }
35 
36 static void
test_happy_eyeballs(void)37 test_happy_eyeballs (void)
38 {
39   GSocketClient *client;
40   GSocketService *service;
41   GError *error = NULL;
42   guint16 port;
43   GMainLoop *loop;
44 
45   loop = g_main_loop_new (NULL, FALSE);
46 
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);
51 
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.
56    */
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);
60 
61   g_main_loop_unref (loop);
62   g_object_unref (service);
63   g_object_unref (client);
64 }
65 
66 static void
on_connected_cancelled(GObject * source_object,GAsyncResult * result,gpointer user_data)67 on_connected_cancelled (GObject      *source_object,
68                         GAsyncResult *result,
69                         gpointer      user_data)
70 {
71   GSocketConnection *conn;
72   GError *error = NULL;
73 
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);
76   g_assert_null (conn);
77 
78   g_error_free (error);
79   g_main_loop_quit (user_data);
80 }
81 
82 static int
on_timer(GCancellable * cancel)83 on_timer (GCancellable *cancel)
84 {
85   g_cancellable_cancel (cancel);
86   return G_SOURCE_REMOVE;
87 }
88 
89 static void
on_event(GSocketClient * client,GSocketClientEvent event,GSocketConnectable * connectable,GIOStream * connection,gboolean * got_completed_event)90 on_event (GSocketClient      *client,
91           GSocketClientEvent  event,
92           GSocketConnectable *connectable,
93           GIOStream          *connection,
94           gboolean           *got_completed_event)
95 {
96   if (event == G_SOCKET_CLIENT_COMPLETE)
97     {
98       *got_completed_event = TRUE;
99       g_assert_null (connection);
100     }
101 }
102 
103 static void
test_happy_eyeballs_cancel_delayed(void)104 test_happy_eyeballs_cancel_delayed (void)
105 {
106   GSocketClient *client;
107   GSocketService *service;
108   GError *error = NULL;
109   guint16 port;
110   GMainLoop *loop;
111   GCancellable *cancel;
112   gboolean got_completed_event = FALSE;
113 
114   /* This just tests that cancellation works as expected, still emits the completed signal,
115    * and never returns a connection */
116 
117   loop = g_main_loop_new (NULL, FALSE);
118 
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);
123 
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);
130 
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);
136 }
137 
138 static void
test_happy_eyeballs_cancel_instant(void)139 test_happy_eyeballs_cancel_instant (void)
140 {
141   GSocketClient *client;
142   GSocketService *service;
143   GError *error = NULL;
144   guint16 port;
145   GMainLoop *loop;
146   GCancellable *cancel;
147   gboolean got_completed_event = FALSE;
148 
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 */
151 
152   loop = g_main_loop_new (NULL, FALSE);
153 
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);
158 
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);
165 
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);
171 }
172 
173 int
main(int argc,char * argv[])174 main (int argc, char *argv[])
175 {
176   g_test_init (&argc, &argv, NULL);
177 
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);
181 
182 
183   return g_test_run ();
184 }