From 59f1b395868e36602e561d099795e7163a7873fb Mon Sep 17 00:00:00 2001
From: Justin Kim <justin.kim@collabora.com>
Date: Fri, 26 Aug 2016 23:35:52 +0900
Subject: [PATCH] gstreamer-buffering: Pick up a running port by simple-httpd

Even if the port is already in use, simple-httpd doesn't report and
just show a message "Waiting for requests...". It's a simple bug of
example of libsoup2.4.
Fortunately, without a specific port, simple-httpd will pick up
a port which is available. Then, we can parse its stdout to parse
running port.

Signed-off-by: Justin Kim <justin.kim@collabora.com>
Reviewed-by: Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>
Differential Revision: https://phabricator.apertis.org/D4080
---
 gstreamer-buffering/automated/run-test.sh | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/gstreamer-buffering/automated/run-test.sh b/gstreamer-buffering/automated/run-test.sh
index 361e40f..4890972 100755
--- a/gstreamer-buffering/automated/run-test.sh
+++ b/gstreamer-buffering/automated/run-test.sh
@@ -12,6 +12,9 @@ VIDEO_PLAYER="${VIDEO_PLAYER:-${TESTLIBDIR}/video-player}"
 # Running HTTP server used for network cgroup test
 HTTP_SERVER_PID=""
 
+# Port number for local HTTP server
+HTTP_SERVER_PORT=""
+
 # List of processes to kill on exit
 PROC_KILL_LIST=()
 
@@ -41,12 +44,18 @@ _setup_qdiscs() {
 }
 
 _setup_http_server() {
-    local port=$1 www_root=$2
+    local www_root=$1
 
+    TMPLOGFILE=`mktemp`
     cd "${www_root}" || setup_failure
-    /usr/lib/libsoup2.4/simple-httpd -p ${port} &
+    /usr/lib/libsoup2.4/simple-httpd > ${TMPLOGFILE} &
     # Running HTTP servers used for network cgroup test
     HTTP_SERVER_PID=$!
+
+    HTTP_SERVER_PORT=$(grep -m1 "Listening" <( tail -f ${TMPLOGFILE} --pid ${HTTP_SERVER_PID}) | cut -d ':' -f 3 | sed "s/\///g")
+
+    # Remove a temporary file
+    rm -f ${TMPLOGFILE}
 }
 
 _wipe_qdiscs() {
@@ -83,8 +92,8 @@ setup_success
 # Execute #
 ###########
 test_gstreamer_download_buffering() {
-    local ret server_pid port="9000"
-    local media_file="http://localhost:${port}/trailer_400p.ogg"
+    local ret server_pid
+    local media_file
 
     ## Setup ##
     _setup_qdiscs
@@ -92,10 +101,8 @@ test_gstreamer_download_buffering() {
     echo "Test setup complete, beginning test..."
 
     ## Test ##
-    _setup_http_server ${port} "${TEST_MEDIA_DIR}"
+    _setup_http_server "${TEST_MEDIA_DIR}"
     ret=$?
-    # Wait for server to start up
-    _sleep 3
 
     # Check it is running
     if ! kill -0 ${HTTP_SERVER_PID}; then
@@ -108,6 +115,7 @@ test_gstreamer_download_buffering() {
         return $ret
     fi
 
+    media_file="http://localhost:${HTTP_SERVER_PORT}/trailer_400p.ogg"
     cd "${TESTLIBDIR}"
     ${VIDEO_PLAYER} "${media_file}"
     ret=$?
-- 
GitLab