[glib/wip/tingping/happy-eyeballs: 3/3] tests: Add gsocketclient test for slow connections



commit f5785492d630436148cf043294bba9b72fbb32fa
Author: Patrick Griffis <tingping tingping se>
Date:   Fri Nov 16 13:54:07 2018 -0500

    tests: Add gsocketclient test for slow connections

 gio/tests/gsocketclient-slow.c   | 74 ++++++++++++++++++++++++++++++++++++++++
 gio/tests/meson.build            | 21 +++++++++++-
 gio/tests/slow-connect-preload.c | 43 +++++++++++++++++++++++
 3 files changed, 137 insertions(+), 1 deletion(-)
---
diff --git a/gio/tests/gsocketclient-slow.c b/gio/tests/gsocketclient-slow.c
new file mode 100644
index 000000000..bc7d02772
--- /dev/null
+++ b/gio/tests/gsocketclient-slow.c
@@ -0,0 +1,74 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2018 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gio/gio.h>
+
+static void
+on_connected (GObject      *source_object,
+              GAsyncResult *result,
+              gpointer      user_data)
+{
+  GSocketConnection *conn;
+  GError *error = NULL;
+
+  conn = g_socket_client_connect_to_uri_finish (G_SOCKET_CLIENT (source_object), result, &error);
+  g_assert_no_error (error);
+
+  g_object_unref (conn);
+  g_main_loop_quit (user_data);
+}
+
+static void
+test_happy_eyeballs (void)
+{
+  GSocketClient *client;
+  GSocketService *service;
+  GError *error = NULL;
+  guint16 port;
+  GMainLoop *loop;
+
+  loop = g_main_loop_new (NULL, FALSE);
+
+  service = g_socket_service_new ();
+  port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
+  g_assert_no_error (error);
+  g_socket_service_start (service);
+
+  /* All of the magic here actually happens in slow-connect-preload.c
+   * which as you would guess is preloaded. So this is just making a
+   * normal connection that happens to take 600ms each time. This will
+   * trigger the logic to make multiple parallel connections.
+   */
+  client = g_socket_client_new ();
+  g_socket_client_connect_to_host_async (client, "localhost", port, NULL, on_connected, loop);
+  g_main_loop_run (loop);
+
+  g_main_loop_unref (loop);
+  g_object_unref (service);
+  g_object_unref (client);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/socket-client/happy-eyeballs", test_happy_eyeballs);
+
+  return g_test_run ();
+}
\ No newline at end of file
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index 14a59cd73..e665cb47b 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -134,6 +134,18 @@ if host_machine.system() != 'windows'
     'unix-mounts' : {},
     'unix-streams' : {},
     'g-file-info-filesystem-readonly' : {},
+    'gsocketclient-slow' : {
+      'depends' : [
+        shared_library('slow-connect-preload',
+          'slow-connect-preload.c',
+          name_prefix : '',
+          dependencies: cc.find_library('dl'),
+        )
+      ],
+      'env' : {
+        'LD_PRELOAD': '@0@/slow-connect-preload.so'.format(meson.current_build_dir())
+      }
+    },
     'gschema-compile' : {'install' : false},
     'trash' : {},
   }
@@ -495,12 +507,19 @@ foreach test_name, extra_args : gio_tests
 
   suite = ['gio'] + extra_args.get('suite', [])
   timeout = suite.contains('slow') ? test_timeout_slow : test_timeout
+  local_test_env = test_env
+
+  foreach var, value : extra_args.get('env', {})
+    local_test_env.append(var, value)
+  endforeach
+
   test(test_name, exe,
-    env : test_env,
+    env : local_test_env,
     timeout : timeout,
     suite : suite,
     args : ['--tap'],
     is_parallel : extra_args.get('is_parallel', true),
+    depends : extra_args.get('depends', []),
   )
 endforeach
 
diff --git a/gio/tests/slow-connect-preload.c b/gio/tests/slow-connect-preload.c
new file mode 100644
index 000000000..9fdb431c6
--- /dev/null
+++ b/gio/tests/slow-connect-preload.c
@@ -0,0 +1,43 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2018 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+/* This is used in gsocketclient-slow.c used to test
+ * and get coverage on how GSocketClient reacts to
+ * slow connections.
+ */
+int
+connect (int                    sockfd,
+         const struct sockaddr *addr,
+         socklen_t              addrlen)
+{
+  static int (*real_connect)(int, const struct sockaddr *, socklen_t);
+
+  if (real_connect == NULL)
+    real_connect = dlsym (RTLD_NEXT, "connect");
+
+  /* This is long enough for multiple connection attempts to be done
+   * in parallel given that their timeout is 250ms */
+  usleep (600 * 1000);
+  return real_connect (sockfd, addr, addrlen);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]