def test_peer_connection_failure(): # Test connecting a peer when the first connection attempt fails. MockConnection = mock.MagicMock() connection = mock.MagicMock() with mock.patch.object(tpeer.Peer, 'connection_class', MockConnection): @gen.coroutine def try_connect(*args, **kwargs): if MockConnection.outgoing.call_count == 1: # If this is the first call, fail. raise ZeroDivisionError('great sadness') else: raise gen.Return(connection) MockConnection.outgoing.side_effect = try_connect peer = tpeer.Peer(mock.MagicMock(), 'localhost:4040') future = peer.connect() with pytest.raises(ZeroDivisionError) as excinfo: yield future assert 'great sadness' in str(excinfo) got = yield peer.connect() assert got is connection assert MockConnection.outgoing.call_count == 2
def test_peer_incoming_connections_are_preferred(request): incoming = mock.MagicMock() outgoing = mock.MagicMock() peer = tpeer.Peer(mock.MagicMock(), 'localhost:4040') with mock.patch('tchannel.tornado.connection.StreamConnection.outgoing' ) as mock_outgoing: mock_outgoing.return_value = gen.maybe_future(outgoing) peer.connect() assert (yield peer.connect()) is outgoing peer.register_incoming_conn(incoming) assert (yield peer.connect()) is incoming