Skip to content

Commit

Permalink
[tests] use in-memory mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgorana committed Mar 6, 2024
1 parent 6987e0e commit 15f102a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 119 deletions.
1 change: 1 addition & 0 deletions packages/syft/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ test_plugins =
faker
lxml
fakeredis[lua]
pymongo-inmemory

[options.entry_points]
console_scripts =
Expand Down
44 changes: 42 additions & 2 deletions packages/syft/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from .syft.stores.store_fixtures_test import mongo_action_store # noqa: F401
from .syft.stores.store_fixtures_test import mongo_document_store # noqa: F401
from .syft.stores.store_fixtures_test import mongo_queue_stash # noqa: F401
from .syft.stores.store_fixtures_test import mongo_server_mock # noqa: F401
from .syft.stores.store_fixtures_test import mongo_store_partition # noqa: F401
from .syft.stores.store_fixtures_test import sqlite_action_store # noqa: F401
from .syft.stores.store_fixtures_test import sqlite_document_store # noqa: F401
Expand Down Expand Up @@ -56,6 +55,24 @@ def pytest_xdist_auto_num_workers(config):
return None


def pytest_collection_modifyitems(items):
for item in items:
item_fixtures = getattr(item, "fixturenames", ())

# group tests so that they run on the same worker
if "test_mongo_" in item.nodeid or "mongo_client" in item_fixtures:
item.add_marker(pytest.mark.xdist_group(name="mongo"))

elif "redis_client" in item_fixtures:
item.add_marker(pytest.mark.xdist_group(name="redis"))

elif "test_sqlite_" in item.nodeid:
item.add_marker(pytest.mark.xdist_group(name="sqlite"))

elif "test_actionobject_" in item.nodeid:
item.add_marker(pytest.mark.xdist_group(name="action_object"))


@pytest.fixture(autouse=True)
def protocol_file():
random_name = sy.UID().to_string()
Expand Down Expand Up @@ -127,9 +144,32 @@ def action_store(worker):
return worker.action_store


@pytest.fixture(scope="session")
def redis_client(monkeypatch):
# third party
import fakeredis

client = fakeredis.FakeRedis()

# Current Lock implementation creates it's own StrictRedis, this is a way to circumvent that issue
monkeypatch.setattr("redis.Redis", lambda *args, **kwargs: client)
monkeypatch.setattr("redis.StrictRedis", lambda *args, **kwargs: client)

return client


@pytest.fixture(scope="session")
def mongo_client():
# third party
import pymongo_inmemory

client = pymongo_inmemory.MongoClient()

return client


