def test__connect_ipv6(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='::1') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() sock_mock.settimeout.assert_called_with(2)
def test__close(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock connection = pyredis.connection.Connection(unix_sock='/tmp/test.sock') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() connection.close() sock_mock.close.assert_called_with() self.assertIsNone(connection._sock) self.assertIsNone(connection._reader) self.assertTrue(connection._closed)
def test__connect_ipv6(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='::1') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() sock_mock.settimeout.assert_called_with(2) connection._authenticate.assertcalledwith() connection._setdb.assertcalledwith()
def test_read_exception_connection_lost(self): sock_mock = Mock() sock_mock.recv.side_effect = [''] self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() pyredis.connection.Reader = Reader connection._connect() self.assertRaises(PyRedisConnClosed, connection.read) self.assertTrue(connection.closed)
def test__connect_unix(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(unix_sock='/tmp/test.sock') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() sock_mock.settimeout.assert_called_with(2) self.assertEqual(connection._sock, self.socket_mock.socket()) connection._authenticate.assertcalledwith() connection._setdb.assertcalledwith()
def test_read_exception_socket_timeout_close_on_timeout_false(self): sock_mock = Mock() sock_mock.recv.side_effect = [socket.timeout] self.socket_mock.socket.return_value = sock_mock self.socket_mock.timeout = socket.timeout reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() pyredis.connection.Reader = Reader connection._connect() self.assertRaises(PyRedisConnReadTimeout, connection.read, close_on_timeout=False) self.assertFalse(connection.closed)
def test__connect_ipv4_encoding_utf_8(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() sock_mock.settimeout.assert_called_with(2) self.assertEqual(connection._sock, self.socket_mock.socket()) self.assertEqual(connection._encoding, 'utf-8') self.assertEqual(connection._reader, reader_mock) self.reader_mock.assert_called_with(encoding='utf-8')
def test_write_exception_brokenpipeerror(self): cmd = 'ECHO' payload = "x" * 512 sock_mock = Mock() sock_mock.send.side_effect = [BrokenPipeError] self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() self.assertRaises(PyRedisConnError, connection.write, cmd, payload)
def test_write_exception_brokenpipeerror(self): cmd = 'ECHO' payload = "x" * 512 sock_mock = Mock() sock_mock.sendall.side_effect = [BrokenPipeError] self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() self.assertRaises(PyRedisConnError, connection.write, cmd, payload)
def test_authenticate_sentinel(self): connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8', sentinel=True) authenticate_mock = Mock() setdb_mock = Mock() set_read_only_mock = Mock() connection._authenticate = authenticate_mock connection._setdb = setdb_mock connection._set_read_only = set_read_only_mock connection._connect() authenticate_mock.assert_called() setdb_mock.assert_not_called() set_read_only_mock.assert_not_called()
def test_read_one_chunk_two_messages(self): raw_answer = b'$10\r\nXXXXXXXXXX\r\n$10\r\nYYYYYYYYYY\r\n' answer = 'XXXXXXXXXX' sock_mock = Mock() sock_mock.recv.side_effect = [raw_answer] self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() pyredis.connection.Reader = Reader connection._connect() result = connection.read() self.assertEqual(result, answer)
def test__connect_ipv4_encoding_utf_8(self): sock_mock = Mock() self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() connection._connect() sock_mock.settimeout.assert_called_with(2) self.assertEqual(connection._sock, self.socket_mock.socket()) self.assertEqual(connection._encoding, 'utf-8') self.assertEqual(connection._reader, reader_mock) self.reader_mock.assert_called_with(encoding='utf-8') connection._authenticate.assertcalledwith() connection._setdb.assertcalledwith()
def test_read_message_ready_from_previous_chunck(self): raw_answer = b'$10\r\nXXXXXXXXXX\r\n$10\r\nYYYYYYYYYY\r\n' answer1 = 'XXXXXXXXXX' answer2 = 'YYYYYYYYYY' sock_mock = Mock() sock_mock.recv.side_effect = [raw_answer] self.socket_mock.socket.return_value = sock_mock reader_mock = Mock() self.reader_mock.return_value = reader_mock connection = pyredis.connection.Connection(host='127.0.0.1', encoding='utf-8') connection._authenticate = Mock() connection._setdb = Mock() pyredis.connection.Reader = Reader connection._connect() result1 = connection.read() result2 = connection.read() self.assertEqual(result1, answer1) self.assertEqual(result2, answer2) self.assertEqual(sock_mock.recv.call_args_list, [call(1500)])