get tests to pass

This commit is contained in:
Brian Warner 2017-09-13 17:49:42 -07:00
parent 03e8d36e7f
commit b15e955fec
4 changed files with 18 additions and 74 deletions

View File

@ -1,69 +1,24 @@
# no unicode_literals untill twisted update #from __future__ import unicode_literals
from twisted.application import service from twisted.internet import reactor, endpoints
from twisted.internet import defer, task, reactor from twisted.internet.defer import inlineCallbacks
from twisted.python import log from ..transit_server import Transit
from click.testing import CliRunner
import mock
from ..cli import cli
from ..transit import allocate_tcp_port
from ..server.server import RelayServer
class ServerBase: class ServerBase:
def setUp(self): def setUp(self):
self._setup_relay(None) self._lp = None
self._setup_relay()
def _setup_relay(self, error, advertise_version=None): @inlineCallbacks
self.sp = service.MultiService() def _setup_relay(self, blur_usage=None, usage_logfile=None, stats_file=None):
self.sp.startService() ep = endpoints.TCP4ServerEndpoint(reactor, 0, interface="127.0.0.1")
self.transitport = allocate_tcp_port() self._transit_server = Transit(blur_usage=blur_usage,
# need to talk to twisted team about only using unicode in usage_logfile=usage_logfile,
# endpoints.serverFromString stats_file=stats_file)
s = RelayServer("tcp:%s:interface=127.0.0.1" % self.transitport, self._lp = yield ep.listen(self._transit_server)
advertise_version=advertise_version, addr = self._lp.getHost()
signal_error=error)
s.setServiceParent(self.sp)
self._relay_server = s
self._rendezvous = s._rendezvous
self._transit_server = s._transit
# ws://127.0.0.1:%d/wormhole-relay/ws # 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): def tearDown(self):
# Unit tests that spawn a (blocking) client in a thread might still if self._lp:
# have threads running at this point, if one is stuck waiting for a return self._lp.stopListening()
# 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

View File

@ -3,7 +3,6 @@ from binascii import hexlify
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import protocol, reactor, defer from twisted.internet import protocol, reactor, defer
from twisted.internet.endpoints import clientFromString, connectProtocol from twisted.internet.endpoints import clientFromString, connectProtocol
from twisted.web import client
from .common import ServerBase from .common import ServerBase
from .. import transit_server from .. import transit_server
@ -51,11 +50,6 @@ class Transit(ServerBase, unittest.TestCase):
self.failUnlessEqual(blur(1100e6), 1100e6) self.failUnlessEqual(blur(1100e6), 1100e6)
self.failUnlessEqual(blur(1150e6), 1200e6) 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 @defer.inlineCallbacks
def test_register(self): def test_register(self):
ep = clientFromString(reactor, self.transit) ep = clientFromString(reactor, self.transit)

View File

@ -2,7 +2,6 @@ from __future__ import print_function, unicode_literals
import os, re, time, json import os, re, time, json
from twisted.python import log from twisted.python import log
from twisted.internet import protocol from twisted.internet import protocol
from twisted.application import service
SECONDS = 1.0 SECONDS = 1.0
MINUTE = 60*SECONDS MINUTE = 60*SECONDS
@ -222,11 +221,9 @@ class Transit(protocol.ServerFactory):
protocol = TransitConnection protocol = TransitConnection
def __init__(self, blur_usage, usage_logfile, stats_file): def __init__(self, blur_usage, usage_logfile, stats_file):
service.MultiService.__init__(self)
self._blur_usage = blur_usage self._blur_usage = blur_usage
self._log_requests = blur_usage is None self._log_requests = blur_usage is None
if usage_logfile: self._usage_logfile = open(usage_logfile, "a") if usage_logfile else None
self._usage_logfile = open(usage_logfile, "a")
self._stats_file = stats_file self._stats_file = stats_file
self._pending_requests = {} # token -> set((side, TransitConnection)) self._pending_requests = {} # token -> set((side, TransitConnection))
self._active_connections = set() # TransitConnection self._active_connections = set() # TransitConnection

View File

@ -15,7 +15,6 @@ deps =
pyflakes >= 1.2.3 pyflakes >= 1.2.3
commands = commands =
pyflakes setup.py src pyflakes setup.py src
wormhole --version
python -m twisted.trial {posargs:wormhole_transit_relay} python -m twisted.trial {posargs:wormhole_transit_relay}
[testenv:coverage] [testenv:coverage]
@ -24,6 +23,5 @@ deps =
coverage coverage
commands = commands =
pyflakes setup.py src pyflakes setup.py src
wormhole --version
coverage run --branch -m twisted.trial {posargs:wormhole_transit_relay} coverage run --branch -m twisted.trial {posargs:wormhole_transit_relay}
coverage xml coverage xml