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
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 import Config
from tiramisu.error import ConfigError, PropertiesOptionError
from tiramisu.setting import groups
from tiramisu import undefined, Params, ParamValue, ParamOption, \
list_sessions, default_storage, delete_session
from tiramisu.api import TIRAMISU_VERSION
def teardown_function(function):
@ -230,9 +226,6 @@ def test_reset_cache():
assert 'u1' not in values._p_.get_cached()
assert 'u1' not in settings._p_.get_cached()
cfg.option('u1').value.get()
sleep(1)
cfg.option('u1').value.get()
sleep(1)
cfg.option('u2').value.get()
assert 'u1' in values._p_.get_cached()
assert 'u1' in settings._p_.get_cached()
@ -270,10 +263,7 @@ def test_cache_leadership():
cfg.option('ip_admin_eth0.ip_admin_eth0').value.get()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
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 cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2']
#assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None])
@ -283,20 +273,14 @@ 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['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.get()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()
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 cache['ip_admin_eth0.ip_admin_eth0'][None][0] == ['192.168.1.2', '192.168.1.1']
#assert set(cache['ip_admin_eth0.netmask_admin_eth0'].keys()) == set([None])
@ -307,10 +291,7 @@ 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['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, ...
@ -336,8 +317,6 @@ def test_cache_callback():
maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5])
cfg = Config(maconfig)
cfg.property.read_write()
if TIRAMISU_VERSION == 2:
cfg.property.pop('expire')
cfg.value.dict()
#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)}}
@ -430,42 +409,27 @@ def test_cache_leader_and_followers():
val1_props = []
val1_val1_props = ['empty']
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)
val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_props)
val1_val2_props = frozenset(val1_val2_props)
global_props = frozenset(global_props)
val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_props)
val1_val2_props = frozenset(val1_val2_props)
#None because no value
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)},
'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)},
'val1.val2': {idx_val2: (val1_val2_props, None)}})
# len is 0 so don't get any value
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([], None)}})
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)},
'val1.val2': {idx_val2: (val1_val2_props, None)}})
# len is 0 so don't get any value
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([], None)}})
#
cfg.option('val1.val1').value.set([undefined])
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() == {}
cfg.value.dict()
if TIRAMISU_VERSION == 2:
val_val2 = [None]
val_val2_props = {None: (set(), None), 0: (set(), None)}
else:
#has value
idx_val2 = 0
val_val2 = None
val_val2_props = {idx_val2: (val1_val2_props, None), None: (set(), None)}
#has value
idx_val2 = 0
val_val2 = None
val_val2_props = {idx_val2: (val1_val2_props, None), None: (set(), None)}
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)},
@ -477,10 +441,7 @@ def test_cache_leader_and_followers():
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)}})
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)},
# 'val1': {None: (val1_props, None)},
# 'val1.val1': {None: (val1_val1_props, None)},
@ -505,26 +466,15 @@ def test_cache_leader_callback():
val1_props = []
val1_val1_props = ['empty']
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)
val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_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)},
'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)},
'val1.val2': {None: (val1_val2_props, None)}})
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([], None)}})
global_props = frozenset(global_props)
val1_props = frozenset(val1_props)
val1_val1_props = frozenset(val1_val1_props)
val1_val2_props = frozenset(val1_val2_props)
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)},
'val1.val2': {None: (val1_val2_props, None)}})
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([], None)}})
cfg.option('val1.val1').value.set([undefined])
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(global_props), None)}})
@ -544,19 +494,14 @@ def test_cache_requires():
od = OptionDescription('service', '', [a, b])
cfg = Config(od)
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.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)},
'activate_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)},
'activate_service': {None: (True, None)}})
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)},
'activate_service': {None: (True, None)}})
cfg.value.dict()
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)},
@ -601,11 +546,8 @@ def test_cache_global_properties():
'activate_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)},
'activate_service': {None: (True, None)}})
compare(cfg._config_bag.context.cfgimpl_get_values()._p_.get_cached(), {'ip_address_service': {None: (None, None)},
'activate_service': {None: (True, None)}})
cfg.property.pop('disabled')
assert cfg.option('ip_address_service').value.get() == None
compare(cfg._config_bag.context.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (set(['cache', 'frozen', 'hidden', 'validator', 'warnings', 'force_store_value']), None)},
@ -623,6 +565,7 @@ def test_callback_value_incr():
val2 = IntOption('val2', "", callback=return_value, callback_params=Params(kwargs={'value': ParamOption(val1)}))
maconfig = OptionDescription('rootconfig', '', [val1, val2])
cfg = Config(maconfig)
cfg.cache.expiration_time(1)
cfg.property.read_write()
assert cfg.option('val1').value.get() == 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 .i18n import _
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 .option import ChoiceOption, OptionDescription
@ -1301,6 +1301,10 @@ class TiramisuContextCache(TiramisuContext):
def reset(self):
self._config_bag.context.cfgimpl_reset_cache(None, None)
def expiration_time(self,
time: int):
self._config_bag.expiration_time = time
class TiramisuDispatcher:
pass

View File

@ -23,14 +23,11 @@ from .error import (RequirementError, PropertiesOptionError,
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.
This delay start first time value/setting is set in cache, even if
user access several time to value/setting
"""
expires_time = 5
EXPIRATION_TIME = 5
"""List of default properties (you can add new one if needed).
For common properties and personalise properties, if a propery is set for
@ -68,7 +65,7 @@ cache
if set, enable cache settings and values
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
whole option in config are frozen (even if option have not frozen
@ -186,7 +183,9 @@ class ConfigBag:
'properties', # properties for current context
'true_properties', # properties for current context
'permissives', # permissives for current context
'expiration_time' # EXPIRATION_TIME
)
def __init__(self, context, **kwargs):
self.context = context
for key, value in kwargs.items():
@ -194,13 +193,18 @@ class ConfigBag:
def __getattr__(self, key):
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
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
if key == 'true_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
def remove_warnings(self):
@ -414,7 +418,7 @@ class Settings(object):
if apply_requires:
props = config_bag.properties
is_cached, props = self._p_.getcache(path,
expires_time,
config_bag.expiration_time,
index,
props,
{},

View File

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

View File

@ -18,7 +18,7 @@
import weakref
from typing import Optional
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 .i18n import _
@ -62,7 +62,7 @@ class Values(object):
# try to retrive value in cache
setting_properties = option_bag.config_bag.properties
is_cached, value = self._p_.getcache(option_bag.path,
expires_time,
option_bag.config_bag.expiration_time,
option_bag.index,
setting_properties,
option_bag.properties,