Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change pipeline behaviour to execute all commands at the end of the block #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aredis/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.reset()
await self.execute() # also resets

def __len__(self):
return len(self.command_stack)
Expand Down
6 changes: 3 additions & 3 deletions docs/source/pipelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Pipelines are quite simple to use:
async with await client.pipeline() as pipe:
await pipe.delete('bar')
await pipe.set('bar', 'foo')
await pipe.execute() # needs to be called explicitly
# pipe.execute() is called when `with` block is exited


Here are more examples:
Expand All @@ -30,7 +30,7 @@ Here are more examples:
await pipe.set('bar', 'foo')
# commands will be buffered
await pipe.keys('*')
res = await pipe.execute()
res = await pipe.execute() # call explicitly to retrieve results
# results should be in order corresponding to your command
assert res == [True, True, True, [b'bar', b'foo']]

Expand Down Expand Up @@ -104,7 +104,7 @@ explicitly calling reset():
... try:
... await pipe.watch('OUR-SEQUENCE-KEY')
... ...
... await pipe.execute()
... await pipe.execute() # trigger any WatchError early
... break
... except WatchError:
... continue
Expand Down
14 changes: 14 additions & 0 deletions tests/client/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ async def test_pipeline_length(self, r):
assert len(pipe) == 0
assert not pipe

@pytest.mark.asyncio(forbid_global_loop=True)
async def test_pipeline_autoexecute(self, r):
await r.flushdb()
async with await r.pipeline() as pipe:
# Fill 'er up!
await pipe.set('d', 'd1')
await pipe.set('e', 'e1')
await pipe.set('f', 'f1')
assert len(pipe) == 3
assert pipe

# exiting with block calls execute() and reset(), so empty once again
assert len(pipe) == 0

@pytest.mark.asyncio(forbid_global_loop=True)
async def test_pipeline_no_transaction(self, r):
await r.flushdb()
Expand Down