Skip to content

Flaky test in test suite makes CI randomly failing due to query_counter context manager #1869

@bagerard

Description

@bagerard

The test def test_no_cached_queryset(self): Is failing from time to time which is not handy as it makes the CI failing for unrelated reasons in PRs (e.g: #1766).

def test_no_cached_queryset(self):
class Person(Document):
name = StringField()
Person.drop_collection()
for i in range(100):
Person(name="No: %s" % i).save()
with query_counter() as q:
self.assertEqual(q, 0)
people = Person.objects.no_cache()

The issue occurs because the query_counter() context was catching queries that aren't related to this particular test.

I managed to reproduce the issue after adding some debugging code and running travis in my fork (https://travis-ci.com/bagerard/mongoengine/jobs/143421029). The additional query being counted is actually a 'killcursors' query, probably being issued after the garbage collector closes another pymongo cursors.

The query_counter context manager is very handy for debugging but since it is not only counting the queries being issued within the context (other threads or processes can pollute it), it is not very robust.

I'll push a PR that makes the query_counter context ignoring 'killcursors' queries to fix this (and add a parameter ignore_query to the query_counter to let users customize the behavior if needed). Let me know if you have other ideas

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions