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

Commit 2ba15fb

Browse files
author
Maksim Novikov
authored
Fix buffer is closed error when using PythonParser class (#1213)
* Fix buffer is closed error when using PythonParser class Fixes #1212 Fixes #1114 * Fix test_invalid_response on older python versions without AsyncMock
1 parent 0aa06df commit 2ba15fb

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

CHANGES/1212.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix buffer is closed error when using PythonParser class

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Jeff Moser
3939
Joongi Kim <achimnol>
4040
Kyrylo Dehtyarenko
4141
Leonid Shvechikov
42+
Maksim Novikov
4243
Manuel Miranda
4344
Marek Szapiel
4445
Marijn Giesen

aioredis/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def __init__(self, socket_read_size: int):
373373
def on_connect(self, connection: "Connection"):
374374
"""Called when the stream connects"""
375375
self._stream = connection._reader
376-
if self._buffer is None or self._stream is None:
376+
if self._stream is None:
377377
raise RedisError("Buffer is closed.")
378378

379379
self._buffer = SocketBuffer(

tests/conftest.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import aioredis
1010
from aioredis.client import Monitor
11-
from aioredis.connection import parse_url
11+
from aioredis.connection import (
12+
HIREDIS_AVAILABLE,
13+
HiredisParser,
14+
PythonParser,
15+
parse_url,
16+
)
1217

1318
from .compat import mock
1419

@@ -125,16 +130,40 @@ def skip_unless_arch_bits(arch_bits):
125130
)
126131

127132

128-
@pytest.fixture(params=[True, False], ids=["single", "pool"])
133+
@pytest.fixture(
134+
params=[
135+
(True, PythonParser),
136+
(False, PythonParser),
137+
pytest.param(
138+
(True, HiredisParser),
139+
marks=pytest.mark.skipif(
140+
not HIREDIS_AVAILABLE, reason="hiredis is not installed"
141+
),
142+
),
143+
pytest.param(
144+
(False, HiredisParser),
145+
marks=pytest.mark.skipif(
146+
not HIREDIS_AVAILABLE, reason="hiredis is not installed"
147+
),
148+
),
149+
],
150+
ids=[
151+
"single-python-parser",
152+
"pool-python-parser",
153+
"single-hiredis",
154+
"pool-hiredis",
155+
],
156+
)
129157
def create_redis(request, event_loop):
130158
"""Wrapper around aioredis.create_redis."""
131-
single_connection = request.param
159+
single_connection, parser_cls = request.param
132160

133161
async def f(url: str = request.config.getoption("--redis-url"), **kwargs):
134162
single = kwargs.pop("single_connection_client", False) or single_connection
163+
parser_class = kwargs.pop("parser_class", None) or parser_cls
135164
url_options = parse_url(url)
136165
url_options.update(kwargs)
137-
pool = aioredis.ConnectionPool(**url_options)
166+
pool = aioredis.ConnectionPool(parser_class=parser_class, **url_options)
138167
client: aioredis.Redis = aioredis.Redis(connection_pool=pool)
139168
if single:
140169
client = client.client()

tests/test_connection.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import asyncio
22
from typing import TYPE_CHECKING
3-
from unittest import mock
43

54
import pytest
65

7-
from aioredis.connection import UnixDomainSocketConnection
6+
from aioredis.connection import PythonParser, UnixDomainSocketConnection
87
from aioredis.exceptions import InvalidResponse
9-
from aioredis.utils import HIREDIS_AVAILABLE
108

11-
if TYPE_CHECKING:
12-
from aioredis.connection import PythonParser
9+
from .compat import mock
1310

1411

15-
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
1612
@pytest.mark.asyncio
17-
async def test_invalid_response(r):
13+
@pytest.mark.parametrize("create_redis", [(True, PythonParser)], indirect=True)
14+
async def test_invalid_response(create_redis):
15+
r = await create_redis()
16+
1817
raw = b"x"
18+
readline_mock = mock.AsyncMock(return_value=raw)
19+
1920
parser: "PythonParser" = r.connection._parser
20-
with mock.patch.object(parser._buffer, "readline", return_value=raw):
21+
with mock.patch.object(parser._buffer, "readline", readline_mock):
2122
with pytest.raises(InvalidResponse) as cm:
2223
await parser.read_response()
2324
assert str(cm.value) == "Protocol Error: %r" % raw

0 commit comments

Comments
 (0)