tor_provider: use new Foolscap API to provide better status

This commit is contained in:
Brian Warner 2016-12-08 15:11:45 -08:00
parent de4295ae60
commit 8d008967e7
3 changed files with 26 additions and 8 deletions

View File

@ -62,7 +62,8 @@ class Tor(unittest.TestCase):
n = FakeNode(config)
h = n._make_tor_handler()
private_dir = os.path.join(n.basedir, "private")
exp = mock.call(n._tor_provider._make_control_endpoint)
exp = mock.call(n._tor_provider._make_control_endpoint,
takes_status=True)
self.assertEqual(f.mock_calls, [exp])
self.assertIdentical(h, h1)
@ -75,7 +76,9 @@ class Tor(unittest.TestCase):
cfs = mock.Mock(return_value=tcep)
with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor):
with mock.patch("allmydata.util.tor_provider.clientFromString", cfs):
cep = self.successResultOf(tp._make_control_endpoint(reactor))
d = tp._make_control_endpoint(reactor,
update_status=lambda status: None)
cep = self.successResultOf(d)
launch_tor.assert_called_with(reactor, executable, private_dir,
tp._txtorcon)
cfs.assert_called_with(reactor, "ep_desc")

View File

@ -271,6 +271,14 @@ class FakeConfig(dict):
raise KeyError
return value
class EmptyContext(object):
def __init__(self):
pass
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
class Provider(unittest.TestCase):
def test_build(self):
tor_provider.Provider("basedir", FakeConfig(), "reactor")
@ -298,13 +306,15 @@ class Provider(unittest.TestCase):
txtorcon = mock.Mock()
handler = object()
tor.control_endpoint_maker = mock.Mock(return_value=handler)
tor.add_context = mock.Mock(return_value=EmptyContext())
with mock_tor(tor):
with mock_txtorcon(txtorcon):
p = tor_provider.Provider("basedir", FakeConfig(launch=True),
reactor)
h = p.get_tor_handler()
self.assertIs(h, handler)
tor.control_endpoint_maker.assert_called_with(p._make_control_endpoint)
tor.control_endpoint_maker.assert_called_with(p._make_control_endpoint,
takes_status=True)
# make sure Tor is launched just once, the first time an endpoint is
# requested, and never again. The clientFromString() function is
@ -316,7 +326,8 @@ class Provider(unittest.TestCase):
cfs = mock.Mock(return_value=ep)
with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor):
with mock.patch("allmydata.util.tor_provider.clientFromString", cfs):
d = p._make_control_endpoint(reactor)
d = p._make_control_endpoint(reactor,
update_status=lambda status: None)
yield flushEventualQueue()
self.assertIs(self.successResultOf(d), ep)
launch_tor.assert_called_with(reactor, None,
@ -328,7 +339,8 @@ class Provider(unittest.TestCase):
cfs2 = mock.Mock(return_value=ep)
with mock.patch("allmydata.util.tor_provider._launch_tor", launch_tor2):
with mock.patch("allmydata.util.tor_provider.clientFromString", cfs2):
d2 = p._make_control_endpoint(reactor)
d2 = p._make_control_endpoint(reactor,
update_status=lambda status: None)
yield flushEventualQueue()
self.assertIs(self.successResultOf(d2), ep)
self.assertEqual(launch_tor2.mock_calls, [])

View File

@ -226,7 +226,8 @@ class Provider(service.MultiService):
if self._get_tor_config("launch", False, boolean=True):
if not self._txtorcon:
return None
return self._tor.control_endpoint_maker(self._make_control_endpoint)
return self._tor.control_endpoint_maker(self._make_control_endpoint,
takes_status=True)
socks_endpoint_desc = self._get_tor_config("socks.port", None)
if socks_endpoint_desc:
@ -241,9 +242,11 @@ class Provider(service.MultiService):
return self._tor.default_socks()
@inlineCallbacks
def _make_control_endpoint(self, reactor):
def _make_control_endpoint(self, reactor, update_status):
# this will only be called when tahoe.cfg has "[tor] launch = true"
(endpoint_desc, _) = yield self._get_launched_tor(reactor)
update_status("launching Tor")
with self._tor.add_context(update_status, "launching Tor"):
(endpoint_desc, _) = yield self._get_launched_tor(reactor)
tor_control_endpoint = clientFromString(reactor, endpoint_desc)
returnValue(tor_control_endpoint)