Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/google-cloud-firestore/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"pytest-asyncio==0.21.2",
"six",
"pyyaml",
"pytest-xdist",
Comment thread
ohmayr marked this conversation as resolved.
]
SYSTEM_TEST_LOCAL_DEPENDENCIES: List[str] = []
SYSTEM_TEST_DEPENDENCIES: List[str] = []
Expand Down Expand Up @@ -402,6 +403,8 @@ def system(session):
if system_test_exists:
session.run(
"py.test",
"-n",
"auto",
Comment on lines +406 to +407

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When running system tests in parallel with pytest-xdist (-n auto), the default distribution mode is --dist=load, which distributes individual tests to any available worker. This can cause tests from the same module to run on different workers, meaning module-scoped fixtures (like query_docs which was changed to scope="module" in this PR) will be executed multiple times (once per worker), defeating the caching benefit.

To ensure that all tests within a single module run on the same worker and preserve the efficiency of module-scoped fixtures, consider using --dist=loadfile.

Suggested change
"-n",
"auto",
"-n",
"auto",
"--dist",
"loadfile",

"--quiet",
f"--junitxml=system_{session.python}_sponge_log.xml",
system_test_path,
Expand All @@ -410,6 +413,8 @@ def system(session):
if system_test_folder_exists:
session.run(
"py.test",
"-n",
"auto",
Comment on lines +416 to +417

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When running system tests in parallel with pytest-xdist (-n auto), the default distribution mode is --dist=load, which distributes individual tests to any available worker. This can cause tests from the same module to run on different workers, meaning module-scoped fixtures (like query_docs which was changed to scope="module" in this PR) will be executed multiple times (once per worker), defeating the caching benefit.

To ensure that all tests within a single module run on the same worker and preserve the efficiency of module-scoped fixtures, consider using --dist=loadfile.

Suggested change
"-n",
"auto",
"-n",
"auto",
"--dist",
"loadfile",

"--quiet",
f"--junitxml=system_{session.python}_sponge_log.xml",
system_test_folder_path,
Expand Down
6 changes: 3 additions & 3 deletions packages/google-cloud-firestore/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ def test_unicode_doc(client, cleanup, database):
assert snapshot2.reference.id == explicit_doc_id


@pytest.fixture
@pytest.fixture(scope="module")
def query_docs(client, database):
Comment on lines +1271 to 1272

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If the database fixture is function-scoped, defining query_docs with scope="module" will raise a ScopeMismatch error. To prevent this, the database fixture must also be configured with scope="module" or higher, or query_docs should remain function-scoped.

collection_id = "qs" + UNIQUE_RESOURCE_ID
sub_collection = "child" + UNIQUE_RESOURCE_ID
Expand Down Expand Up @@ -1297,13 +1297,13 @@ def query_docs(client, database):
operation()


@pytest.fixture
@pytest.fixture(scope="module")
def collection(query_docs):
collection, _, _ = query_docs
return collection


@pytest.fixture
@pytest.fixture(scope="module")
def query(collection):
return collection.where(filter=FieldFilter("a", "==", 1))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ async def test_list_collections_with_read_time(client, cleanup, database):
}


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="module")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In pytest-asyncio (especially with the pinned version 0.21.2 in noxfile.py), the default event_loop fixture is function-scoped. Defining an async fixture with scope="module" (like query_docs, collection, and async_query) will raise a ScopeMismatch error during test collection or execution.

To resolve this, you must define a module-scoped event_loop fixture in this file or in conftest.py to override the default one.

@pytest.fixture(scope="module")
def event_loop():
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest_asyncio.fixture(scope="module")

async def query_docs(client):
collection_id = "qs" + UNIQUE_RESOURCE_ID
sub_collection = "child" + UNIQUE_RESOURCE_ID
Expand Down Expand Up @@ -1272,13 +1272,13 @@ async def query_docs(client):
await operation()


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="module")
async def collection(query_docs):
collection, _, _ = query_docs
yield collection


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="module")
async def async_query(collection):
return collection.where(filter=FieldFilter("a", "==", 1))

Expand Down
Loading