can change expiration_time in api

This commit is contained in:
Emmanuel Garette 2019-02-24 10:36:42 +01:00
parent 9812975aa7
commit f03394563c
5 changed files with 56 additions and 105 deletions

View file

@ -5,16 +5,12 @@ from py.test import raises
from .autopath import do_autopath from .autopath import do_autopath
do_autopath() do_autopath()
from tiramisu import setting, value
setting.expires_time = 1
value.expires_time = 1
from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDescription, Leadership from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDescription, Leadership
from tiramisu import Config from tiramisu import Config
from tiramisu.error import ConfigError, PropertiesOptionError from tiramisu.error import ConfigError, PropertiesOptionError
from tiramisu.setting import groups from tiramisu.setting import groups
from tiramisu import undefined, Params, ParamValue, ParamOption, \ from tiramisu import undefined, Params, ParamValue, ParamOption, \
list_sessions, default_storage, delete_session list_sessions, default_storage, delete_session
from tiramisu.api import TIRAMISU_VERSION
def teardown_function(function): def teardown_function(function):
@ -230,9 +226,6 @@ def test_reset_cache():
assert 'u1' not in values._p_.get_cached() assert 'u1' not in values._p_.get_cached()
assert 'u1' not in settings._p_.get_cached() assert 'u1' not in settings._p_.get_cached()
cfg.option('u1').value.get() cfg.option('u1').value.get()
sleep(1)
cfg.option('u1').value.get()
sleep(1)
cfg.option('u2').value.get() cfg.option('u2').value.get()
assert 'u1' in values._p_.get_cached() assert 'u1' in values._p_.get_cached()
assert 'u1' in settings._p_.get_cached() assert 'u1' in settings._p_.get_cached()
@ -270,9 +263,6 @@ def test_cache_leadership():
cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() cfg.option('ip_admin_eth0.ip_admin_eth0').value.get()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
cache = cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() cache = cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached()
if TIRAMISU_VERSION == 2:
assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0'])
else:
assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None])
assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2'] assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2']
@ -283,9 +273,6 @@ def test_cache_leadership():
assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert set(cache['ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0'].keys()) == set([None])
assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None])
if TIRAMISU_VERSION == 2:
assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None, 0])
else:
assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([0]) assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([0])
# #
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2', '192.168.1.1']) cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2', '192.168.1.1'])
@ -293,9 +280,6 @@ def test_cache_leadership():
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()
cache = cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() cache = cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached()
if TIRAMISU_VERSION == 2:
assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0'])
else:
assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None])
assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2', '192.168.1.1'] assert cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2', '192.168.1.1']
@ -307,9 +291,6 @@ def test_cache_leadership():
assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0']) assert set(cache.keys()) == set([None, 'ip_admin_eth0', 'ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert set(cache['ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0'].keys()) == set([None])
assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None]) assert set(cache['ip_admin_eth0.ip_admin_eth0'].keys()) == set([None])
if TIRAMISU_VERSION == 2:
assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None, 0, 1])
else:
assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([0, 1]) assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([0, 1])
#DEL, insert, ... #DEL, insert, ...
@ -336,8 +317,6 @@ def test_cache_callback():
maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5]) maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5])
cfg = Config(maconfig) cfg = Config(maconfig)
cfg.property.read_write() cfg.property.read_write()
if TIRAMISU_VERSION == 2:
cfg.property.pop('expire')
cfg.value.dict() cfg.value.dict()
#assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)}, #assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings']), None)},
# 'val1': {None: (set([]), None)}} # 'val1': {None: (set([]), None)}}
@ -430,23 +409,12 @@ def test_cache_leader_and_followers():
val1_props = [] val1_props = []
val1_val1_props = ['empty'] val1_val1_props = ['empty']
val1_val2_props = [] val1_val2_props = []
if TIRAMISU_VERSION == 2:
cfg.property.pop('expire')
global_props = set(global_props)
val1_props = set(val1_props)
val1_val1_props = set(val1_val1_props)
val1_val2_props = set(val1_val2_props)
else:
global_props = frozenset(global_props) global_props = frozenset(global_props)
val1_props = frozenset(val1_props) val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_props) val1_val1_props = frozenset(val1_val1_props)
val1_val2_props = frozenset(val1_val2_props) val1_val2_props = frozenset(val1_val2_props)
#None because no value #None because no value
idx_val2 = None idx_val2 = None
if TIRAMISU_VERSION == 2:
assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {}
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {}
else:
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)}, compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)}, 'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)}, 'val1.val1': {None: (val1_val1_props, None)},
@ -458,10 +426,6 @@ def test_cache_leader_and_followers():
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(global_props), None)}}) compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(global_props), None)}})
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {} assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {}
cfg.value.dict() cfg.value.dict()
if TIRAMISU_VERSION == 2:
val_val2 = [None]
val_val2_props = {None: (set(), None), 0: (set(), None)}
else:
#has value #has value
idx_val2 = 0 idx_val2 = 0
val_val2 = None val_val2 = None
@ -477,9 +441,6 @@ def test_cache_leader_and_followers():
cfg.option('val1.val2', 1).value.set('oui') cfg.option('val1.val2', 1).value.set('oui')
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings', 'force_store_value']), None)}}) compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings', 'force_store_value']), None)}})
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {} assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {}
if TIRAMISU_VERSION == 2:
val1_val2_props = {None: (set([]), None), 0: (set([]), None), 1: (set([]), None)}
else:
val1_val2_props = {0: (frozenset([]), None), 1: (frozenset([]), None)} val1_val2_props = {0: (frozenset([]), None), 1: (frozenset([]), None)}
#assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (global_props, None)}, #assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {None: {None: (global_props, None)},
# 'val1': {None: (val1_props, None)}, # 'val1': {None: (val1_props, None)},
@ -505,21 +466,10 @@ def test_cache_leader_callback():
val1_props = [] val1_props = []
val1_val1_props = ['empty'] val1_val1_props = ['empty']
val1_val2_props = [] val1_val2_props = []
if TIRAMISU_VERSION == 2:
cfg.property.pop('expire')
global_props = set(global_props)
val1_props = set(val1_props)
val1_val1_props = set(val1_val1_props)
val1_val2_props = set(val1_val2_props)
else:
global_props = frozenset(global_props) global_props = frozenset(global_props)
val1_props = frozenset(val1_props) val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_props) val1_val1_props = frozenset(val1_val1_props)
val1_val2_props = frozenset(val1_val2_props) val1_val2_props = frozenset(val1_val2_props)
if TIRAMISU_VERSION == 2:
assert cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached() == {}
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {}
else:
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)}, compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)}, 'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)}, 'val1.val1': {None: (val1_val1_props, None)},
@ -544,17 +494,12 @@ def test_cache_requires():
od = OptionDescription('service', '', [a, b]) od = OptionDescription('service', '', [a, b])
cfg = Config(od) cfg = Config(od)
cfg.property.read_write() cfg.property.read_write()
if TIRAMISU_VERSION == 2:
cfg.property.pop('expire')
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {} assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {}
assert cfg.option('ip_address_service').value.get() == None assert cfg.option('ip_address_service').value.get() == None
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings', 'force_store_value']), None)}, compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(['cache', 'disabled', 'frozen', 'hidden', 'validator', 'warnings', 'force_store_value']), None)},
'activate_service': {None: (set([]), None)}, 'activate_service': {None: (set([]), None)},
'ip_address_service': {None: (set([]), None)}}) 'ip_address_service': {None: (set([]), None)}})
if TIRAMISU_VERSION == 2:
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {'ip_address_service': {None: (None, None)}}
else:
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)}, compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)},
'activate_service': {None: (True, None)}}) 'activate_service': {None: (True, None)}})
cfg.value.dict() cfg.value.dict()
@ -601,9 +546,6 @@ def test_cache_global_properties():
'activate_service': {None: (set([]), None)}, 'activate_service': {None: (set([]), None)},
'ip_address_service': {None: (set([]), None)}}) 'ip_address_service': {None: (set([]), None)}})
if TIRAMISU_VERSION == 2:
assert cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached() == {'ip_address_service': {None: (None, None)}}
else:
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)}, compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)},
'activate_service': {None: (True, None)}}) 'activate_service': {None: (True, None)}})
cfg.property.pop('disabled') cfg.property.pop('disabled')
@ -623,6 +565,7 @@ def test_callback_value_incr():
val2 = IntOption('val2', "", callback=return_value, callback_params=Params(kwargs={'value': ParamOption(val1)})) val2 = IntOption('val2', "", callback=return_value, callback_params=Params(kwargs={'value': ParamOption(val1)}))
maconfig = OptionDescription('rootconfig', '', [val1, val2]) maconfig = OptionDescription('rootconfig', '', [val1, val2])
cfg = Config(maconfig) cfg = Config(maconfig)
cfg.cache.expiration_time(1)
cfg.property.read_write() cfg.property.read_write()
assert cfg.option('val1').value.get() == 1 assert cfg.option('val1').value.get() == 1
sleep(1) sleep(1)

