gsocket: Handle cancellation of connect() in non-blocking mode

As with the previous commit, but for `g_socket_connect()`, which is the
other cancellable use of `g_socket_condition_wait()` in the file.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall
2025-07-25 15:29:51 +01:00
parent e6a1eacfe1
commit 4e1fcbffbd
2 changed files with 47 additions and 0 deletions

View File

@@ -2578,6 +2578,49 @@ test_accept_cancelled (void)
g_clear_object (&socket);
}
static void
test_connect_cancelled (void)
{
GSocket *socket = NULL;
GError *local_error = NULL;
GCancellable *cancellable = NULL;
GInetAddress *inet_addr = NULL;
GSocketAddress *addr = NULL;
g_test_summary ("Calling g_socket_connect() with a cancelled cancellable "
"should return immediately regardless of whether the socket "
"is blocking");
socket = g_socket_new (G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
&local_error);
g_assert_no_error (local_error);
cancellable = g_cancellable_new ();
g_cancellable_cancel (cancellable);
inet_addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
addr = g_inet_socket_address_new (inet_addr, 0);
for (unsigned int i = 0; i < 2; i++)
{
gboolean retval;
g_socket_set_blocking (socket, i);
retval = g_socket_connect (socket, addr, cancellable, &local_error);
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (retval);
g_clear_error (&local_error);
}
g_clear_object (&addr);
g_clear_object (&inet_addr);
g_clear_object (&cancellable);
g_clear_object (&socket);
}
int
main (int argc,
char *argv[])
@@ -2648,6 +2691,7 @@ main (int argc,
g_test_add_func ("/socket/receive_bytes_from", test_receive_bytes_from);
g_test_add_func ("/socket/accept/cancelled", test_accept_cancelled);
g_test_add_func ("/socket/connect/cancelled", test_connect_cancelled);
return g_test_run();
}