Skip to content

Commit 6483eda

Browse files
Merge branch 'master' into DOC-4560-trans-pipe-examples
2 parents 4c9c600 + 9d5751d commit 6483eda

File tree

7 files changed

+55
-24
lines changed

7 files changed

+55
-24
lines changed

redis/_parsers/helpers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,20 @@ def parse_item(item):
396396
# an O(N) complexity) instead of the command.
397397
if isinstance(item[3], list):
398398
result["command"] = space.join(item[3])
399-
result["client_address"] = item[4]
400-
result["client_name"] = item[5]
399+
400+
# These fields are optional, depends on environment.
401+
if len(item) >= 6:
402+
result["client_address"] = item[4]
403+
result["client_name"] = item[5]
401404
else:
402405
result["complexity"] = item[3]
403406
result["command"] = space.join(item[4])
404-
result["client_address"] = item[5]
405-
result["client_name"] = item[6]
407+
408+
# These fields are optional, depends on environment.
409+
if len(item) >= 7:
410+
result["client_address"] = item[5]
411+
result["client_name"] = item[6]
412+
406413
return result
407414

408415
return [parse_item(item) for item in response]

redis/asyncio/connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,13 @@ def __del__(self, _warnings: Any = warnings):
214214
_warnings.warn(
215215
f"unclosed Connection {self!r}", ResourceWarning, source=self
216216
)
217-
self._close()
217+
218+
try:
219+
asyncio.get_running_loop()
220+
self._close()
221+
except RuntimeError:
222+
# No actions been taken if pool already closed.
223+
pass
218224

219225
def _close(self):
220226
"""

redis/asyncio/sentinel.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ def __init__(self, **kwargs):
2929
super().__init__(**kwargs)
3030

3131
def __repr__(self):
32-
pool = self.connection_pool
33-
s = (
34-
f"<{self.__class__.__module__}.{self.__class__.__name__}"
35-
f"(service={pool.service_name}"
36-
)
32+
s = f"<{self.__class__.__module__}.{self.__class__.__name__}"
3733
if self.host:
3834
host_info = f",host={self.host},port={self.port}"
3935
s += host_info

tests/test_asyncio/test_hash.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import math
23
from datetime import datetime, timedelta
34

45
from tests.conftest import skip_if_server_version_lt
@@ -128,9 +129,9 @@ async def test_hpexpire_multiple_fields(r):
128129
async def test_hexpireat_basic(r):
129130
await r.delete("test:hash")
130131
await r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
131-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
132+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
132133
assert await r.hexpireat("test:hash", exp_time, "field1") == [1]
133-
await asyncio.sleep(1.1)
134+
await asyncio.sleep(2.1)
134135
assert await r.hexists("test:hash", "field1") is False
135136
assert await r.hexists("test:hash", "field2") is True
136137

@@ -139,9 +140,9 @@ async def test_hexpireat_basic(r):
139140
async def test_hexpireat_with_datetime(r):
140141
await r.delete("test:hash")
141142
await r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
142-
exp_time = datetime.now() + timedelta(seconds=1)
143+
exp_time = (datetime.now() + timedelta(seconds=2)).replace(microsecond=0)
143144
assert await r.hexpireat("test:hash", exp_time, "field1") == [1]
144-
await asyncio.sleep(1.1)
145+
await asyncio.sleep(2.1)
145146
assert await r.hexists("test:hash", "field1") is False
146147
assert await r.hexists("test:hash", "field2") is True
147148

@@ -175,9 +176,9 @@ async def test_hexpireat_multiple_fields(r):
175176
"test:hash",
176177
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
177178
)
178-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
179+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
179180
assert await r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
180-
await asyncio.sleep(1.5)
181+
await asyncio.sleep(2.1)
181182
assert await r.hexists("test:hash", "field1") is False
182183
assert await r.hexists("test:hash", "field2") is False
183184
assert await r.hexists("test:hash", "field3") is True

tests/test_asyncio/test_sentinel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,23 @@ async def mock_disconnect():
264264

265265
assert calls == 1
266266
await pool.disconnect()
267+
268+
269+
@pytest.mark.onlynoncluster
270+
async def test_repr_correctly_represents_connection_object(sentinel):
271+
pool = SentinelConnectionPool("mymaster", sentinel)
272+
connection = await pool.get_connection("PING")
273+
274+
assert (
275+
str(connection)
276+
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
277+
)
278+
assert connection.connection_pool == pool
279+
await pool.release(connection)
280+
281+
del pool
282+
283+
assert (
284+
str(connection)
285+
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
286+
)

tests/test_hash.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import time
23
from datetime import datetime, timedelta
34

@@ -147,9 +148,9 @@ def test_hpexpire_multiple_condition_flags_error(r):
147148
def test_hexpireat_basic(r):
148149
r.delete("test:hash")
149150
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
150-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
151+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
151152
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
152-
time.sleep(1.1)
153+
time.sleep(2.1)
153154
assert r.hexists("test:hash", "field1") is False
154155
assert r.hexists("test:hash", "field2") is True
155156

@@ -158,9 +159,9 @@ def test_hexpireat_basic(r):
158159
def test_hexpireat_with_datetime(r):
159160
r.delete("test:hash")
160161
r.hset("test:hash", mapping={"field1": "value1", "field2": "value2"})
161-
exp_time = datetime.now() + timedelta(seconds=1)
162+
exp_time = (datetime.now() + timedelta(seconds=2)).replace(microsecond=0)
162163
assert r.hexpireat("test:hash", exp_time, "field1") == [1]
163-
time.sleep(1.1)
164+
time.sleep(2.1)
164165
assert r.hexists("test:hash", "field1") is False
165166
assert r.hexists("test:hash", "field2") is True
166167

@@ -194,9 +195,9 @@ def test_hexpireat_multiple_fields(r):
194195
"test:hash",
195196
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
196197
)
197-
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
198+
exp_time = math.ceil((datetime.now() + timedelta(seconds=1)).timestamp())
198199
assert r.hexpireat("test:hash", exp_time, "field1", "field2") == [1, 1]
199-
time.sleep(1.1)
200+
time.sleep(2.1)
200201
assert r.hexists("test:hash", "field1") is False
201202
assert r.hexists("test:hash", "field2") is False
202203
assert r.hexists("test:hash", "field3") is True

tests/test_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,8 @@ def test_set_path(client):
15211521

15221522
root = tempfile.mkdtemp()
15231523
sub = tempfile.mkdtemp(dir=root)
1524-
jsonfile = tempfile.mktemp(suffix=".json", dir=sub)
1525-
nojsonfile = tempfile.mktemp(dir=root)
1524+
jsonfile = tempfile.mkstemp(suffix=".json", dir=sub)[1]
1525+
nojsonfile = tempfile.mkstemp(dir=root)[1]
15261526

15271527
with open(jsonfile, "w+") as fp:
15281528
fp.write(json.dumps({"hello": "world"}))

0 commit comments

Comments
 (0)