View file

@ -22,7 +22,7 @@ from typing import List, Set, Any, Optional, Callable, Union, Dict
from .error import APIError, ConfigError, LeadershipError, PropertiesOptionError from .error import APIError, ConfigError, LeadershipError, PropertiesOptionError
from .i18n import _ from .i18n import _
from .setting import ConfigBag, OptionBag, owners, groups, Undefined, undefined, \ from .setting import ConfigBag, OptionBag, owners, groups, Undefined, undefined, \
FORBIDDEN_SET_PROPERTIES, SPECIAL_PROPERTIES FORBIDDEN_SET_PROPERTIES, SPECIAL_PROPERTIES, EXPIRATION_TIME
from .config import KernelConfig, SubConfig, KernelGroupConfig, KernelMetaConfig, KernelMixConfig from .config import KernelConfig, SubConfig, KernelGroupConfig, KernelMetaConfig, KernelMixConfig
from .option import ChoiceOption, OptionDescription from .option import ChoiceOption, OptionDescription
@ -1301,6 +1301,10 @@ class TiramisuContextCache(TiramisuContext):
def reset(self): def reset(self):
self._config_bag.context.cfgimpl_reset_cache(None, None) self._config_bag.context.cfgimpl_reset_cache(None, None)
def expiration_time(self,
time: int):
self._config_bag.expiration_time = time
class TiramisuDispatcher: class TiramisuDispatcher:
pass pass

