Skip to content

Commit

Permalink
Merge pull request #291 from shachlanAmazon/py-transaction
Browse files Browse the repository at this point in the history
Fix python's transaction API.
  • Loading branch information
shachlanAmazon authored Jun 26, 2023
2 parents 5f6cc1f + 035d815 commit fdd4250
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
13 changes: 5 additions & 8 deletions python/python/pybushka/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ def __init__(self) -> None:
self.commands = []
self.lock = threading.Lock()

def execute_command(self, request_type: RequestType, args: List[str]):
def append_command(self, request_type: RequestType, args: List[str]):
self.lock.acquire()
try:
self.commands.append([request_type, args])
finally:
self.lock.release()

def get(self, key: str):
self.execute_command(RequestType.GetString, [key])
self.append_command(RequestType.GetString, [key])

def set(
self,
Expand All @@ -134,7 +134,7 @@ def set(
args.append("GET")
if expiry is not None:
args.extend(expiry.get_cmd_args())
self.execute_command(RequestType.SetString, args)
self.append_command(RequestType.SetString, args)

def custom_command(self, command_args: List[str]):
"""Executes a single command, without checking inputs.
Expand All @@ -148,7 +148,7 @@ def custom_command(self, command_args: List[str]):
Returns:
TResult: The returning value depends on the executed command
"""
self.execute_command(RequestType.CustomCommand, command_args)
self.append_command(RequestType.CustomCommand, command_args)

def dispose(self):
with self.lock:
Expand Down Expand Up @@ -210,13 +210,10 @@ async def get(self, key: str) -> Union[str, None]:
"""
return await self.execute_command(RequestType.GetString, [key])

async def multi(self) -> Transaction:
return Transaction()

async def exec(self, transaction: Transaction) -> List[Union[str, None, TResult]]:
commands = transaction.commands[:]
transaction.dispose()
return await self.execute_command_Transaction(commands)
return await self.execute_transaction(commands)

async def custom_command(self, command_args: List[str]) -> TResult:
"""Executes a single command, without checking inputs.
Expand Down
2 changes: 1 addition & 1 deletion python/python/pybushka/async_socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async def execute_command(
await response_future
return response_future.result()

async def execute_command_Transaction(
async def execute_transaction(
self, commands: List[Union[TRequestType, List[str]]]
) -> TResult:
request = RedisRequest()
Expand Down
21 changes: 13 additions & 8 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

import pytest
from packaging import version
from pybushka.async_commands.core import ConditionalSet, ExpirySet, ExpiryType
from pybushka.async_commands.core import (
ConditionalSet,
ExpirySet,
ExpiryType,
Transaction,
)
from pybushka.async_ffi_client import RedisAsyncFFIClient
from pybushka.async_socket_client import RedisAsyncSocketClient
from pybushka.async_socket_cluster_client import RedisClusterAsyncSocket
Expand Down Expand Up @@ -204,7 +209,7 @@ async def test_transaction_set_get(
):
key = get_random_string(10)
value = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.set(key, value)
transaction.get(key)
result = await async_socket_client.exec(transaction)
Expand All @@ -217,7 +222,7 @@ async def test_transaction_multiple_commands(
key = get_random_string(10)
key2 = "{{{}}}:{}".format(key, get_random_string(3)) # to get the same slot
value = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.set(key, value)
transaction.get(key)
transaction.set(key2, value)
Expand All @@ -229,7 +234,7 @@ async def test_transaction_multiple_commands(
async def test_transaction_with_different_slots(
self, async_socket_client: RedisAsyncSocketClient
):
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.set("key1", "value1")
transaction.set("key2", "value2")
with pytest.raises(Exception) as e:
Expand All @@ -241,7 +246,7 @@ async def test_transaction_custom_command(
self, async_socket_client: RedisAsyncSocketClient
):
key = get_random_string(10)
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.custom_command(["HSET", key, "foo", "bar"])
transaction.custom_command(["HGET", key, "foo"])
result = await async_socket_client.exec(transaction)
Expand All @@ -252,7 +257,7 @@ async def test_transaction_custom_unsupported_command(
self, async_socket_client: RedisAsyncSocketClient
):
key = get_random_string(10)
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.custom_command(["WATCH", key])
with pytest.raises(Exception) as e:
await async_socket_client.exec(transaction)
Expand All @@ -266,7 +271,7 @@ async def test_transaction_discard_command(
):
key = get_random_string(10)
await async_socket_client.set(key, "1")
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.custom_command(["INCR", key])
transaction.custom_command(["DISCARD"])
with pytest.raises(Exception) as e:
Expand All @@ -280,7 +285,7 @@ async def test_transaction_exec_abort(
self, async_socket_client: RedisAsyncSocketClient
):
key = get_random_string(10)
transaction = await async_socket_client.multi()
transaction = Transaction()
transaction.custom_command(["INCR", key, key, key])
with pytest.raises(Exception) as e:
await async_socket_client.exec(transaction)
Expand Down

0 comments on commit fdd4250

Please sign in to comment.