Skip to content

Commit

Permalink
Redis Publically Read-Only (#6018)
Browse files Browse the repository at this point in the history
* multiplexed database

* ReadOnly Mode enabled

* adding in the requested changes
  • Loading branch information
Aarsh2001 authored Oct 24, 2022
1 parent c8a0ce9 commit 32e3dae
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 37 deletions.
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

0 comments on commit 32e3dae

Please sign in to comment.