tests: fixed racy behavior in rtspserver tests

https://bugzilla.gnome.org/show_bug.cgi?id=710078
This commit is contained in:
Patricia Muscalu 2013-10-14 08:30:33 +02:00 committed by Sebastian Dröge
parent cf82d90452
commit de7be1c9b2
2 changed files with 39 additions and 42 deletions

View file

@ -842,6 +842,29 @@ gst_rtsp_server_create_socket (GstRTSPServer * server,
}
if (g_socket_bind (socket, sockaddr, TRUE, bind_error ? NULL : &bind_error)) {
/* ask what port the socket has been bound to */
if (port == 0 || !strcmp (priv->service, "0")) {
GError *addr_error = NULL;
g_object_unref (sockaddr);
sockaddr = g_socket_get_local_address (socket, &addr_error);
if (addr_error != NULL) {
GST_DEBUG_OBJECT (server,
"failed to get the local address of a bound socket %s",
addr_error->message);
g_clear_error (&addr_error);
break;
}
port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
if (port != 0) {
g_free (priv->service);
priv->service = g_strdup_printf ("%d", port);
} else {
GST_DEBUG_OBJECT (server, "failed to get the port of a bound socket");
}
}
g_object_unref (sockaddr);
break;
}

View file

@ -61,38 +61,6 @@ iterate (void)
}
}
/* returns an unused port that can be used by the test */
static int
get_unused_port (gint type)
{
int sock;
struct sockaddr_in addr;
socklen_t addr_len;
gint port;
/* create socket */
fail_unless ((sock = socket (AF_INET, type, 0)) > 0);
/* pass port 0 to bind, which will bind to any free port */
memset (&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons (0);
fail_unless (bind (sock, (struct sockaddr *) &addr, sizeof addr) == 0);
/* ask what port was bound using getsockname */
addr_len = sizeof addr;
memset (&addr, 0, addr_len);
fail_unless (getsockname (sock, (struct sockaddr *) &addr, &addr_len) == 0);
port = ntohs (addr.sin_port);
/* close the socket so the port gets unbound again (and can be used by the
* test) */
close (sock);
return port;
}
static void
get_client_ports_full (GstRTSPRange * range, GSocket ** rtp_socket,
GSocket ** rtcp_socket)
@ -197,16 +165,19 @@ start_server (void)
gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
g_object_unref (mounts);
/* set port */
test_port = get_unused_port (SOCK_STREAM);
service = g_strdup_printf ("%d", test_port);
gst_rtsp_server_set_service (server, service);
g_free (service);
/* set port to any */
gst_rtsp_server_set_service (server, "0");
/* attach to default main context */
source_id = gst_rtsp_server_attach (server, NULL);
fail_if (source_id == 0);
/* get port */
service = gst_rtsp_server_get_service (server);
test_port = atoi (service);
fail_unless (test_port != 0);
g_free (service);
GST_DEBUG ("rtsp server listening on port %d", test_port);
}
@ -1252,16 +1223,19 @@ GST_START_TEST (test_play_specific_server_port)
gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
g_object_unref (mounts);
/* set port */
test_port = get_unused_port (SOCK_STREAM);
service = g_strdup_printf ("%d", test_port);
gst_rtsp_server_set_service (server, service);
g_free (service);
/* set port to any */
gst_rtsp_server_set_service (server, "0");
/* attach to default main context */
source_id = gst_rtsp_server_attach (server, NULL);
fail_if (source_id == 0);
/* get port */
service = gst_rtsp_server_get_service (server);
test_port = atoi (service);
fail_unless (test_port != 0);
g_free (service);
GST_DEBUG ("rtsp server listening on port %d", test_port);