9
9
10
10
import os
11
11
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
12
18
from os .path import join
13
19
14
20
from functools import partial
26
32
# twice.
27
33
from ..helpers import _cleanup_crossbar , start_crossbar , functest_session
28
34
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
29
68
30
69
31
70
@inlineCallbacks
@@ -143,8 +182,8 @@ def test_proxy(request, virtualenv, reactor, session_temp):
143
182
"id" : "ws_test_0" ,
144
183
"endpoint" : {
145
184
"type" : "tcp" ,
146
- "port" : 8443 ,
147
- "shared" : True ,
185
+ "port" : proxy_port ( 0 ) ,
186
+ "shared" : shared_port () ,
148
187
},
149
188
"paths" : {
150
189
"autobahn" : {
@@ -236,8 +275,8 @@ def test_proxy(request, virtualenv, reactor, session_temp):
236
275
"type" : "web" ,
237
276
"endpoint" : {
238
277
"type" : "tcp" ,
239
- "port" : 8443 ,
240
- "shared" : True ,
278
+ "port" : proxy_port ( 1 ) ,
279
+ "shared" : shared_port () ,
241
280
},
242
281
"paths" : {
243
282
"autobahn" : {
@@ -303,6 +342,7 @@ class WaitForTransportAndProxy(object):
303
342
Super hacky, but ... other suggestions? Could busy-wait for ports
304
343
to become connect()-able? Better text to search for?
305
344
"""
345
+
306
346
def __init__ (self , done ):
307
347
self .data = ''
308
348
self .done = done
@@ -332,11 +372,11 @@ def write(self, data):
332
372
333
373
listening = Deferred ()
334
374
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 ,
340
380
)
341
381
request .addfinalizer (partial (_cleanup_crossbar , protocol ))
342
382
@@ -386,6 +426,7 @@ def call_test(*args, **kw):
386
426
@callee .on_ready
387
427
def _ (session ):
388
428
callee_ready .callback (None )
429
+
389
430
callee .start ()
390
431
391
432
yield callee_ready
@@ -394,7 +435,6 @@ def _(session):
394
435
caller_sessions = []
395
436
results = []
396
437
for _ in range (num_callees ):
397
-
398
438
@inlineCallbacks
399
439
def main (reactor , session ):
400
440
# print("main: {} {}".format(reactor, session))
0 commit comments