def test_recv_all_no_newline(self):
     connection = Connection('host', 1)
     connection._sock = mock.MagicMock()
     buf = io.BytesIO(b'foo')
     connection._sock.recv = lambda buf_size: buf.read()
     response = connection._recv_all()
     expected = 'foo'
     self.assertEqual(response, expected)
 def test_recv_all_large(self):
     connection = Connection('host', 1)
     connection._sock = mock.MagicMock()
     buf = io.BytesIO((b'foo' * 1500) + b'\r\n')
     connection._sock.recv = lambda buf_size: buf.read(100)
     response = connection._recv_all()
     expected = ('foo' * 1500) + '\r\n'
     self.assertEqual(response, expected)
Beispiel #3
0
 def _get_connection(self):
     # TODO: Try random trackers, implement retries
     host, port = self._trackers[0]
     connection = Connection(host, port)
     connection._connect()
     return connection
 def test_connect_settimeout(self):
     connection = Connection('host', 1)
     with mock.patch('socket.socket'):
         connection._connect()
         connection._sock.settimeout.assert_called_with(10)
 def test_connect(self):
     connection = Connection('host', 1)
     with mock.patch('socket.socket'):
         connection._connect()
         connection._sock.connect.assert_called_with(('host', 1))
 def test_no_socket_on_instantiation(self):
     connection = Connection('host', 1)
     self.assertFalse(hasattr(connection, '_sock'))
 def test_do_request_requires_request_instance(self):
     connection = Connection('host', 1)
     with self.assertRaises(AssertionError):
         connection.do_request('test')
Beispiel #8
0
 def test_noop(self):
     connection = Connection('host', 1)
     connection._sock = mock.MagicMock()
     buf = io.BytesIO(b'OK\r\n')
     connection._sock.recv = lambda buf_size: buf.read()
     connection.noop()
Beispiel #9
0
 def test_no_socket_on_instantiation(self):
     connection = Connection('host', 1)
     self.assertEqual(None, connection._sock)
Beispiel #10
0
 def __init__(self, trackers):
     self._trackers = [[Connection(*tracker.split(':')), 0]
                       for tracker in trackers]
Beispiel #11
0
def _close_connection_quietly(conn: Connection):
    try:
        conn.close()
    except:
        pass