Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Fix buffer is closed error when using PythonParser class (#1213)
Browse files Browse the repository at this point in the history
* Fix buffer is closed error when using PythonParser class

Fixes #1212
Fixes #1114 

* Fix test_invalid_response on older python versions without AsyncMock
  • Loading branch information
Maksim Novikov committed Nov 22, 2021
1 parent 0aa06df commit 2ba15fb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES/1212.bugfix
@@ -0,0 +1 @@
Fix buffer is closed error when using PythonParser class
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -39,6 +39,7 @@ Jeff Moser
Joongi Kim <achimnol>
Kyrylo Dehtyarenko
Leonid Shvechikov
Maksim Novikov
Manuel Miranda
Marek Szapiel
Marijn Giesen
Expand Down
2 changes: 1 addition & 1 deletion aioredis/connection.py
Expand Up @@ -373,7 +373,7 @@ def __init__(self, socket_read_size: int):
def on_connect(self, connection: "Connection"):
"""Called when the stream connects"""
self._stream = connection._reader
if self._buffer is None or self._stream is None:
if self._stream is None:
raise RedisError("Buffer is closed.")

self._buffer = SocketBuffer(
Expand Down
37 changes: 33 additions & 4 deletions tests/conftest.py
Expand Up @@ -8,7 +8,12 @@

import aioredis
from aioredis.client import Monitor
from aioredis.connection import parse_url
from aioredis.connection import (
HIREDIS_AVAILABLE,
HiredisParser,
PythonParser,
parse_url,
)

from .compat import mock

Expand Down Expand Up @@ -125,16 +130,40 @@ def skip_unless_arch_bits(arch_bits):
)


@pytest.fixture(params=[True, False], ids=["single", "pool"])
@pytest.fixture(
params=[
(True, PythonParser),
(False, PythonParser),
pytest.param(
(True, HiredisParser),
marks=pytest.mark.skipif(
not HIREDIS_AVAILABLE, reason="hiredis is not installed"
),
),
pytest.param(
(False, HiredisParser),
marks=pytest.mark.skipif(
not HIREDIS_AVAILABLE, reason="hiredis is not installed"
),
),
],
ids=[
"single-python-parser",
"pool-python-parser",
"single-hiredis",
"pool-hiredis",
],
)
def create_redis(request, event_loop):
"""Wrapper around aioredis.create_redis."""
single_connection = request.param
single_connection, parser_cls = request.param

async def f(url: str = request.config.getoption("--redis-url"), **kwargs):
single = kwargs.pop("single_connection_client", False) or single_connection
parser_class = kwargs.pop("parser_class", None) or parser_cls
url_options = parse_url(url)
url_options.update(kwargs)
pool = aioredis.ConnectionPool(**url_options)
pool = aioredis.ConnectionPool(parser_class=parser_class, **url_options)
client: aioredis.Redis = aioredis.Redis(connection_pool=pool)
if single:
client = client.client()
Expand Down
17 changes: 9 additions & 8 deletions tests/test_connection.py
@@ -1,23 +1,24 @@
import asyncio
from typing import TYPE_CHECKING
from unittest import mock

import pytest

from aioredis.connection import UnixDomainSocketConnection
from aioredis.connection import PythonParser, UnixDomainSocketConnection
from aioredis.exceptions import InvalidResponse
from aioredis.utils import HIREDIS_AVAILABLE

if TYPE_CHECKING:
from aioredis.connection import PythonParser
from .compat import mock


@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
@pytest.mark.asyncio
async def test_invalid_response(r):
@pytest.mark.parametrize("create_redis", [(True, PythonParser)], indirect=True)
async def test_invalid_response(create_redis):
r = await create_redis()

raw = b"x"
readline_mock = mock.AsyncMock(return_value=raw)

parser: "PythonParser" = r.connection._parser
with mock.patch.object(parser._buffer, "readline", return_value=raw):
with mock.patch.object(parser._buffer, "readline", readline_mock):
with pytest.raises(InvalidResponse) as cm:
await parser.read_response()
assert str(cm.value) == "Protocol Error: %r" % raw
Expand Down

0 comments on commit 2ba15fb

Please sign in to comment.