Config locks (#2654)

* Config locks

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>

* Add locks for all_XXX

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>

* Remove a word

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>

* Add acquire_lock kwarg for value context manager

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>

* Add towncrier entry

Signed-off-by: Toby <tobyharradine@gmail.com>

* Fix issues with `get_custom_lock` and `get_members_lock`

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
This commit is contained in:
Toby Harradine
2019-07-24 06:50:07 +10:00
committed by Michael H
parent a8091332b8
commit af096bc1cc
4 changed files with 241 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
import asyncio
from unittest.mock import patch
import pytest
@@ -506,3 +507,50 @@ def test_config_custom_doubleinit(config):
config.init_custom("TEST", 3)
with pytest.raises(ValueError):
config.init_custom("TEST", 2)
@pytest.mark.asyncio
async def test_config_locks_cache(config, empty_guild):
lock1 = config.foo.get_lock()
assert lock1 is config.foo.get_lock()
lock2 = config.guild(empty_guild).foo.get_lock()
assert lock2 is config.guild(empty_guild).foo.get_lock()
assert lock1 is not lock2
@pytest.mark.asyncio
async def test_config_value_atomicity(config):
config.register_global(foo=[])
tasks = []
for _ in range(15):
async def func():
async with config.foo.get_lock():
foo = await config.foo()
foo.append(0)
await asyncio.sleep(0.1)
await config.foo.set(foo)
tasks.append(func())
await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
assert len(await config.foo()) == 15
@pytest.mark.asyncio
async def test_config_ctxmgr_atomicity(config):
config.register_global(foo=[])
tasks = []
for _ in range(15):
async def func():
async with config.foo() as foo:
foo.append(0)
await asyncio.sleep(0.1)
tasks.append(func())
await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
assert len(await config.foo()) == 15