def test_setter(): """Test if we can set a new interval""" def update(previous_data): return previous_data + 1 cd = CachedData(data=-1, interval=0.1, update_fct=update) assert (cd.interval == 0.1) cd.interval = 0.2 assert (cd.interval == 0.2) assert (cd.data == 0) time.sleep(0.05) # Data must not change assert (cd.data == 0) time.sleep(0.2) # Data must have change assert (cd.data == 1)
def __init__(self, hostname="127.0.0.1", port=None, protocol="http", path="", name=None, timeout=0.5, ping_path=None, ping_interval=1): """ :param hostname: The host name of the backend :param port: Port to the service :param protocol: Protocol to use :param path: Path to the ressource :param name: Name of the backend :param timeout: Timeout to consider the backend unavaible :param ping_path: Path to use to make a ping, if None will use path """ self.__hostname = hostname self.__port = port self.__protocol = protocol self.__path = path self.__name = hostname if name is None else name self.__timeout = timeout self.__ping_path = path if ping_path is None else ping_path self.__ping_interval = ping_interval self.__available = CachedData(data=False, interval=self.__ping_interval, update_fct=self._ping)
def test_last_update(): """Test that the last_update is update each time the data is update""" def update(previous_data): return previous_data + 1 cd = CachedData(data=-1, interval=0.5, update_fct=update) assert (cd.data == 0) lu = cd.last_update mtime = time.time() # Check that last_update is update while cd.last_update == lu: time.sleep(0.001) d = cd.data if cd.last_update == lu: assert (d == 0) else: break if time.time() - mtime >= 60: raise Exception("last_update was not update!") # Data must have change assert (cd.data == 1)
def test_force_data(): """Test that when we use the force_data, data is update immediately""" def update(previous_data): return previous_data + 1 cd = CachedData(data=-1, interval=0.2, update_fct=update) for i in range(0, 10): assert (cd.force_data == i)
def test_no_interval(): """Test that when interval is 0 update as each data call""" def update(previous_data): return previous_data + 1 cd = CachedData(data=-1, interval=0, update_fct=update) for i in range(0, 9): assert (cd.data == i)
def test_update_raise(): """Test if the update function raise an exception, we will still return a data (the previous one)""" def update(previous_data): raise Exception("Just an example") cd = CachedData(data=0, interval=0.1, update_fct=update) assert (cd.data == 0) time.sleep(0.25) assert (cd.data == 0)
def __init__(self, agent_port=5005, monitoring_interval=30, sessions_interval=0.5, max_session_per_user=1, **kwargs): """ :param agent_port: Port of the agent :param agent_path: Path to the agent :param kwargs: """ Backend.__init__(self, **kwargs) self.__agent_port = agent_port self.__monitoring = CachedData(data=self._default_value_monitoring(), interval=monitoring_interval, update_fct=self._update_monitoring) self.__sessions = CachedData(data=self._default_value_pls(), interval=sessions_interval, update_fct=self._update_pls) self.__max_session_per_user = max_session_per_user
def test_update(): """Test that the data is update after the interval""" def update(previous_data): return previous_data + 1 # When we create a cached data, cd = CachedData(data=-1, interval=0.2, update_fct=update) # First call to cd.data must call the udpate function assert (cd.data == 0) assert (cd.interval == 0.2) time.sleep(0.5) assert (cd.data == 1)
def test_interval(): """Test tha we can't see wrong value as interval. Interval must be a number""" def update(previous_data): return previous_data + 1 cd = CachedData(data=-1, interval=0.5, update_fct=update) with pytest.raises(Exception): cd.interval = -1 with pytest.raises(Exception): cd.interval = -1.0 with pytest.raises(Exception): cd.interval = "Toto" with pytest.raises(Exception): cd.interval = None with pytest.raises(Exception): CachedData(data=-1, interval=-1, update_fct=update) with pytest.raises(Exception): CachedData(data=-1, interval=-1.0, update_fct=update) with pytest.raises(Exception): CachedData(data=-1, interval="Toto", update_fct=update) with pytest.raises(Exception): CachedData(data=-1, interval=None, update_fct=update)
def __init__(self, *args, process_name="FakeService", **kwargs): # Default data kwargs["data"] = [] CachedData.__init__(self, *args, **kwargs) self.__process_name = process_name
def __init__(self, *args, **kwargs): # Default data kwargs["data"] = self.update(None) CachedData.__init__(self, *args, **kwargs)
def test_no_update_fct(): """Test that when no update function is set, it does nothing""" cd = CachedData(data=-1, interval=0) assert (cd.data == -1) assert (cd.data == -1)