__all__ = [
"mongo_store_partition",
"mongo_server_mock",
"mongo_document_store",
"mongo_queue_stash",
"mongo_action_store",
Expand Down
16 changes: 2 additions & 14 deletions packages/syft/tests/syft/locks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from syft.store.locks import SyftLock
from syft.store.locks import ThreadingLockingConfig

REDIS_CLIENT_CACHE = None

def_params = {
"lock_name": "testing_lock",
"expire": 5, # seconds,
Expand Down Expand Up @@ -53,20 +55,6 @@ def locks_file_config():
return FileLockingConfig(**def_params)


@pytest.fixture
def redis_client(monkeypatch):
# third party
import fakeredis

redis_client = fakeredis.FakeRedis()

# make sure redis client instances always returns our fake client
monkeypatch.setattr("redis.Redis", lambda *args, **kwargs: redis_client)
monkeypatch.setattr("redis.StrictRedis", lambda *args, **kwargs: redis_client)

return redis_client


@pytest.fixture(scope="function")
def locks_redis_config(redis_client):
def_params["lock_name"] = generate_lock_name()
Expand Down
91 changes: 53 additions & 38 deletions packages/syft/tests/syft/stores/mongo_document_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from threading import Thread
from typing import List
from typing import Set
from typing import Tuple

# third party
from joblib import Parallel
Expand Down Expand Up @@ -57,16 +56,25 @@ def test_mongo_store_partition_sanity(
assert hasattr(mongo_store_partition, "_permissions")


@pytest.mark.skip(
reason="Test gets stuck at store.init_store() OR does not return res.is_err()"
)
def test_mongo_store_partition_init_failed(root_verify_key) -> None:
# won't connect
mongo_config = MongoStoreClientConfig(connectTimeoutMS=1, timeoutMS=1)
mongo_config = MongoStoreClientConfig(
connectTimeoutMS=1,
timeoutMS=1,
)

store_config = MongoStoreConfig(client_config=mongo_config)
settings = PartitionSettings(name="test", object_type=MockObjectType)

store = MongoStorePartition(
root_verify_key, settings=settings, store_config=store_config
root_verify_key,
settings=settings,
store_config=store_config,
)
print(store)

res = store.init_store()
assert res.is_err()
Expand Down Expand Up @@ -297,22 +305,20 @@ def test_mongo_store_partition_update(
)
@pytest.mark.flaky(reruns=5, reruns_delay=2)
@pytest.mark.xfail
def test_mongo_store_partition_set_threading(
root_verify_key,
mongo_server_mock: Tuple,
) -> None:
def test_mongo_store_partition_set_threading(root_verify_key, mongo_client) -> None:
thread_cnt = 3
repeats = REPEATS

execution_err = None
mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()

def _kv_cbk(tid: int) -> None:
nonlocal execution_err

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
for idx in range(repeats):
obj = MockObjectType(data=idx)
Expand Down Expand Up @@ -343,7 +349,9 @@ def _kv_cbk(tid: int) -> None:
assert execution_err is None

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
stored_cnt = len(
mongo_store_partition.all(
Expand All @@ -353,23 +361,24 @@ def _kv_cbk(tid: int) -> None:
assert stored_cnt == thread_cnt * repeats


@pytest.mark.skipif(
sys.platform != "linux", reason="pytest_mock_resources + docker issues on Windows"
@pytest.mark.skip(
reason="PicklingError: Could not pickle the task to send it to the workers. And what is the point of this test?"
)
@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_mongo_store_partition_set_joblib(
root_verify_key,
mongo_server_mock,
mongo_client,
) -> None:
thread_cnt = 3
repeats = REPEATS
mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()

def _kv_cbk(tid: int) -> None:
for idx in range(repeats):
mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
obj = MockObjectType(data=idx)

Expand All @@ -393,7 +402,9 @@ def _kv_cbk(tid: int) -> None:
assert execution_err is None

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
stored_cnt = len(
mongo_store_partition.all(
Expand All @@ -410,15 +421,16 @@ def _kv_cbk(tid: int) -> None:
@pytest.mark.xfail(reason="Fails in CI sometimes")
def test_mongo_store_partition_update_threading(
root_verify_key,
mongo_server_mock,
mongo_client,
) -> None:
thread_cnt = 3
repeats = REPEATS

mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()
mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)

obj = MockSyftObject(data=0)
Expand All @@ -430,7 +442,9 @@ def _kv_cbk(tid: int) -> None:
nonlocal execution_err

mongo_store_partition_local = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
for repeat in range(repeats):
obj = MockSyftObject(data=repeat)
Expand Down Expand Up @@ -462,26 +476,26 @@ def _kv_cbk(tid: int) -> None:
sys.platform != "linux", reason="pytest_mock_resources + docker issues on Windows"
)
@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_mongo_store_partition_update_joblib(
root_verify_key,
mongo_server_mock: Tuple,
) -> None:
def test_mongo_store_partition_update_joblib(root_verify_key, mongo_client) -> None:
thread_cnt = 3
repeats = REPEATS

mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
obj = MockSyftObject(data=0)
key = mongo_store_partition.settings.store_key.with_obj(obj)
mongo_store_partition.set(root_verify_key, obj, ignore_duplicates=False)

def _kv_cbk(tid: int) -> None:
mongo_store_partition_local = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
for repeat in range(repeats):
obj = MockSyftObject(data=repeat)
Expand Down Expand Up @@ -509,18 +523,19 @@ def _kv_cbk(tid: int) -> None:
)
def test_mongo_store_partition_set_delete_threading(
root_verify_key,
mongo_server_mock,
mongo_client,
) -> None:
thread_cnt = 3
repeats = REPEATS
execution_err = None
mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()

def _kv_cbk(tid: int) -> None:
nonlocal execution_err
mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)

for idx in range(repeats):
Expand Down Expand Up @@ -557,7 +572,9 @@ def _kv_cbk(tid: int) -> None:
assert execution_err is None

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
stored_cnt = len(
mongo_store_partition.all(
Expand All @@ -571,18 +588,14 @@ def _kv_cbk(tid: int) -> None:
@pytest.mark.skipif(
sys.platform != "linux", reason="pytest_mock_resources + docker issues on Windows"
)
def test_mongo_store_partition_set_delete_joblib(
root_verify_key,
mongo_server_mock,
) -> None:
def test_mongo_store_partition_set_delete_joblib(root_verify_key, mongo_client) -> None:
thread_cnt = 3
repeats = REPEATS
mongo_db_name = generate_db_name()
mongo_kwargs = mongo_server_mock.pmr_credentials.as_mongo_kwargs()

def _kv_cbk(tid: int) -> None:
mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client, root_verify_key, mongo_db_name=mongo_db_name
)

for idx in range(repeats):
Expand Down Expand Up @@ -612,7 +625,9 @@ def _kv_cbk(tid: int) -> None:
assert execution_err is None

mongo_store_partition = mongo_store_partition_fn(
root_verify_key, mongo_db_name=mongo_db_name, **mongo_kwargs
mongo_client,
root_verify_key,
mongo_db_name=mongo_db_name,
)
stored_cnt = len(
mongo_store_partition.all(
Expand Down
2 changes: 1 addition & 1 deletion packages/syft/tests/syft/stores/queue_stash_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _kv_cbk(tid: int) -> None:
@pytest.mark.parametrize(
"backend", [helper_queue_set_threading, helper_queue_set_joblib]
)
@pytest.mark.flaky(reruns=3, reruns_delay=1)
@pytest.mark.flaky(reruns=5, reruns_delay=3)
def test_queue_set_sqlite(root_verify_key, sqlite_workspace, backend):
def create_queue_cbk():
return sqlite_queue_stash_fn(root_verify_key, sqlite_workspace)
Expand Down
Loading

0 comments on commit 15f102a

Please sign in to comment.