-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable multiprocess (xdist) use of the redis fixture.
- Loading branch information
Showing
8 changed files
with
95 additions
and
8 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
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pytest_mock_resources import create_redis_fixture | ||
|
||
redis = create_redis_fixture() |
47 changes: 47 additions & 0 deletions
47
tests/examples/test_multiprocess_redis_database/test_split.py
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Produce a test example which would induce a redis database related race condition. | ||
The premise is that given a pytest invocation: `pytest -n 4 test_split.py`, | ||
multiple processes would start up simultaneously, and all connecting to the same | ||
redis database. | ||
The tests would then proceed to clobber one another's values, leading to flaky | ||
tests. | ||
A correct implementation would use some mechanism to avoid this inter-parallel-test | ||
key conflict problem. | ||
""" | ||
import random | ||
import time | ||
|
||
|
||
def test_node_one(redis, pytestconfig): | ||
run_test(redis, pytestconfig) | ||
|
||
|
||
def test_node_two(redis, pytestconfig): | ||
run_test(redis, pytestconfig) | ||
|
||
|
||
def test_node_three(redis, pytestconfig): | ||
run_test(redis, pytestconfig) | ||
|
||
|
||
def test_node_four(redis, pytestconfig): | ||
run_test(redis, pytestconfig) | ||
|
||
|
||
def run_test(redis, pytestconfig): | ||
worker_id = int(pytestconfig.workerinput['workerid'][2:]) | ||
database = redis.connection_pool.get_connection('set').db | ||
assert worker_id == database | ||
print(worker_id, database) | ||
|
||
redis.set("foo", "bar") | ||
# XXX: Ideally we would sleep random times to ensure we're hitting the problem, | ||
# XXX: however until the plugin is overall more process-safe, it's too flaky. | ||
# time.sleep(random.randrange(1, 10) / 10) | ||
value = redis.get("foo") | ||
|
||
assert value == b"bar" | ||
|
||
redis.flushdb() |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import pytest | ||
|
||
|
||
@pytest.mark.redis | ||
def test_multiprocess_redis_database(pytester): | ||
pytester.copy_example() | ||
|
||
# The `-n 2` are here is tightly coupled with the implementation of `test_split.py`. | ||
args = ["-vv", "-n", "4", "test_split.py"] | ||
result = pytester.inline_run(*args) | ||
result.assertoutcome(passed=4, skipped=0, failed=0) |