47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import asyncio
|
|
import os
|
|
from contextlib import suppress
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from redis.asyncio import Redis
|
|
from redis.exceptions import RedisError
|
|
|
|
from warehouse_allocation.infrastructure.inventory import RedisInventoryRepository
|
|
|
|
|
|
@pytest.mark.redis
|
|
async def test_real_redis_reservation_is_atomic() -> None:
|
|
redis_url = os.getenv("WA_TEST_REDIS_URL")
|
|
if not redis_url:
|
|
pytest.skip("WA_TEST_REDIS_URL is not configured")
|
|
|
|
client = Redis.from_url(redis_url, decode_responses=True)
|
|
repository = RedisInventoryRepository(client)
|
|
warehouse_id = f"ci-{uuid4().hex}"
|
|
key = f"inventory:{warehouse_id}"
|
|
|
|
try:
|
|
for _attempt in range(20):
|
|
try:
|
|
if await client.ping():
|
|
break
|
|
except RedisError:
|
|
await asyncio.sleep(0.25)
|
|
else:
|
|
pytest.fail("Redis integration service did not become ready")
|
|
|
|
await repository.set_levels(warehouse_id, {"ATOMIC-SKU": 5})
|
|
|
|
results = await asyncio.gather(
|
|
repository.reserve(warehouse_id, {"ATOMIC-SKU": 4}),
|
|
repository.reserve(warehouse_id, {"ATOMIC-SKU": 4}),
|
|
)
|
|
|
|
assert sorted(results) == [False, True]
|
|
assert await client.hget(key, "ATOMIC-SKU") == "1"
|
|
finally:
|
|
with suppress(RedisError):
|
|
await client.delete(key)
|
|
await repository.close()
|