View file

@ -23,14 +23,11 @@ from .error import (RequirementError, PropertiesOptionError,
from .i18n import _ from .i18n import _
"Default encoding for display a Config if raise UnicodeEncodeError"
default_encoding = 'utf-8'
"""If cache and expire is enable, time before cache is expired. """If cache and expire is enable, time before cache is expired.
This delay start first time value/setting is set in cache, even if This delay start first time value/setting is set in cache, even if
user access several time to value/setting user access several time to value/setting
""" """
expires_time = 5 EXPIRATION_TIME = 5
"""List of default properties (you can add new one if needed). """List of default properties (you can add new one if needed).
For common properties and personalise properties, if a propery is set for For common properties and personalise properties, if a propery is set for
@ -68,7 +65,7 @@ cache
if set, enable cache settings and values if set, enable cache settings and values
expire expire
if set, settings and values in cache expire after ``expires_time`` if set, settings and values in cache expire after ``expiration_time``
everything_frozen everything_frozen
whole option in config are frozen (even if option have not frozen whole option in config are frozen (even if option have not frozen
@ -186,7 +183,9 @@ class ConfigBag:
'properties', # properties for current context 'properties', # properties for current context
'true_properties', # properties for current context 'true_properties', # properties for current context
'permissives', # permissives for current context 'permissives', # permissives for current context
'expiration_time' # EXPIRATION_TIME
) )
def __init__(self, context, **kwargs): def __init__(self, context, **kwargs):
self.context = context self.context = context
for key, value in kwargs.items(): for key, value in kwargs.items():
@ -194,13 +193,18 @@ class ConfigBag:
def __getattr__(self, key): def __getattr__(self, key):
if key == 'properties': if key == 'properties':
self.properties = self.context.cfgimpl_get_settings().get_context_properties() settings = self.context.cfgimpl_get_settings()
self.properties = settings.get_context_properties()
return self.properties return self.properties
if key == 'permissives': if key == 'permissives':
self.permissives = self.context.cfgimpl_get_settings().get_context_permissives() settings = self.context.cfgimpl_get_settings()
self.permissives = settings.get_context_permissives()
return self.permissives return self.permissives
if key == 'true_properties': if key == 'true_properties':
return self.properties return self.properties
if key == 'expiration_time':
self.expiration_time = EXPIRATION_TIME
return self.expiration_time
raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover
def remove_warnings(self): def remove_warnings(self):
@ -414,7 +418,7 @@ class Settings(object):
if apply_requires: if apply_requires:
props = config_bag.properties props = config_bag.properties
is_cached, props = self._p_.getcache(path, is_cached, props = self._p_.getcache(path,
expires_time, config_bag.expiration_time,
index, index,
props, props,
{}, {},

View file

@ -55,7 +55,7 @@ class Cache(DictCache):
def getcache(self, def getcache(self,
path, path,
expires_time, expiration_time,
index, index,
props, props,
self_props, self_props,
@ -75,11 +75,11 @@ class Cache(DictCache):
self_props = value self_props = value
# recheck "cache" value # recheck "cache" value
if 'cache' in props or 'cache' in props: if 'cache' in props or 'cache' in props:
if expires_time and timestamp and \ if expiration_time and timestamp and \
('expire' in props or \ ('expire' in props or \
'expire' in self_props): 'expire' in self_props):
ntime = int(time()) ntime = int(time())
if timestamp + expires_time >= ntime: if timestamp + expiration_time >= ntime:
if DEBUG: # pragma: no cover if DEBUG: # pragma: no cover
print('getcache in cache (1)', path, value, _display_classname(self), print('getcache in cache (1)', path, value, _display_classname(self),
id(self), index) id(self), index)
@ -87,7 +87,7 @@ class Cache(DictCache):
else: else:
if DEBUG: # pragma: no cover if DEBUG: # pragma: no cover
print('getcache expired value for path {} < {}'.format( print('getcache expired value for path {} < {}'.format(
timestamp + expires_time, ntime)) timestamp + expiration_time, ntime))
# if expired, remove from cache # if expired, remove from cache
#self.delcache(path) #self.delcache(path)
else: else:

View file

@ -18,7 +18,7 @@
import weakref import weakref
from typing import Optional from typing import Optional
from .error import ConfigError, PropertiesOptionError from .error import ConfigError, PropertiesOptionError
from .setting import owners, expires_time, undefined, forbidden_owners, OptionBag, ConfigBag from .setting import owners, undefined, forbidden_owners, OptionBag, ConfigBag
from .autolib import carry_out_calculation from .autolib import carry_out_calculation
from .i18n import _ from .i18n import _
@ -62,7 +62,7 @@ class Values(object):
# try to retrive value in cache # try to retrive value in cache
setting_properties = option_bag.config_bag.properties setting_properties = option_bag.config_bag.properties
is_cached, value = self._p_.getcache(option_bag.path, is_cached, value = self._p_.getcache(option_bag.path,
expires_time, option_bag.config_bag.expiration_time,
option_bag.index, option_bag.index,
setting_properties, setting_properties,
option_bag.properties, option_bag.properties,