Skip to content

Commit

Permalink
Merge pull request #3564 from elpaso/test-wait-for-network3
Browse files Browse the repository at this point in the history
Yet another strategy to get a free port from the server
  • Loading branch information
elpaso authored Oct 3, 2016
2 parents 368c1ce + 9761a86 commit d3f8763
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions tests/src/python/qgis_wrapped_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def do_POST(self):


if __name__ == '__main__':
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)
print('Starting server on %s:%s, use <Ctrl-C> to stop' %
(QGIS_SERVER_HOST, server.server_port), flush=True)

def signal_handler(signal, frame):
global qgs_app
Expand Down
21 changes: 12 additions & 9 deletions tests/src/python/test_authmanager_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""
import os
import sys
import re
import subprocess
import tempfile
import random
Expand All @@ -29,7 +30,6 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from time import sleep
from urllib.parse import quote
from shutil import rmtree

Expand All @@ -45,14 +45,12 @@
unittest,
)


try:
QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT = os.environ['QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT']
QGIS_SERVER_ENDPOINT_PORT = os.environ['QGIS_SERVER_ENDPOINT_PORT']
except:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT = s.getsockname()[1]
s.close()
QGIS_SERVER_ENDPOINT_PORT = '0' # Auto


QGIS_AUTH_DB_DIR_PATH = tempfile.mkdtemp()

Expand All @@ -67,7 +65,7 @@ class TestAuthManager(unittest.TestCase):
def setUpClass(cls):
"""Run before all tests:
Creates an auth configuration"""
cls.port = QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT
cls.port = QGIS_SERVER_ENDPOINT_PORT
# Clean env just to be sure
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
for ev in env_vars:
Expand Down Expand Up @@ -96,13 +94,18 @@ def setUpClass(cls):
server_path = os.path.dirname(os.path.realpath(__file__)) + \
'/qgis_wrapped_server.py'
cls.server = subprocess.Popen([sys.executable, server_path],
env=os.environ)
env=os.environ, stdout=subprocess.PIPE)
line = cls.server.stdout.readline()
cls.port = int(re.findall(b':(\d+)', line)[0])
assert cls.port != 0
# Wait for the server process to start
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
cls.server.terminate()
cls.server.wait()
rmtree(QGIS_AUTH_DB_DIR_PATH)
del cls.server

Expand Down
30 changes: 14 additions & 16 deletions tests/src/python/test_offline_editing_wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import os
import sys
import re
import subprocess
from shutil import copytree, rmtree
import tempfile
Expand All @@ -47,13 +48,9 @@


try:
QGIS_SERVER_WFST_DEFAULT_PORT = os.environ['QGIS_SERVER_WFST_DEFAULT_PORT']
QGIS_SERVER_OFFLINE_PORT = os.environ['QGIS_SERVER_OFFLINE_PORT']
except:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
QGIS_SERVER_WFST_DEFAULT_PORT = s.getsockname()[1]
s.close()
QGIS_SERVER_OFFLINE_PORT = '0' # Auto

qgis_app = start_app()

Expand All @@ -66,7 +63,7 @@ class TestWFST(unittest.TestCase, OfflineTestBase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.port = QGIS_SERVER_WFST_DEFAULT_PORT
cls.port = QGIS_SERVER_OFFLINE_PORT
# Create tmp folder
cls.temp_path = tempfile.mkdtemp()
cls.testdata_path = cls.temp_path + '/' + 'wfs_transactional' + '/'
Expand Down Expand Up @@ -97,7 +94,10 @@ def tearDownClass(cls):
def setUp(self):
"""Run before each test."""
self.server = subprocess.Popen([sys.executable, self.server_path],
env=os.environ)
env=os.environ, stdout=subprocess.PIPE)
line = self.server.stdout.readline()
self.port = int(re.findall(b':(\d+)', line)[0])
assert self.port != 0
# Wait for the server process to start
assert waitServer('http://127.0.0.1:%s' % self.port), "Server is not responding!"
self._setUp()
Expand All @@ -108,15 +108,13 @@ def tearDown(self):
self._clearLayer(self._getOnlineLayer('test_point'))
# Kill the server
self.server.terminate()
self.server.wait()
del self.server
# Wait for the server process to stop
sleep(2)
# Delete the sqlite db
os.unlink(os.path.join(self.temp_path, 'offlineDbFile.sqlite'))
self._tearDown()

@classmethod
def _getOnlineLayer(cls, type_name, layer_name=None):
def _getOnlineLayer(self, type_name, layer_name=None):
"""
Return a new WFS layer, overriding the WFS cache
"""
Expand All @@ -125,14 +123,14 @@ def _getOnlineLayer(cls, type_name, layer_name=None):
parms = {
'srsname': 'EPSG:4326',
'typename': type_name,
'url': 'http://127.0.0.1:%s/%s/?map=%s' % (cls.port,
cls.counter,
cls.project_path),
'url': 'http://127.0.0.1:%s/%s/?map=%s' % (self.port,
self.counter,
self.project_path),
'version': 'auto',
'table': '',
#'sql': '',
}
cls.counter += 1
self.counter += 1
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
assert wfs_layer.isValid()
Expand Down
18 changes: 10 additions & 8 deletions tests/src/python/test_qgsserver_wfst.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import os
import sys
import re
import subprocess
from shutil import copytree, rmtree
import tempfile
Expand All @@ -57,13 +58,9 @@
)

try:
QGIS_SERVER_WFST_DEFAULT_PORT = os.environ['QGIS_SERVER_WFST_DEFAULT_PORT']
QGIS_SERVER_WFST_PORT = os.environ['QGIS_SERVER_WFST_PORT']
except:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
QGIS_SERVER_WFST_DEFAULT_PORT = s.getsockname()[1]
s.close()
QGIS_SERVER_WFST_PORT = '0' # Auto


qgis_app = start_app()
Expand All @@ -74,7 +71,7 @@ class TestWFST(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
cls.port = QGIS_SERVER_WFST_DEFAULT_PORT
cls.port = QGIS_SERVER_WFST_PORT
# Create tmp folder
cls.temp_path = tempfile.mkdtemp()
cls.testdata_path = cls.temp_path + '/' + 'wfs_transactional' + '/'
Expand All @@ -98,13 +95,18 @@ def setUpClass(cls):
server_path = os.path.dirname(os.path.realpath(__file__)) + \
'/qgis_wrapped_server.py'
cls.server = subprocess.Popen([sys.executable, server_path],
env=os.environ)
env=os.environ, stdout=subprocess.PIPE)
line = cls.server.stdout.readline()
cls.port = int(re.findall(b':(\d+)', line)[0])
assert cls.port != 0
# Wait for the server process to start
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
cls.server.terminate()
cls.server.wait()
del cls.server
# Clear all test layers
for ln in ['test_point', 'test_polygon', 'test_linestring']:
Expand Down

0 comments on commit d3f8763

Please sign in to comment.