Skip to content

Commit c902ac7

Browse files
committed
add close() method to sync Pipeline
1 parent fd08589 commit c902ac7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

redis/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,10 @@ def reset(self):
11921192
self.connection_pool.release(self.connection)
11931193
self.connection = None
11941194

1195+
def close(self):
1196+
"""Close the pipeline"""
1197+
self.reset()
1198+
11951199
def multi(self):
11961200
"""
11971201
Start a transactional block of the pipeline after WATCH commands

tests/test_pipeline.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from contextlib import closing
2+
from unittest import mock
3+
14
import pytest
25
import redis
36

@@ -284,6 +287,24 @@ def test_watch_reset_unwatch(self, r):
284287
assert unwatch_command is not None
285288
assert unwatch_command["command"] == "UNWATCH"
286289

290+
@pytest.mark.onlynoncluster
291+
def test_close_is_reset(self, r):
292+
with r.pipeline() as pipe:
293+
called = 0
294+
295+
def mock_reset():
296+
nonlocal called
297+
called += 1
298+
299+
with mock.patch.object(pipe, "reset", mock_reset):
300+
pipe.close()
301+
assert called == 1
302+
303+
@pytest.mark.onlynoncluster
304+
def test_closing(self, r):
305+
with closing(r.pipeline()):
306+
pass
307+
287308
@pytest.mark.onlynoncluster
288309
def test_transaction_callable(self, r):
289310
r["a"] = 1

0 commit comments

Comments
 (0)