def test_delete_key_should_raise_exception_when_key_does_not_exists(): cache = MemoryCache() try: cache.delete("invalid key") except KeyError: assert True, "Expected exception"
def test_delete_key_should_remove_key_when_it_exists(): cache = MemoryCache() cache.add("key", "value") assert cache.get("key") cache.delete("key") assert cache.get("key", None) is None
def __init__(self, env, cache_manager=None): # Get all environment variables self.iam = Container() self.iam.token = env.get('IAM_TOKEN') self.iam.client_id = env.get('IAM_CLIENT_ID') self.iam.client_secret = env.get('IAM_CLIENT_SECRET') self.marathon = Container() self.marathon.user = env.get('MARATHON_USER'), self.marathon.passwd = env.get('MARATHON_PASSWD') # CACHE self.cache_dir = '/tmp' if cache_manager == 'ZOOKEEPER': self.cache = ZookeeperCache(env.get('ZOOKEEPER_HOST_LIST')) elif cache_manager == 'MARATHON': self.cache = MarathonCache(self.marathon.user, self.marathon.passwd) else: self.cache = MemoryCache() # LOAD PROXY CONFIG FILE with open(CONFIG_FILE_PATH) as config_file: proxy_config = json.load(config_file) # Configuration containers self.config = Container() self.config.local_cache = Container() self.config.lock_file = Container() self.config.tts = Container() self.config.iam = Container() self.config.user = Container() # Configuration variables self.config.local_cache.expiration_time = proxy_config.get( 'local_cache_expiration_time') self.config.audience = proxy_config.get('audience') self.config.lock_file.age = proxy_config.get('lock_file_age') self.config.lock_file.path = "{}/lock".format(self.cache_dir) self.config.tts.url = proxy_config.get('tts') self.config.tts.output_data = '{}/output.json'.format(self.cache_dir) self.config.iam.endpoint = proxy_config.get('iam_endpoint') self.config.iam.token_endpoint = self.config.iam.endpoint + 'token' self.config.iam.introspect_endpoint = self.config.iam.endpoint + 'introspect' self.config.iam.credential_endpoint = proxy_config.get( 'credential_endpoint') self.config.user.cert = "{}/usercert.crt".format(self.cache_dir) self.config.user.key = "{}/userkey.key".format(self.cache_dir) self.config.user.passwd = "{}/userpasswd.txt".format(self.cache_dir) self.config.user.proxy = "{}/userproxy.pem".format(self.cache_dir) self.exchanged_token = ""
def execute_command(cache, command): command, *args = command.strip().split(" ") if command == "SET": key, value = args cache.add(key, value) return "OK" elif command == "GET": return cache.get(args[0], "NOT_FOUND") elif command == "DELETE": try: cache.delete(args[0]) except KeyError: return "NOT_FOUND" return "OK" elif command == "EXIT": raise StopIteration() else: return "Commands available: SET, GET, DELETE, EXIT" if __name__ == "__main__": memory_cache = MemoryCache() asyncio.run(start_cache_server(memory_cache, "127.0.0.1", 5000))
def test_get_key_should_return_none_when_key_does_not_exist(): cache = MemoryCache() assert cache.get("key") is None
def test_key_can_be_retrieved_after_it_is_added(): cache = MemoryCache() cache.add("key", "value") assert cache.get("key") == "value", "Value does not match"
def test_get_key_should_return_value_stored(): cache = MemoryCache() cache.add("key", "value") assert cache.get("key") == "value"