Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Random Crash] Usually during unittests #472

Open
DEKHTIARJonathan opened this issue Dec 20, 2024 · 1 comment
Open

[Random Crash] Usually during unittests #472

DEKHTIARJonathan opened this issue Dec 20, 2024 · 1 comment

Comments

@DEKHTIARJonathan
Copy link

DEKHTIARJonathan commented Dec 20, 2024

I'm having a weird crash - totally random and hard to reproduce:

tests/factories.py:68: in ProjectFactory
    country = random.choice(countries)[0]
/opt/hostedtoolcache/Python/3.13.1/x64/lib/python3.13/random.py:352: in choice
    return seq[self._randbelow(len(seq))]
/opt/hostedtoolcache/Python/3.13.1/x64/lib/python3.13/site-packages/django_countries/__init__.py:588: in __getitem__
    return next(itertools.islice(self.__iter__(), index, index + 1))
E   StopIteration

Probably means that self.__iter__() returns an empty result for some reasons.
I believe it may happen when something doesn't get initialized. No idea what and why.

Any idea what the issue could be ?

@DEKHTIARJonathan
Copy link
Author

DEKHTIARJonathan commented Jan 9, 2025

In case someone else has the issue, I haven't found the reason, but an additional "sleep" at the initialization addresses the issue:

  • conftest.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time

import pytest


# Note: This wait time is necessary because of django-countries sometimes being not
# ready. Simple workaround to fix the issue and barely noticeable.
def pytest_sessionstart(session):
    """Hook to delay the start of the pytest session."""
    initial_wait_time = 0.2  # seconds
    print(f"Waiting for {initial_wait_time} seconds before starting pytest session...")  # noqa: T201
    time.sleep(initial_wait_time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant