From a9f25a595a08a3d976c04fe880cf251627e8d6c0 Mon Sep 17 00:00:00 2001 From: Ricardo Branco Date: Wed, 4 Sep 2024 20:05:31 +0200 Subject: [PATCH] Drop singleton pattern --- cloudview/instance.py | 4 +--- cloudview/singleton.py | 39 --------------------------------------- tests/test_azure.py | 5 ----- tests/test_ec2.py | 5 ----- tests/test_gce.py | 6 ------ tests/test_openstack.py | 6 ------ tests/test_singleton.py | 30 ------------------------------ 7 files changed, 1 insertion(+), 94 deletions(-) delete mode 100644 cloudview/singleton.py delete mode 100644 tests/test_singleton.py diff --git a/cloudview/instance.py b/cloudview/instance.py index ecd3155..0818f2d 100644 --- a/cloudview/instance.py +++ b/cloudview/instance.py @@ -9,8 +9,6 @@ from libcloud.compute.types import NodeState, LibcloudError from requests.exceptions import RequestException -from cloudview.singleton import Singleton2 - STATES = [str(getattr(NodeState, _)) for _ in dir(NodeState) if _.isupper()] @@ -31,7 +29,7 @@ class Instance: # pylint: disable=too-many-instance-attributes extra: dict -class CSP(metaclass=Singleton2): +class CSP: """ Cloud Service Provider class """ diff --git a/cloudview/singleton.py b/cloudview/singleton.py deleted file mode 100644 index 78634d2..0000000 --- a/cloudview/singleton.py +++ /dev/null @@ -1,39 +0,0 @@ -""" -Singleton metaclasses -""" - -import threading - - -class Singleton(type): - """ - Singleton metaclass - """ - - def __init__(cls, name, bases, attrs) -> None: - super().__init__(name, bases, attrs) - cls._singleton_instance = None - - def __call__(cls, *args, **kwargs): - if cls._singleton_instance is None: - cls._singleton_instance = super().__call__(*args, **kwargs) - return cls._singleton_instance - - -class Singleton2(type): - """ - Singleton metaclass that considers arguments - """ - - def __init__(cls, name, bases, attrs) -> None: - super().__init__(name, bases, attrs) - cls._singleton_instances: dict[tuple, type] = {} - cls.__singleton_lock = threading.RLock() - - def __call__(cls, *args, **kwargs): - key = (cls, args, frozenset(kwargs.items())) - if key not in cls._singleton_instances: - with cls.__singleton_lock: - if key not in cls._singleton_instances: - cls._singleton_instances[key] = super().__call__(*args, **kwargs) - return cls._singleton_instances[key] diff --git a/tests/test_azure.py b/tests/test_azure.py index 4c48c32..545498a 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -62,11 +62,6 @@ def test_get_creds_missing_env_vars(): assert "subscription_id" not in creds -@pytest.fixture(autouse=True) -def reset_singleton(monkeypatch): - monkeypatch.setattr(Azure, "_singleton_instances", {}) - - @pytest.fixture def valid_creds(): return { diff --git a/tests/test_ec2.py b/tests/test_ec2.py index d731bfa..22c41a1 100644 --- a/tests/test_ec2.py +++ b/tests/test_ec2.py @@ -62,11 +62,6 @@ def valid_creds(): return {"key": "your_access_key", "secret": "your_secret_key"} -@pytest.fixture(autouse=True) -def reset_singleton(monkeypatch): - monkeypatch.setattr(EC2, "_singleton_instances", {}) - - def test_list_instances_in_region( mock_ec2_driver, mock_ec2_instance, mocker, valid_creds ): diff --git a/tests/test_gce.py b/tests/test_gce.py index f3ff1e5..778fa34 100644 --- a/tests/test_gce.py +++ b/tests/test_gce.py @@ -88,12 +88,6 @@ def test_get_creds_with_invalid_creds_file_and_env_var( assert creds is None -# Fixture to reset the singleton instance before each test -@pytest.fixture(autouse=True) -def reset_singleton(monkeypatch): - monkeypatch.setattr(GCE, "_singleton_instances", {}) - - @pytest.fixture def valid_creds(): return { diff --git a/tests/test_openstack.py b/tests/test_openstack.py index 58d71dc..bbd0d84 100644 --- a/tests/test_openstack.py +++ b/tests/test_openstack.py @@ -56,12 +56,6 @@ def test_get_creds_with_missing_auth_url(mock_openstack_env, monkeypatch): assert not creds -# Fixture to reset the singleton instance before each test -@pytest.fixture(autouse=True) -def reset_singleton(monkeypatch): - monkeypatch.setattr(Openstack, "_singleton_instances", {}) - - @pytest.fixture def valid_creds(): return { diff --git a/tests/test_singleton.py b/tests/test_singleton.py deleted file mode 100644 index a227372..0000000 --- a/tests/test_singleton.py +++ /dev/null @@ -1,30 +0,0 @@ -# pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring,too-few-public-methods - -from cloudview.singleton import Singleton, Singleton2 - - -def test_singleton(): - class Example(metaclass=Singleton): - def __init__(self, value): - self.value = value - - instance1 = Example("first instance") - instance2 = Example("second instance") - - assert instance1 is instance2 - assert instance1.value == instance2.value - - -def test_singleton2(): - class Example(metaclass=Singleton2): - def __init__(self, value): - self.value = value - - instance1 = Example("first instance") - instance2 = Example("first instance") - instance3 = Example("second instance") - - assert instance1 is instance2 - assert instance1 is not instance3 - assert instance1.value == instance2.value - assert instance1.value != instance3.value