Skip to content

Commit 6debfe1

Browse files
committed
Allow proxy tests to run on platform without shared socket support
1 parent 15ee8cd commit 6debfe1

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

test/functests/cbtests/test_cb_proxy.py

+50-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
import os
1111
import re
12+
import socket
13+
import sys
14+
import platform
15+
16+
from twisted.internet import fdesc, tcp, ssl
17+
from twisted.python.runtime import platformType
1218
from os.path import join
1319

1420
from functools import partial
@@ -26,6 +32,39 @@
2632
# twice.
2733
from ..helpers import _cleanup_crossbar, start_crossbar, functest_session
2834

35+
# Allow to test on macOS and other platforms without shared ports
36+
37+
_HAS_SHARED_LOADBALANCED_SOCKET = False
38+
39+
if sys.platform.startswith('linux'):
40+
try:
41+
# get Linux kernel version, like: (3, 19)
42+
_LINUX_KERNEL_VERSION = [int(x) for x in tuple(sys.platform.uname()[2].split('.')[:2])]
43+
44+
# SO_REUSEPORT only supported for Linux kernels >= 3.9
45+
if (_LINUX_KERNEL_VERSION[0] == 3 and _LINUX_KERNEL_VERSION[1] >= 9) or _LINUX_KERNEL_VERSION[0] >= 4:
46+
_HAS_SHARED_LOADBALANCED_SOCKET = True
47+
48+
# monkey patch missing constant if needed
49+
if not hasattr(socket, 'SO_REUSEPORT'):
50+
socket.SO_REUSEPORT = 15
51+
except:
52+
pass
53+
54+
elif sys.platform == 'win32':
55+
# http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
56+
_HAS_SHARED_LOADBALANCED_SOCKET = True
57+
58+
59+
def shared_port():
60+
return _HAS_SHARED_LOADBALANCED_SOCKET
61+
62+
63+
def proxy_port(proxy_ordinal):
64+
if _HAS_SHARED_LOADBALANCED_SOCKET:
65+
return 8443
66+
else:
67+
return 8443 + proxy_ordinal
2968

3069

3170
@inlineCallbacks
@@ -143,8 +182,8 @@ def test_proxy(request, virtualenv, reactor, session_temp):
143182
"id": "ws_test_0",
144183
"endpoint": {
145184
"type": "tcp",
146-
"port": 8443,
147-
"shared": True,
185+
"port": proxy_port(0),
186+
"shared": shared_port(),
148187
},
149188
"paths": {
150189
"autobahn": {
@@ -236,8 +275,8 @@ def test_proxy(request, virtualenv, reactor, session_temp):
236275
"type": "web",
237276
"endpoint": {
238277
"type": "tcp",
239-
"port": 8443,
240-
"shared": True,
278+
"port": proxy_port(1),
279+
"shared": shared_port(),
241280
},
242281
"paths": {
243282
"autobahn": {
@@ -303,6 +342,7 @@ class WaitForTransportAndProxy(object):
303342
Super hacky, but ... other suggestions? Could busy-wait for ports
304343
to become connect()-able? Better text to search for?
305344
"""
345+
306346
def __init__(self, done):
307347
self.data = ''
308348
self.done = done
@@ -332,11 +372,11 @@ def write(self, data):
332372

333373
listening = Deferred()
334374
protocol = yield start_crossbar(
335-
reactor, virtualenv,
336-
cbdir, crossbar_config,
337-
stdout=WaitForTransportAndProxy(listening),
338-
stderr=WaitForTransportAndProxy(listening),
339-
log_level='debug' if request.config.getoption('logdebug', False) else False,
375+
reactor, virtualenv,
376+
cbdir, crossbar_config,
377+
stdout=WaitForTransportAndProxy(listening),
378+
stderr=WaitForTransportAndProxy(listening),
379+
log_level='debug' if request.config.getoption('logdebug', False) else False,
340380
)
341381
request.addfinalizer(partial(_cleanup_crossbar, protocol))
342382

@@ -386,6 +426,7 @@ def call_test(*args, **kw):
386426
@callee.on_ready
387427
def _(session):
388428
callee_ready.callback(None)
429+
389430
callee.start()
390431

391432
yield callee_ready
@@ -394,7 +435,6 @@ def _(session):
394435
caller_sessions = []
395436
results = []
396437
for _ in range(num_callees):
397-
398438
@inlineCallbacks
399439
def main(reactor, session):
400440
# print("main: {} {}".format(reactor, session))

0 commit comments

Comments
 (0)