diff --git a/src/wormhole_transit_relay/test/common.py b/src/wormhole_transit_relay/test/common.py index 6404532..440c028 100644 --- a/src/wormhole_transit_relay/test/common.py +++ b/src/wormhole_transit_relay/test/common.py @@ -1,69 +1,24 @@ -# no unicode_literals untill twisted update -from twisted.application import service -from twisted.internet import defer, task, reactor -from twisted.python import log -from click.testing import CliRunner -import mock -from ..cli import cli -from ..transit import allocate_tcp_port -from ..server.server import RelayServer +#from __future__ import unicode_literals +from twisted.internet import reactor, endpoints +from twisted.internet.defer import inlineCallbacks +from ..transit_server import Transit class ServerBase: def setUp(self): - self._setup_relay(None) + self._lp = None + self._setup_relay() - def _setup_relay(self, error, advertise_version=None): - self.sp = service.MultiService() - self.sp.startService() - self.transitport = allocate_tcp_port() - # need to talk to twisted team about only using unicode in - # endpoints.serverFromString - s = RelayServer("tcp:%s:interface=127.0.0.1" % self.transitport, - advertise_version=advertise_version, - signal_error=error) - s.setServiceParent(self.sp) - self._relay_server = s - self._rendezvous = s._rendezvous - self._transit_server = s._transit + @inlineCallbacks + def _setup_relay(self, blur_usage=None, usage_logfile=None, stats_file=None): + ep = endpoints.TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1") + self._transit_server = Transit(blur_usage=blur_usage, + usage_logfile=usage_logfile, + stats_file=stats_file) + self._lp = yield ep.listen(self._transit_server) + addr = self._lp.getHost() # ws://127.0.0.1:%d/wormhole-relay/ws - self.transit = u"tcp:127.0.0.1:%d" % self.transitport + self.transit = u"tcp:127.0.0.1:%d" % addr.port def tearDown(self): - # Unit tests that spawn a (blocking) client in a thread might still - # have threads running at this point, if one is stuck waiting for a - # message from a companion which has exited with an error. Our - # relay's .stopService() drops all connections, which ought to - # encourage those threads to terminate soon. If they don't, print a - # warning to ease debugging. - - # XXX FIXME there's something in _noclobber test that's not - # waiting for a close, I think -- was pretty relieably getting - # unclean-reactor, but adding a slight pause here stops it... - from twisted.internet import reactor - - tp = reactor.getThreadPool() - if not tp.working: - d = defer.succeed(None) - d.addCallback(lambda _: self.sp.stopService()) - d.addCallback(lambda _: task.deferLater(reactor, 0.1, lambda: None)) - return d - return self.sp.stopService() - # disconnect all callers - d = defer.maybeDeferred(self.sp.stopService) - wait_d = defer.Deferred() - # wait a second, then check to see if it worked - reactor.callLater(1.0, wait_d.callback, None) - def _later(res): - if len(tp.working): - log.msg("wormhole.test.common.ServerBase.tearDown:" - " I was unable to convince all threads to exit.") - tp.dumpStats() - print("tearDown warning: threads are still active") - print("This test will probably hang until one of the" - " clients gives up of their own accord.") - else: - log.msg("wormhole.test.common.ServerBase.tearDown:" - " I convinced all threads to exit.") - return d - wait_d.addCallback(_later) - return wait_d + if self._lp: + return self._lp.stopListening() diff --git a/src/wormhole_transit_relay/test/test_transit_server.py b/src/wormhole_transit_relay/test/test_transit_server.py index 304fbde..eb2e997 100644 --- a/src/wormhole_transit_relay/test/test_transit_server.py +++ b/src/wormhole_transit_relay/test/test_transit_server.py @@ -3,7 +3,6 @@ from binascii import hexlify from twisted.trial import unittest from twisted.internet import protocol, reactor, defer from twisted.internet.endpoints import clientFromString, connectProtocol -from twisted.web import client from .common import ServerBase from .. import transit_server @@ -51,11 +50,6 @@ class Transit(ServerBase, unittest.TestCase): self.failUnlessEqual(blur(1100e6), 1100e6) self.failUnlessEqual(blur(1150e6), 1200e6) - @defer.inlineCallbacks - def test_web_request(self): - resp = yield client.getPage('http://127.0.0.1:{}/'.format(self.relayport).encode('ascii')) - self.assertEqual('Wormhole Relay'.encode('ascii'), resp.strip()) - @defer.inlineCallbacks def test_register(self): ep = clientFromString(reactor, self.transit) diff --git a/src/wormhole_transit_relay/transit_server.py b/src/wormhole_transit_relay/transit_server.py index cf08e88..f792aa2 100644 --- a/src/wormhole_transit_relay/transit_server.py +++ b/src/wormhole_transit_relay/transit_server.py @@ -2,7 +2,6 @@ from __future__ import print_function, unicode_literals import os, re, time, json from twisted.python import log from twisted.internet import protocol -from twisted.application import service SECONDS = 1.0 MINUTE = 60*SECONDS @@ -222,11 +221,9 @@ class Transit(protocol.ServerFactory): protocol = TransitConnection def __init__(self, blur_usage, usage_logfile, stats_file): - service.MultiService.__init__(self) self._blur_usage = blur_usage self._log_requests = blur_usage is None - if usage_logfile: - self._usage_logfile = open(usage_logfile, "a") + self._usage_logfile = open(usage_logfile, "a") if usage_logfile else None self._stats_file = stats_file self._pending_requests = {} # token -> set((side, TransitConnection)) self._active_connections = set() # TransitConnection diff --git a/tox.ini b/tox.ini index ff99228..1aca8d2 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,6 @@ deps = pyflakes >= 1.2.3 commands = pyflakes setup.py src - wormhole --version python -m twisted.trial {posargs:wormhole_transit_relay} [testenv:coverage] @@ -24,6 +23,5 @@ deps = coverage commands = pyflakes setup.py src - wormhole --version coverage run --branch -m twisted.trial {posargs:wormhole_transit_relay} coverage xml