Skip to content

Commit 368c1ce

Browse files
authoredOct 3, 2016
Merge pull request #3563 from elpaso/test-wait-for-network2
Wait for server ready (and times out) before starting the tests
2 parents 4fd5c01 + 28f547e commit 368c1ce

File tree

6 files changed

+41
-18
lines changed

6 files changed

+41
-18
lines changed
 

‎ci/travis/linux/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ export CCACHE_TEMPDIR=/tmp
2121

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

24-
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
24+
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
2525
# xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest" -S ./qgis-test-travis.ctest --output-on-failure

‎tests/src/python/qgis_wrapped_server.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
QGIS Server HTTP wrapper
44
55
This script launches a QGIS Server listening on port 8081 or on the port
6-
specified on the environment variable QGIS_SERVER_DEFAULT_PORT
6+
specified on the environment variable QGIS_SERVER_PORT
7+
QGIS_SERVER_HOST (defaults to 127.0.0.1)
78
89
For testing purposes, HTTP Basic can be enabled by setting the following
910
environment variables:
@@ -36,10 +37,8 @@
3637
from qgis.core import QgsApplication
3738
from qgis.server import QgsServer
3839

39-
try:
40-
QGIS_SERVER_DEFAULT_PORT = int(os.environ['QGIS_SERVER_DEFAULT_PORT'])
41-
except KeyError:
42-
QGIS_SERVER_DEFAULT_PORT = 8081
40+
QGIS_SERVER_PORT = int(os.environ.get('QGIS_SERVER_PORT', '8081'))
41+
QGIS_SERVER_HOST = os.environ.get('QGIS_SERVER_HOST', '127.0.0.1')
4342

4443
qgs_app = QgsApplication([], False)
4544
qgs_server = QgsServer()
@@ -101,9 +100,9 @@ def do_POST(self):
101100

102101

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

108107
def signal_handler(signal, frame):
109108
global qgs_app

‎tests/src/python/test_authmanager_endpoint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from urllib.parse import quote
3434
from shutil import rmtree
3535

36-
from utilities import unitTestDataPath
36+
from utilities import unitTestDataPath, waitServer
3737
from qgis.core import (
3838
QgsAuthManager,
3939
QgsAuthMethodConfig,
@@ -92,12 +92,12 @@ def setUpClass(cls):
9292
os.environ['QGIS_SERVER_HTTP_BASIC_AUTH'] = '1'
9393
os.environ['QGIS_SERVER_USERNAME'] = cls.username
9494
os.environ['QGIS_SERVER_PASSWORD'] = cls.password
95-
os.environ['QGIS_SERVER_DEFAULT_PORT'] = str(cls.port)
95+
os.environ['QGIS_SERVER_PORT'] = str(cls.port)
9696
server_path = os.path.dirname(os.path.realpath(__file__)) + \
9797
'/qgis_wrapped_server.py'
9898
cls.server = subprocess.Popen([sys.executable, server_path],
9999
env=os.environ)
100-
sleep(2)
100+
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
101101

102102
@classmethod
103103
def tearDownClass(cls):

‎tests/src/python/test_offline_editing_wfs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from shutil import copytree, rmtree
3636
import tempfile
3737
from time import sleep
38-
from utilities import unitTestDataPath
38+
from utilities import unitTestDataPath, waitServer
3939
from qgis.core import QgsVectorLayer
4040

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

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

105105
def tearDown(self):

‎tests/src/python/test_qgsserver_wfst.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from shutil import copytree, rmtree
4242
import tempfile
4343
from time import sleep
44-
from utilities import unitTestDataPath
44+
from utilities import unitTestDataPath, waitServer
4545
from qgis.core import (
4646
QgsVectorLayer,
4747
QgsFeature,
@@ -94,12 +94,12 @@ def setUpClass(cls):
9494
# Clear all test layers
9595
for ln in ['test_point', 'test_polygon', 'test_linestring']:
9696
cls._clearLayer(ln)
97-
os.environ['QGIS_SERVER_DEFAULT_PORT'] = str(cls.port)
97+
os.environ['QGIS_SERVER_PORT'] = str(cls.port)
9898
server_path = os.path.dirname(os.path.realpath(__file__)) + \
9999
'/qgis_wrapped_server.py'
100100
cls.server = subprocess.Popen([sys.executable, server_path],
101101
env=os.environ)
102-
sleep(2)
102+
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
103103

104104
@classmethod
105105
def tearDownClass(cls):

‎tests/src/python/utilities.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import glob
1919
import platform
2020
import tempfile
21+
try:
22+
from urllib2 import urlopen, HTTPError
23+
except ImportError:
24+
from urllib.request import urlopen, HTTPError
25+
2126

2227
from qgis.PyQt.QtCore import QDir
2328

@@ -828,3 +833,22 @@ def memberIsDocumented(self, member_elem):
828833
if doc is not None and list(doc):
829834
return True
830835
return False
836+
837+
838+
def waitServer(url, timeout=10):
839+
""" Wait for a server to be online and to respond
840+
HTTP errors are ignored
841+
@param timeout: in seconds
842+
@return: True of False
843+
"""
844+
from time import time as now
845+
end = now() + timeout
846+
while True:
847+
try:
848+
urlopen(url, timeout=1)
849+
return True
850+
except HTTPError:
851+
return True
852+
except Exception as e:
853+
if now() > end:
854+
return False

0 commit comments

Comments
 (0)
Please sign in to comment.