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)
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 _get_connection(self): # TODO: Try random trackers, implement retries host, port = self._trackers[0] connection = Connection(host, port) connection._connect() return connection
def test_no_socket_on_instantiation(self): connection = Connection('host', 1) self.assertFalse(hasattr(connection, '_sock'))
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_do_request_requires_request_instance(self): connection = Connection('host', 1) with self.assertRaises(AssertionError): connection.do_request('test')
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()
def test_no_socket_on_instantiation(self): connection = Connection('host', 1) self.assertEqual(None, connection._sock)
def __init__(self, trackers): self._trackers = [[Connection(*tracker.split(':')), 0] for tracker in trackers]