Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3563 from elpaso/test-wait-for-network2
Wait for server ready (and times out) before starting the tests
  • Loading branch information
elpaso committed Oct 3, 2016
2 parents 4fd5c01 + 28f547e commit 368c1ce
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ci/travis/linux/script.sh
Expand Up @@ -21,5 +21,5 @@ export CCACHE_TEMPDIR=/tmp

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest|PyQgsWFSProviderGUI|qgis_ziplayertest|qgis_ogcutilstest|PyQgsServerWFST|$(cat ${DIR}/blacklist.txt | paste -sd '|' -)" -S ./qgis-test-travis.ctest --output-on-failure
xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest|PyQgsWFSProviderGUI|qgis_ziplayertest|qgis_ogcutilstest|$(cat ${DIR}/blacklist.txt | paste -sd '|' -)" -S ./qgis-test-travis.ctest --output-on-failure
# xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest" -S ./qgis-test-travis.ctest --output-on-failure
15 changes: 7 additions & 8 deletions tests/src/python/qgis_wrapped_server.py
Expand Up @@ -3,7 +3,8 @@
QGIS Server HTTP wrapper
This script launches a QGIS Server listening on port 8081 or on the port
specified on the environment variable QGIS_SERVER_DEFAULT_PORT
specified on the environment variable QGIS_SERVER_PORT
QGIS_SERVER_HOST (defaults to 127.0.0.1)
For testing purposes, HTTP Basic can be enabled by setting the following
environment variables:
Expand Down Expand Up @@ -36,10 +37,8 @@
from qgis.core import QgsApplication
from qgis.server import QgsServer

try:
QGIS_SERVER_DEFAULT_PORT = int(os.environ['QGIS_SERVER_DEFAULT_PORT'])
except KeyError:
QGIS_SERVER_DEFAULT_PORT = 8081
QGIS_SERVER_PORT = int(os.environ.get('QGIS_SERVER_PORT', '8081'))
QGIS_SERVER_HOST = os.environ.get('QGIS_SERVER_HOST', '127.0.0.1')

qgs_app = QgsApplication([], False)
qgs_server = QgsServer()
Expand Down Expand Up @@ -101,9 +100,9 @@ def do_POST(self):


if __name__ == '__main__':
server = HTTPServer(('localhost', QGIS_SERVER_DEFAULT_PORT), Handler)
print('Starting server on localhost:%s, use <Ctrl-C> to stop' %
QGIS_SERVER_DEFAULT_PORT)
print('Starting server on %s:%s, use <Ctrl-C> to stop' %
(QGIS_SERVER_HOST, QGIS_SERVER_PORT))
server = HTTPServer((QGIS_SERVER_HOST, QGIS_SERVER_PORT), Handler)

def signal_handler(signal, frame):
global qgs_app
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_authmanager_endpoint.py
Expand Up @@ -33,7 +33,7 @@
from urllib.parse import quote
from shutil import rmtree

from utilities import unitTestDataPath
from utilities import unitTestDataPath, waitServer
from qgis.core import (
QgsAuthManager,
QgsAuthMethodConfig,
Expand Down Expand Up @@ -92,12 +92,12 @@ def setUpClass(cls):
os.environ['QGIS_SERVER_HTTP_BASIC_AUTH'] = '1'
os.environ['QGIS_SERVER_USERNAME'] = cls.username
os.environ['QGIS_SERVER_PASSWORD'] = cls.password
os.environ['QGIS_SERVER_DEFAULT_PORT'] = str(cls.port)
os.environ['QGIS_SERVER_PORT'] = str(cls.port)
server_path = os.path.dirname(os.path.realpath(__file__)) + \
'/qgis_wrapped_server.py'
cls.server = subprocess.Popen([sys.executable, server_path],
env=os.environ)
sleep(2)
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"

@classmethod
def tearDownClass(cls):
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_offline_editing_wfs.py
Expand Up @@ -35,7 +35,7 @@
from shutil import copytree, rmtree
import tempfile
from time import sleep
from utilities import unitTestDataPath
from utilities import unitTestDataPath, waitServer
from qgis.core import QgsVectorLayer

from qgis.testing import (
Expand Down Expand Up @@ -85,7 +85,7 @@ def setUpClass(cls):
pass
# Clear all test layers
cls._clearLayer(cls._getLayer('test_point'))
os.environ['QGIS_SERVER_DEFAULT_PORT'] = str(cls.port)
os.environ['QGIS_SERVER_PORT'] = str(cls.port)
cls.server_path = os.path.dirname(os.path.realpath(__file__)) + \
'/qgis_wrapped_server.py'

Expand All @@ -99,7 +99,7 @@ def setUp(self):
self.server = subprocess.Popen([sys.executable, self.server_path],
env=os.environ)
# Wait for the server process to start
sleep(2)
assert waitServer('http://127.0.0.1:%s' % self.port), "Server is not responding!"
self._setUp()

def tearDown(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgsserver_wfst.py
Expand Up @@ -41,7 +41,7 @@
from shutil import copytree, rmtree
import tempfile
from time import sleep
from utilities import unitTestDataPath
from utilities import unitTestDataPath, waitServer
from qgis.core import (
QgsVectorLayer,
QgsFeature,
Expand Down Expand Up @@ -94,12 +94,12 @@ def setUpClass(cls):
# Clear all test layers
for ln in ['test_point', 'test_polygon', 'test_linestring']:
cls._clearLayer(ln)
os.environ['QGIS_SERVER_DEFAULT_PORT'] = str(cls.port)
os.environ['QGIS_SERVER_PORT'] = str(cls.port)
server_path = os.path.dirname(os.path.realpath(__file__)) + \
'/qgis_wrapped_server.py'
cls.server = subprocess.Popen([sys.executable, server_path],
env=os.environ)
sleep(2)
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"

@classmethod
def tearDownClass(cls):
Expand Down
24 changes: 24 additions & 0 deletions tests/src/python/utilities.py
Expand Up @@ -18,6 +18,11 @@
import glob
import platform
import tempfile
try:
from urllib2 import urlopen, HTTPError
except ImportError:
from urllib.request import urlopen, HTTPError


from qgis.PyQt.QtCore import QDir

Expand Down Expand Up @@ -828,3 +833,22 @@ def memberIsDocumented(self, member_elem):
if doc is not None and list(doc):
return True
return False


def waitServer(url, timeout=10):
""" Wait for a server to be online and to respond
HTTP errors are ignored
@param timeout: in seconds
@return: True of False
"""
from time import time as now
end = now() + timeout
while True:
try:
urlopen(url, timeout=1)
return True
except HTTPError:
return True
except Exception as e:
if now() > end:
return False

0 comments on commit 368c1ce

Please sign in to comment.