gradio/test/conftest.py
Freddy Boulton 6a4bf7abe2
Delete user state when they close the tab. Add an unload event for the demo and a delete_callback on gr.State to let developers control how resources are cleaned up (#7829)
* Delete state

* add changeset

* Delete state

* WIP

* Add load event

* Working ttl

* unload e2e test

* Clean up

* add changeset

* Fix notebook

* add changeset

* Connect to heartbeat in python client

* 15 second heartbeat

* Demo for unload

* Add notebook

* add changeset

* Fix docs

* revert demo changes

* Add docstrings

* lint 🙄

* Edit

* handle shutdown issue

* state comments

* client test

* Fix:

* Fix e2e test

* 3.11 incompatibility

* delete after one hour

* lint + highlight

* Update .changeset/better-tires-shave.md

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Update .changeset/better-tires-shave.md

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2024-04-01 18:31:56 -04:00

80 lines
2.0 KiB
Python

import inspect
import pathlib
from contextlib import contextmanager
import pytest
from gradio_client import Client
import gradio as gr
def pytest_configure(config):
config.addinivalue_line(
"markers", "flaky: mark test as flaky. Failure will not cause te"
)
@pytest.fixture
def test_file_dir():
return pathlib.Path(pathlib.Path(__file__).parent, "test_files")
@pytest.fixture
def io_components():
classes_to_check = gr.components.Component.__subclasses__()
subclasses = []
while classes_to_check:
subclass = classes_to_check.pop()
if subclass in [
gr.components.FormComponent,
gr.State,
gr.LoginButton,
gr.LogoutButton,
]:
continue
children = subclass.__subclasses__()
if children:
classes_to_check.extend(children)
if "value" in inspect.signature(subclass.__init__).parameters:
subclasses.append(subclass)
return subclasses
@pytest.fixture
def connect():
@contextmanager
def _connect(demo: gr.Blocks, serialize=True, **kwargs):
_, local_url, _ = demo.launch(prevent_thread_lock=True, **kwargs)
try:
client = Client(local_url, serialize=serialize)
yield client
finally:
client.close()
demo.close()
return _connect
@pytest.fixture(autouse=True)
def gradio_temp_dir(monkeypatch, tmp_path):
"""tmp_path is unique to each test function.
It will be cleared automatically according to pytest docs: https://docs.pytest.org/en/6.2.x/reference.html#tmp-path
"""
monkeypatch.setenv("GRADIO_TEMP_DIR", str(tmp_path))
return tmp_path
@pytest.fixture(autouse=True)
def clear_static_files():
"""Clears all static files from the _StaticFiles class.
This is necessary because the tests should be independent of one another.
"""
yield
from gradio import data_classes
data_classes._StaticFiles.clear()