Skip to content

test: replace sleeps with freezegun time ticks in tests#957

Open
bndikt wants to merge 1 commit into
DIRACGrid:mainfrom
bndikt:replace-sleeps-freezegun
Open

test: replace sleeps with freezegun time ticks in tests#957
bndikt wants to merge 1 commit into
DIRACGrid:mainfrom
bndikt:replace-sleeps-freezegun

Conversation

@bndikt

@bndikt bndikt commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • replace wall-clock sleeps in tests with freezegun time ticks
  • wire SQLite test connections to mock_sqlite_time so DATETIME('now') follows frozen Python time
  • freeze tests at the current UTC second to match SQLite timestamp precision

@bndikt bndikt changed the title Replace sleeps with freezegun time ticks in tests test: Replace sleeps with freezegun time ticks in tests Jun 23, 2026
@bndikt bndikt changed the title test: Replace sleeps with freezegun time ticks in tests test: replace sleeps with freezegun time ticks in tests Jun 23, 2026
@bndikt bndikt linked an issue Jun 23, 2026 that may be closed by this pull request
yield sandbox_metadata_db


@pytest.fixture

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As this is defined in two places it probably belongs in diracx-testing/src/diracx/testing/time.py

db.engine.sync_engine, "connect", set_sqlite_pragma
)
sqlalchemy.event.listen(
db.engine.sync_engine, "connect", mock_sqlite_time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similarly, this is now in three places, maybe it can be simplified with a helper:

# diracx-testing/src/diracx/testing/time.py
def install_sqlite_time_mock(engine) -> None:
    """Make SQLite's date/time functions follow frozen Python time on `engine`.

    Registers `mock_sqlite_time` as a "connect" listener so every new DBAPI
    connection gets the mocked DATETIME()/JULIANDAY()/... functions. Accepts a
    sync Engine or an AsyncEngine.
    """
    sync_engine = getattr(engine, "sync_engine", engine)
    sqlalchemy.event.listen(sync_engine, "connect", mock_sqlite_time)

# utils.py:339  (create_db_schemas)
install_sqlite_time_mock(db.engine)

# test_sandbox_metadata.py:21  (sandbox_metadata_db fixture)
install_sqlite_time_mock(sandbox_metadata_db.engine)

# test_freeze_time.py:43
if with_mock:
    install_sqlite_time_mock(engine)

@DIRACGridBot DIRACGridBot marked this pull request as draft June 23, 2026 21:04
@read-the-docs-community

read-the-docs-community Bot commented Jun 24, 2026

Copy link
Copy Markdown

Documentation build overview

📚 diracx | 🛠️ Build #33307984 | 📁 Comparing 9515104 against latest (8b944bf)

  🔍 Preview build  

1 file changed
± dev/reference/pixi-tasks/index.html

@bndikt bndikt force-pushed the replace-sleeps-freezegun branch 2 times, most recently from bb01425 to 0361f1e Compare June 24, 2026 15:47
@bndikt bndikt marked this pull request as ready for review June 24, 2026 19:54
@aldbr

aldbr commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

@chrisburr I noticed freezegun and sqlalchemy are used within diracx-testing but not part of the pyproject.toml. I assume this was forgotten, unless I miss something here?

@aldbr

aldbr commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

ddev: usually we add the import at the top of the modules, we should probably add an explicit rule about that (guidelines for devs)

test: centralize freezegun mock time helpers

test: add time helper dependencies to testing package
@bndikt bndikt force-pushed the replace-sleeps-freezegun branch from 0361f1e to 9515104 Compare June 25, 2026 13:02
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

Successfully merging this pull request may close these issues.

Don't use time.sleep in tests to avoid unique constraint exceptions

3 participants