From 8d008967e71b23035280aa01640fdfbbf6f3a2ad Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 8 Dec 2016 15:11:45 -0800 Subject: [PATCH] tor_provider: use new Foolscap API to provide better status --- src/allmydata/test/test_connections.py | 7 +++++-- src/allmydata/test/test_tor_provider.py | 18 +++++++++++++++--- src/allmydata/util/tor_provider.py | 9 ++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/allmydata/test/test_connections.py b/src/allmydata/test/test_connections.py index df783f784..0683ced87 100644 --- a/src/allmydata/test/test_connections.py +++ b/src/allmydata/test/test_connections.py @@ -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") diff --git a/src/allmydata/test/test_tor_provider.py b/src/allmydata/test/test_tor_provider.py index 2e09ddf9f..dbb74e4b2 100644 --- a/src/allmydata/test/test_tor_provider.py +++ b/src/allmydata/test/test_tor_provider.py @@ -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, []) diff --git a/src/allmydata/util/tor_provider.py b/src/allmydata/util/tor_provider.py index f20ed7f6a..1c930c14e 100644 --- a/src/allmydata/util/tor_provider.py +++ b/src/allmydata/util/tor_provider.py @@ -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)