From 7e32eb7f09b18c096f181e670a6f65db0c3294ea Mon Sep 17 00:00:00 2001 From: Aarsh2001 Date: Sat, 22 Oct 2022 14:33:04 +0530 Subject: [PATCH 1/3] multiplexed database --- ivy_tests/conftest.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/ivy_tests/conftest.py b/ivy_tests/conftest.py index be321ea50a121..3ec111984f14a 100644 --- a/ivy_tests/conftest.py +++ b/ivy_tests/conftest.py @@ -1,28 +1,37 @@ 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"] - ) - settings.register_profile( - "ci_with_db", - database=RedisExampleDatabase(r, key_prefix=b"hypothesis-example:"), - suppress_health_check=(HealthCheck(3), HealthCheck(2)), - print_blob=True, - ) - settings.load_profile("ci_with_db") -else: - settings.register_profile( - "ci", suppress_health_check=(HealthCheck(3), HealthCheck(2)), print_blob=True - ) - settings.load_profile("ci") +cache = os.getcwd() + "/.hypothesis/examples/" +try: + os.makedirs(cache) +except FileExistsError: + pass +r = redis.Redis.from_url( + url="redis://redis-17011.c259.us-central1-2.gce.cloud.redislabs.com:17011", + username="general_use", + password="Hypothesiscache@123", +) +shared = RedisExampleDatabase(r, key_prefix=b"hypothesis-example:") +local = DirectoryBasedExampleDatabase(path=cache) + +settings.register_profile( + "ci_with_db", + database=MultiplexedDatabase(local, ReadOnlyDatabase(shared)), + suppress_health_check=(HealthCheck(3), HealthCheck(2)), + print_blob=True, +) +settings.load_profile("ci_with_db") skip_ids = [] skips_path = Path(__file__).parent / "skips.txt" if skips_path.exists(): From 2ba3bdf3085aed0d6b0975ff98fa8d48bf4d8f9e Mon Sep 17 00:00:00 2001 From: Aarsh2001 Date: Sat, 22 Oct 2022 16:33:42 +0530 Subject: [PATCH 2/3] ReadOnly Mode enabled --- ivy_tests/conftest.py | 94 ++++++++++++++++++++++++++++------ ivy_tests/test_ivy/conftest.py | 26 ---------- 2 files changed, 77 insertions(+), 43 deletions(-) diff --git a/ivy_tests/conftest.py b/ivy_tests/conftest.py index 3ec111984f14a..728f02c668179 100644 --- a/ivy_tests/conftest.py +++ b/ivy_tests/conftest.py @@ -1,3 +1,4 @@ +# global import os import redis from hypothesis import settings, HealthCheck @@ -11,27 +12,86 @@ from pathlib import Path -cache = os.getcwd() + "/.hypothesis/examples/" +cache_path = os.getcwd() + "/.hypothesis/examples/" +r = None try: - os.makedirs(cache) + os.makedirs(cache_path) except FileExistsError: pass -r = redis.Redis.from_url( - url="redis://redis-17011.c259.us-central1-2.gce.cloud.redislabs.com:17011", - username="general_use", - password="Hypothesiscache@123", -) -shared = RedisExampleDatabase(r, key_prefix=b"hypothesis-example:") -local = DirectoryBasedExampleDatabase(path=cache) - -settings.register_profile( - "ci_with_db", - database=MultiplexedDatabase(local, ReadOnlyDatabase(shared)), - suppress_health_check=(HealthCheck(3), HealthCheck(2)), - print_blob=True, -) -settings.load_profile("ci_with_db") + +def is_db_available(): + global r + r = 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: + r.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(r, key_prefix=b"hypothesis-example:") + profile_settings["database"] = MultiplexedDatabase( + DirectoryBasedExampleDatabase(path=cache_path), ReadOnlyDatabase(shared) + ) + + else: + print("Database unavailable, local caching only !") + profile_settings["database"] = DirectoryBasedExampleDatabase(path=cache_path) + + if max_examples: + profile_settings["max_examples"] = max_examples + if deadline: + profile_settings["deadline"] = deadline + + settings.register_profile( + "ivy_profile", + **profile_settings, + suppress_health_check=(HealthCheck(3), HealthCheck(2)), + print_blob=True, + ) + settings.load_profile("ivy_profile") + + skip_ids = [] skips_path = Path(__file__).parent / "skips.txt" if skips_path.exists(): diff --git a/ivy_tests/test_ivy/conftest.py b/ivy_tests/test_ivy/conftest.py index 29336cfe144d0..5e96af0544ba0 100644 --- a/ivy_tests/test_ivy/conftest.py +++ b/ivy_tests/test_ivy/conftest.py @@ -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 @@ -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": @@ -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", From 28bc1da3d4bfe79ee06597b6a4dab8156b8c22cb Mon Sep 17 00:00:00 2001 From: Aarsh2001 Date: Mon, 24 Oct 2022 14:13:44 +0530 Subject: [PATCH 3/3] adding in the requested changes --- ivy_tests/conftest.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ivy_tests/conftest.py b/ivy_tests/conftest.py index 728f02c668179..9d08873123dbd 100644 --- a/ivy_tests/conftest.py +++ b/ivy_tests/conftest.py @@ -12,24 +12,24 @@ from pathlib import Path -cache_path = os.getcwd() + "/.hypothesis/examples/" -r = None +hypothesis_cache = os.getcwd() + "/.hypothesis/examples/" +redis_connect = None try: - os.makedirs(cache_path) + os.makedirs(hypothesis_cache) except FileExistsError: pass def is_db_available(): - global r - r = redis.Redis.from_url( + 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: - r.get("b") + redis_connect.get("b") except redis.exceptions.ConnectionError: print("Fallback to DirectoryBasedExamples") return False @@ -69,14 +69,17 @@ def pytest_configure(config): elif is_db_available(): print("Use Database in ReadOnly Mode with local caching !") - shared = RedisExampleDatabase(r, key_prefix=b"hypothesis-example:") + shared = RedisExampleDatabase(redis_connect, key_prefix=b"hypothesis-example:") profile_settings["database"] = MultiplexedDatabase( - DirectoryBasedExampleDatabase(path=cache_path), ReadOnlyDatabase(shared) + DirectoryBasedExampleDatabase(path=hypothesis_cache), + ReadOnlyDatabase(shared), ) else: print("Database unavailable, local caching only !") - profile_settings["database"] = DirectoryBasedExampleDatabase(path=cache_path) + profile_settings["database"] = DirectoryBasedExampleDatabase( + path=hypothesis_cache + ) if max_examples: profile_settings["max_examples"] = max_examples