-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from mreiche/bugfix/thread-safety
change() is now thread safe using thread-local Config() class
- Loading branch information
Showing
2 changed files
with
64 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,55 @@ | ||
import asyncio | ||
import dataclasses | ||
from time import sleep | ||
|
||
from paf.control import change, __get_global_config, retry | ||
import pytest | ||
|
||
from paf.common import Property | ||
from paf.control import change, get_config, retry | ||
|
||
def test_config(): | ||
|
||
backup = dataclasses.replace(__get_global_config()) | ||
assert backup.retry_count != 99 | ||
assert backup.wait_after_fail != 99 | ||
def test_change(): | ||
|
||
backup_config = dataclasses.replace(get_config()) | ||
assert backup_config.retry_count != 99 | ||
assert backup_config.wait_after_fail != 99 | ||
|
||
with change(retry_count=99, wait_after_fail=99): | ||
global_config = __get_global_config() | ||
global_config = get_config() | ||
assert global_config.wait_after_fail == 99 | ||
assert global_config.retry_count == 99 | ||
retry(lambda: None) | ||
|
||
global_config = __get_global_config() | ||
assert global_config.retry_count == backup.retry_count | ||
assert global_config.wait_after_fail == backup.wait_after_fail | ||
global_config = get_config() | ||
assert global_config.retry_count == backup_config.retry_count | ||
assert global_config.wait_after_fail == backup_config.wait_after_fail | ||
|
||
|
||
def change_first(): | ||
global_config = get_config() | ||
assert global_config.retry_count == Property.env(Property.PAF_SEQUENCE_RETRY_COUNT) | ||
|
||
with change(retry_count=0): | ||
sleep(0.3) | ||
config = get_config() | ||
assert config.retry_count == 0 | ||
|
||
|
||
def change_second(): | ||
global_config = get_config() | ||
assert global_config.retry_count == Property.env(Property.PAF_SEQUENCE_RETRY_COUNT) | ||
|
||
with change(retry_count=99): | ||
sleep(0.1) | ||
config = get_config() | ||
assert config.retry_count == 99 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_thread_safety(): | ||
tasks = [ | ||
asyncio.to_thread(change_first), | ||
asyncio.to_thread(change_second) | ||
] | ||
|
||
await asyncio.gather(*tasks) |