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

Redis Publically Read-Only #6018

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions ivy_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,99 @@
# global
import os
import redis
from hypothesis import settings, HealthCheck
from hypothesis.database import (
MultiplexedDatabase,
ReadOnlyDatabase,
DirectoryBasedExampleDatabase,
)
from hypothesis.extra.redis import RedisExampleDatabase
from pytest import mark
from pathlib import Path

if os.getenv("REDIS_URL", default=False) and os.environ["REDIS_URL"]:
r = redis.Redis.from_url(
os.environ["REDIS_URL"], password=os.environ["REDIS_PASSWD"]

hypothesis_cache = os.getcwd() + "/.hypothesis/examples/"
redis_connect = None
try:
os.makedirs(hypothesis_cache)
except FileExistsError:
pass


def is_db_available():
global redis_connect
redis_connect = redis.Redis.from_url(
url="redis://redis-17011.c259.us-central1-2.gce.cloud.redislabs.com:17011",
username="general_use",
password="Hypothesiscache@123",
max_connections=2,
)
try:
redis_connect.get("b")
except redis.exceptions.ConnectionError:
print("Fallback to DirectoryBasedExamples")
return False
return True


def pytest_addoption(parser):
parser.addoption(
"--num-examples",
action="store",
default=5,
type=int,
help="set max examples generated by Hypothesis",
)
parser.addoption(
"--deadline",
action="store",
default=500000,
type=int,
help="set deadline for testing one example",
)


def pytest_configure(config):
profile_settings = {}
getopt = config.getoption
max_examples = getopt("--num-examples")
deadline = getopt("--deadline")
if os.getenv("REDIS_URL", default=False) and os.environ["REDIS_URL"]:
print("Update Database with examples !")
db = redis.Redis.from_url(
os.environ["REDIS_URL"], password=os.environ["REDIS_PASSWD"]
)
profile_settings["database"] = RedisExampleDatabase(
db, key_prefix=b"hypothesis-example:"
)

elif is_db_available():
print("Use Database in ReadOnly Mode with local caching !")
shared = RedisExampleDatabase(redis_connect, key_prefix=b"hypothesis-example:")
profile_settings["database"] = MultiplexedDatabase(
DirectoryBasedExampleDatabase(path=hypothesis_cache),
ReadOnlyDatabase(shared),
)

else:
print("Database unavailable, local caching only !")
profile_settings["database"] = DirectoryBasedExampleDatabase(
path=hypothesis_cache
)

if max_examples:
profile_settings["max_examples"] = max_examples
if deadline:
profile_settings["deadline"] = deadline

settings.register_profile(
"ci_with_db",
database=RedisExampleDatabase(r, key_prefix=b"hypothesis-example:"),
"ivy_profile",
**profile_settings,
suppress_health_check=(HealthCheck(3), HealthCheck(2)),
print_blob=True,
)
settings.load_profile("ci_with_db")
settings.load_profile("ivy_profile")

else:
settings.register_profile(
"ci", suppress_health_check=(HealthCheck(3), HealthCheck(2)), print_blob=True
)
settings.load_profile("ci")

skip_ids = []
skips_path = Path(__file__).parent / "skips.txt"
Expand Down
26 changes: 0 additions & 26 deletions ivy_tests/test_ivy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import pytest
from typing import Dict, Union, Tuple
from hypothesis import settings

# local
from ivy import clear_backend_stack, DefaultDevice
Expand Down Expand Up @@ -36,17 +35,6 @@


def pytest_configure(config):
num_examples = config.getoption("--num-examples")
deadline = config.getoption("--deadline")
profile_settings = {}
if num_examples:
profile_settings["max_examples"] = num_examples
if deadline:
profile_settings["deadline"] = deadline

settings.register_profile("test-profile", **profile_settings, print_blob=True)
settings.load_profile("test-profile")

# device
raw_value = config.getoption("--device")
if raw_value == "all":
Expand Down Expand Up @@ -191,20 +179,6 @@ def pytest_addoption(parser):
parser.addoption("--with-gradient-testing", action="store_true")

parser.addoption("--no-extra-testing", action="store_true")
parser.addoption(
"--num-examples",
action="store",
default=5,
type=int,
help="set max examples generated by Hypothesis",
)
parser.addoption(
"--deadline",
action="store",
default=500000,
type=int,
help="set deadline for testing one example",
)
parser.addoption(
"--my_test_dump",
action="store",
Expand Down