diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-08-04 15:21:31 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-08-04 15:21:31 +0300 |
commit | c8dc380943f4f1a2533435c31a815ffed5af728d (patch) | |
tree | 24fcafc9987a19b4a09dc2b4af553a0fc99a1a96 | |
parent | 3618aa15de7fdfe44c575a4f92b68f036363c556 (diff) | |
download | qvisqve-c8dc380943f4f1a2533435c31a815ffed5af728d.tar.gz |
Add: use random port to run qvarn backend during tests
This is not 100% reliable, due to a race condition: it's possible for
the test code to pick a free port, and someone else to immediately
reserve the port before Qvarn does. But it's too unlikely to be a real
problem, for tests.
-rwxr-xr-x | randport | 28 | ||||
-rw-r--r-- | yarns/900-implements.yarn | 4 | ||||
-rw-r--r-- | yarns/lib.py | 1 |
3 files changed, 30 insertions, 3 deletions
diff --git a/randport b/randport new file mode 100755 index 0000000..d523401 --- /dev/null +++ b/randport @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +# +# Copyright (C) 2017 Lars Wirzenius +# +# Pick a random port that is free to be listened on. For testing. + + +import errno +import random +import socket +import sys + + +MAX = 1000 +for i in range(MAX): + port = random.randint(1025, 2**15-1) + s = socket.socket() + try: + s.bind(('0.0.0.0', port)) + except OSError as e: + if e.errno == errno.EADDRINUSE: + continue + raise + break +else: + sys.stderr.write("Can't find a free port\n") + sys.exit(1) +sys.stdout.write('{}\n'.format(port)) diff --git a/yarns/900-implements.yarn b/yarns/900-implements.yarn index 7837f8c..d6abb7a 100644 --- a/yarns/900-implements.yarn +++ b/yarns/900-implements.yarn @@ -17,9 +17,7 @@ Start a Qvarn running in the background. vars['api.log'] = 'qvarn.log' vars['gunicorn3.log'] = 'gunicorn3.log' vars['pid-file'] = 'pid' - # FIXME: It would be good for the test suite to pick a random free - # port. But that's not simple. - vars['port'] = 12765 + vars['port'] = cliapp.runcmd([os.path.join(srcdir, 'randport' )]).strip() vars['url'] = 'http://127.0.0.1:{}'.format(vars['port']) config = { 'log': [ diff --git a/yarns/lib.py b/yarns/lib.py index 37ffc21..4b0f250 100644 --- a/yarns/lib.py +++ b/yarns/lib.py @@ -35,6 +35,7 @@ vars = Variables(datadir) def get(url, headers=None): + print('get: url={} headers={}'.format(url, headers)) r = requests.get(url, headers=headers) return r.status_code, r.text |