class FakeTorState(object): def __init__(self, routers): self.routers = routers self.protocol = TorControlProtocol() self.protocol.connectionMade = lambda: None self.protocol.transport = proto_helpers.StringTransport() self.protocol.makeConnection(self.protocol.transport) def _find_circuit_after_extend(self, x): return defer.succeed(None) def build_circuit(self, routers=None, using_guards=True): print "build circuit" cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, basestring) and len(router) == 40 \ and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) d.addCallback(self._find_circuit_after_extend) return d
class FakeTorState(object): def __init__(self, routers): self.routers = routers self.protocol = TorControlProtocol() self.protocol.connectionMade = lambda: None self.protocol.transport = proto_helpers.StringTransport() self.protocol.makeConnection(self.protocol.transport) def _find_circuit_after_extend(self, x): return defer.succeed(None) def build_circuit(self, routers=None, using_guards=True): cmd = "EXTENDCIRCUIT 0 " first = True for router in routers: if first: first = False else: cmd += ',' if isinstance(router, basestring) and len(router) == 40 \ and hashFromHexId(router): cmd += router else: cmd += router.id_hex[1:] d = self.protocol.queue_command(cmd) print "d %r" % (d,) d.addCallback(self._find_circuit_after_extend) return d
class DisconnectionTests(unittest.TestCase): def setUp(self): self.protocol = TorControlProtocol() self.protocol.connectionMade = lambda: None self.transport = proto_helpers.StringTransportWithDisconnection() self.protocol.makeConnection(self.transport) # why doesn't makeConnection do this? self.transport.protocol = self.protocol def tearDown(self): self.protocol = None def test_disconnect_callback(self): """ see that we get our callback on_disconnect if the transport goes away """ def it_was_called(*args): it_was_called.yes = True return None it_was_called.yes = False self.protocol.on_disconnect.addCallback(it_was_called) self.protocol.on_disconnect.addErrback(it_was_called) f = failure.Failure(error.ConnectionDone("It's all over")) self.protocol.connectionLost(f) self.assertTrue(it_was_called.yes) def test_when_disconnect(self): """ see that we get our callback for when_disconnected if the transport goes away """ def it_was_called(arg): it_was_called.yes = True return None it_was_called.yes = False d = self.protocol.when_disconnected() d.addCallback(it_was_called) f = failure.Failure(error.ConnectionDone("It's all over")) self.protocol.connectionLost(f) self.assertTrue(it_was_called.yes) def test_when_disconnect_error(self): """ see that we get our errback for when_disconnected if the transport goes away """ def it_was_called(arg): it_was_called.yes = True return None it_was_called.yes = False d = self.protocol.when_disconnected() d.addErrback(it_was_called) f = failure.Failure(RuntimeError("sadness")) self.protocol.connectionLost(f) self.assertTrue(it_was_called.yes) def test_disconnect_errback(self): """ see that we get our callback on_disconnect if the transport goes away """ def it_was_called(*args): it_was_called.yes = True return None it_was_called.yes = False self.protocol.on_disconnect.addCallback(it_was_called) self.protocol.on_disconnect.addErrback(it_was_called) f = failure.Failure(RuntimeError("The thing didn't do the stuff.")) self.protocol.connectionLost(f) self.assertTrue(it_was_called.yes) def test_disconnect_outstanding_commands(self): """ outstanding commands should errback on disconnect """ def it_was_called(f): str(f) it_was_called.count += 1 return None it_was_called.count = 0 # we want to make sure outstanding commands get errbacks d0 = self.protocol.queue_command("some command0") d1 = self.protocol.queue_command("some command1") d0.addErrback(it_was_called) d1.addErrback(it_was_called) self.protocol.on_disconnect.addErrback(lambda _: None) f = failure.Failure(RuntimeError("The thing didn't do the stuff.")) self.protocol.connectionLost(f) self.assertEqual(it_was_called.count, 2)