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
Browse files Browse the repository at this point in the history
Fixes: #1212
  • Loading branch information
m-novikov committed Nov 20, 2021
1 parent 33b2dbd commit 2bcb685
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 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 @@ -38,6 +38,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
3 changes: 3 additions & 0 deletions tests/test_connection.py
Expand Up @@ -15,6 +15,9 @@
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
@pytest.mark.asyncio
async def test_invalid_response(r):
if r.connection is None:
raise pytest.skip("single connection only")

raw = b"x"
parser: "PythonParser" = r.connection._parser
with mock.patch.object(parser._buffer, "readline", return_value=raw):
Expand Down

0 comments on commit 2bcb685

Please sign in to comment.