From 4e101917f94602e2b5df7ba50d0f6113b10718ce Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Wed, 1 Feb 2012 13:38:45 +0000 Subject: [PATCH 1/2] Tests: use another tcp port if 4242 is already used https://bugs.freedesktop.org/show_bug.cgi?id=45499 --- tests/twisted/gabbletest.py | 69 +++++++++++++++++++++++++----------------- 1 files changed, 41 insertions(+), 28 deletions(-) diff --git a/tests/twisted/gabbletest.py b/tests/twisted/gabbletest.py index 2ee15dd..ba9004c 100644 --- a/tests/twisted/gabbletest.py +++ b/tests/twisted/gabbletest.py @@ -478,7 +478,7 @@ class GoogleXmlStream(BaseXmlStream): self.send(iq) -def make_connection(bus, event_func, params=None, suffix=''): +def make_connection(bus, event_func, params=None, suffix='', tcpport=4242): # Gabble accepts a resource in 'account', but the value of 'resource' # overrides it if there is one. test_name = re.sub('(.*tests/twisted/|\./)', '', sys.argv[0]) @@ -489,7 +489,7 @@ def make_connection(bus, event_func, params=None, suffix=''): 'password': 'pass', 'resource': 'Resource', 'server': 'localhost', - 'port': dbus.UInt32(4242), + 'port': dbus.UInt32(tcpport), 'fallback-socks5-proxies': dbus.Array([], signature='s'), 'require-encryption': False, } @@ -557,37 +557,50 @@ def exec_test_deferred(fun, params, protocol=None, timeout=None, os.environ.get('CHECK_TWISTED_VERBOSE', '') != '' or '-v' in sys.argv) - conns = [] - jids = [] - streams = [] - resource = params.get('resource') if params is not None else None - for i in range(0, num_instances): - if i == 0: - suffix = '' - else: - suffix = str(i) + # If an application unrelated to our tests already listens on our + # preferred port, we try the next one. + listen_success = False + for tcpport in range(4242, 4343): + conns = [] + jids = [] + streams = [] + resource = params.get('resource') if params is not None else None + for i in range(0, num_instances): + if i == 0: + suffix = '' + else: + suffix = str(i) - try: - (conn, jid) = make_connection(bus, queue.append, params, suffix) - except Exception, e: - # Crap. This is normally because the connection's still kicking - # around on the bus. Let's bin any connections we *did* manage to - # get going and then bail out unceremoniously. - print e + try: + (conn, jid) = make_connection(bus, queue.append, params, + suffix, tcpport=tcpport) + except Exception, e: + # Crap. This is normally because the connection's still kicking + # around on the bus. Let's bin any connections we *did* manage to + # get going and then bail out unceremoniously. + print e - for conn in conns: - conn.Disconnect() + for conn in conns: + conn.Disconnect() - os._exit(1) + os._exit(1) - conns.append(conn) - jids.append(jid) - streams.append(make_stream(queue.append, protocol=protocol, - authenticator=authenticator, - resource=resource, suffix=suffix)) + conns.append(conn) + jids.append(jid) + streams.append(make_stream(queue.append, protocol=protocol, + authenticator=authenticator, + resource=resource, suffix=suffix)) - factory = StreamFactory(streams, jids) - port = reactor.listenTCP(4242, factory, interface='localhost') + factory = StreamFactory(streams, jids) + try: + port = reactor.listenTCP(tcpport, factory, interface='localhost') + listen_success = True + break + except Exception, e: + for conn in conns: + conn.Disconnect() + continue + assert listen_success == True, listen_success def signal_receiver(*args, **kw): if kw['path'] == '/org/freedesktop/DBus' and \ -- 1.7.7.3