2023-08-04 21:18:14 +08:00
|
|
|
import asyncio
|
2023-05-16 14:55:46 +08:00
|
|
|
import ipaddress
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import warnings
|
|
|
|
from unittest import mock as mock
|
|
|
|
|
2023-12-06 06:18:09 +08:00
|
|
|
import httpx
|
2023-05-16 14:55:46 +08:00
|
|
|
import pytest
|
|
|
|
|
2023-08-04 21:18:14 +08:00
|
|
|
from gradio import analytics, wasm_utils
|
2023-05-16 14:55:46 +08:00
|
|
|
from gradio.context import Context
|
|
|
|
|
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
|
|
|
|
|
|
|
|
|
|
|
class TestAnalytics:
|
2023-12-06 06:18:09 +08:00
|
|
|
@mock.patch("httpx.get")
|
2023-05-16 14:55:46 +08:00
|
|
|
def test_should_warn_with_unable_to_parse(self, mock_get, monkeypatch):
|
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "True")
|
|
|
|
mock_get.side_effect = json.decoder.JSONDecodeError("Expecting value", "", 0)
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
analytics.version_check()
|
|
|
|
assert (
|
|
|
|
str(w[-1].message)
|
|
|
|
== "unable to parse version details from package URL."
|
|
|
|
)
|
|
|
|
|
2023-12-06 06:18:09 +08:00
|
|
|
@mock.patch("httpx.post")
|
2023-05-16 14:55:46 +08:00
|
|
|
def test_error_analytics_doesnt_crash_on_connection_error(
|
|
|
|
self, mock_post, monkeypatch
|
|
|
|
):
|
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "True")
|
2023-12-06 06:18:09 +08:00
|
|
|
mock_post.side_effect = httpx.ConnectError("Connection error")
|
2023-08-04 21:18:14 +08:00
|
|
|
analytics._do_normal_analytics_request("placeholder", {})
|
2023-05-16 14:55:46 +08:00
|
|
|
mock_post.assert_called()
|
|
|
|
|
2023-12-06 06:18:09 +08:00
|
|
|
@mock.patch("httpx.post")
|
2023-05-16 14:55:46 +08:00
|
|
|
def test_error_analytics_successful(self, mock_post, monkeypatch):
|
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "True")
|
|
|
|
analytics.error_analytics("placeholder")
|
|
|
|
mock_post.assert_called()
|
|
|
|
|
2023-08-04 21:18:14 +08:00
|
|
|
@mock.patch.object(wasm_utils, "IS_WASM", True)
|
|
|
|
@mock.patch("gradio.analytics.pyodide_pyfetch")
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_error_analytics_successful_in_wasm_mode(
|
|
|
|
self, pyodide_pyfetch, monkeypatch
|
|
|
|
):
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "True")
|
|
|
|
|
|
|
|
analytics.error_analytics("placeholder")
|
|
|
|
|
|
|
|
# Await all background tasks.
|
|
|
|
# Ref: https://superfastpython.com/asyncio-wait-for-tasks/#How_to_Wait_for_All_Background_Tasks
|
|
|
|
all_tasks = asyncio.all_tasks(loop)
|
|
|
|
current_task = asyncio.current_task()
|
|
|
|
all_tasks.remove(current_task)
|
|
|
|
await asyncio.wait(all_tasks)
|
|
|
|
|
|
|
|
pyodide_pyfetch.assert_called()
|
|
|
|
|
2023-05-16 14:55:46 +08:00
|
|
|
|
|
|
|
class TestIPAddress:
|
|
|
|
@pytest.mark.flaky
|
|
|
|
def test_get_ip(self):
|
|
|
|
Context.ip_address = None
|
|
|
|
ip = analytics.get_local_ip_address()
|
|
|
|
if ip == "No internet connection" or ip == "Analytics disabled":
|
|
|
|
return
|
|
|
|
ipaddress.ip_address(ip)
|
|
|
|
|
2023-12-06 06:18:09 +08:00
|
|
|
@mock.patch("httpx.get")
|
2023-05-16 14:55:46 +08:00
|
|
|
def test_get_ip_without_internet(self, mock_get, monkeypatch):
|
2023-12-06 06:18:09 +08:00
|
|
|
mock_get.side_effect = httpx.ConnectError("Connection error")
|
2023-05-16 14:55:46 +08:00
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "True")
|
|
|
|
Context.ip_address = None
|
|
|
|
ip = analytics.get_local_ip_address()
|
|
|
|
assert ip == "No internet connection"
|
|
|
|
|
|
|
|
monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "False")
|
|
|
|
Context.ip_address = None
|
|
|
|
ip = analytics.get_local_ip_address()
|
|
|
|
assert ip == "Analytics disabled"
|