Version history
---------------
+[2.0.3] - 2020-01-22
+
+* Add a new SDL storage API function `is_active()` to check healthiness of SDL instance.
+
[2.0.2] - 2020-01-14
* Version bump.
# database services.
# mysdl = _try_func_return(lambda: SyncStorage(fake_db_backend='dict'))
+# Checks if SDL is operational. Note that it is not necessary to call `is_active()` after each
+# SDL instance creation. Below example is here just to show how to call it spontaneously
+# when SDL healthiness is needed to check.
+is_active = mysdl.is_active()
+assert is_active is True
# Sets a value 'my_value' for a key 'my_key' under given namespace. Note that value
# type must be bytes and multiple key values can be set in one set function call.
)
-__version__ = '2.0.2'
+__version__ = '2.0.3'
__all__ = [
class DbBackendAbc(ABC):
"""An abstract Shared Data Layer (SDL) class providing database backend interface."""
+ @abstractmethod
+ def is_connected(self):
+ """Test database backend connection."""
+ pass
+
@abstractmethod
def close(self):
"""Close database backend connection."""
}
)
+ def is_connected(self):
+ return True
+
def close(self):
pass
}
)
+ def is_connected(self):
+ with _map_to_sdl_exception():
+ return self.__redis.ping()
+
def close(self):
self.__redis.close()
from ricsdl.syncstorage_abc import (SyncStorageAbc, SyncLockAbc)
import ricsdl.backend
from ricsdl.backend.dbbackend_abc import DbBackendAbc
-from ricsdl.exceptions import SdlTypeError
+from ricsdl.exceptions import (SdlException, SdlTypeError)
def func_arg_checker(exception, start_arg_idx, **types):
}
)
+ def is_active(self):
+ try:
+ return self.__dbbackend.is_connected()
+ except SdlException:
+ return False
+
def close(self):
self.__dbbackend.close()
A concrete implementation subclass 'SyncStorage' derives from this abstract class.
"""
+ @abstractmethod
+ def is_active(self):
+ """
+ Verify SDL storage healthiness.
+
+ Verify SDL connection to the backend data storage.
+
+ Args:
+ None
+
+ Returns:
+ bool: True if SDL is operational, false otherwise.
+
+ Raises:
+ None
+ """
+ pass
+
@abstractmethod
def close(self):
"""
@pytest.mark.usefixtures('fake_dict_backend_fixture')
class TestFakeDictBackend:
+ def test_is_connected_function_success(self):
+ ret = self.db.is_connected()
+ assert ret is True
+
def test_set_function_success(self):
self.db.set(self.ns, self.dm)
self.db.set(self.ns, self.dm2)
@pytest.mark.usefixtures('redis_backend_fixture')
class TestRedisBackend:
+ def test_is_connected_function_success(self):
+ self.mock_redis.ping.return_value = True
+ ret = self.db.is_connected()
+ self.mock_redis.ping.assert_called_once()
+ assert ret is True
+
+ def test_is_connected_function_returns_false_if_ping_fails(self):
+ self.mock_redis.ping.return_value = False
+ ret = self.db.is_connected()
+ self.mock_redis.ping.assert_called_once()
+ assert ret is False
+
+ def test_is_connected_function_can_map_redis_exception_to_sdl_exception(self):
+ self.mock_redis.ping.side_effect = redis_exceptions.ResponseError('redis error!')
+ with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+ self.db.is_connected()
+
def test_set_function_success(self):
self.db.set(self.ns, self.dm)
self.mock_redis.mset.assert_called_once_with(self.dm_redis)
from ricsdl.syncstorage import SyncStorage
from ricsdl.syncstorage import SyncLock
from ricsdl.syncstorage import func_arg_checker
-from ricsdl.exceptions import SdlTypeError
+from ricsdl.exceptions import (SdlTypeError, NotConnected)
@pytest.fixture()
@pytest.mark.usefixtures('sync_storage_fixture')
class TestSyncStorage:
+ def test_is_active_function_success(self):
+ self.mock_db_backend.is_connected.return_value = True
+ ret = self.storage.is_active()
+ self.mock_db_backend.is_connected.assert_called_once()
+ assert ret is True
+
+ def test_is_active_function_can_catch_backend_exception_and_return_false(self):
+ self.mock_db_backend.is_connected.side_effect = NotConnected
+ ret = self.storage.is_active()
+ self.mock_db_backend.is_connected.assert_called_once()
+ assert ret is False
+
def test_set_function_success(self):
self.storage.set(self.ns, self.dm)
self.mock_db_backend.set.assert_called_once_with(self.ns, self.dm)