Multiple coroutines attempting to access the same state. Args: state_manager: A state manager instance. token: A token.
(state_manager: StateManager, token: str)
| 1466 | |
| 1467 | @pytest.mark.asyncio |
| 1468 | async def test_state_manager_contend(state_manager: StateManager, token: str): |
| 1469 | """Multiple coroutines attempting to access the same state. |
| 1470 | |
| 1471 | Args: |
| 1472 | state_manager: A state manager instance. |
| 1473 | token: A token. |
| 1474 | """ |
| 1475 | n_coroutines = 10 |
| 1476 | exp_num1 = 10 |
| 1477 | |
| 1478 | async with state_manager.modify_state(token) as state: |
| 1479 | state.num1 = 0 |
| 1480 | |
| 1481 | async def _coro(): |
| 1482 | async with state_manager.modify_state(token) as state: |
| 1483 | await asyncio.sleep(0.01) |
| 1484 | state.num1 += 1 |
| 1485 | |
| 1486 | tasks = [asyncio.create_task(_coro()) for _ in range(n_coroutines)] |
| 1487 | |
| 1488 | for f in asyncio.as_completed(tasks): |
| 1489 | await f |
| 1490 | |
| 1491 | assert (await state_manager.get_state(token)).num1 == exp_num1 |
| 1492 | |
| 1493 | if isinstance(state_manager, StateManagerRedis): |
| 1494 | assert (await state_manager.redis.get(f"{token}_lock")) is None |
| 1495 | elif isinstance(state_manager, StateManagerMemory): |
| 1496 | assert token in state_manager._states_locks |
| 1497 | assert not state_manager._states_locks[token].locked() |
| 1498 | |
| 1499 | |
| 1500 | @pytest.fixture(scope="function") |
nothing calls this directly
no test coverage detected