tests: Add gsocketclient test for slow connections

This commit is contained in:
Patrick Griffis 2018-11-16 13:54:07 -05:00 committed by Patrick Griffis
parent 4b29e55097
commit 8b9e88937b
3 changed files with 137 additions and 1 deletions

View File

@ -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 ();
}

View File

@ -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' : {},
}
@ -528,12 +540,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

View File

@ -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);
}