tiramisu/tests/test_dereference.py

292 lines
7.4 KiB
Python
Raw Normal View History

# coding: utf-8
2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2018-10-31 08:00:19 +01:00
import weakref
2019-12-24 15:24:20 +01:00
import pytest
2018-03-19 08:33:53 +01:00
from tiramisu import BoolOption, IntOption, StrOption, IPOption, NetmaskOption, \
SymLinkOption, OptionDescription, DynOptionDescription, submulti, \
2019-09-28 16:32:48 +02:00
Config, GroupConfig, MetaConfig, Params, ParamOption, Calculation
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
2020-01-22 20:46:18 +01:00
from .config import event_loop
2014-11-10 09:13:44 +01:00
IS_DEREFABLE = True
2015-07-24 17:54:10 +02:00
2018-03-19 08:33:53 +01:00
def funcname(*args, **kwargs):
return value
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_storage():
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(cfg._config_bag.context.cfgimpl_get_values()._p_)
del cfg
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_value():
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(cfg._config_bag.context.cfgimpl_get_values())
del cfg
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_setting():
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(cfg._config_bag.context.cfgimpl_get_settings())
del cfg
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_config():
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(cfg)
del cfg
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_option():
2014-11-10 09:13:44 +01:00
global IS_DEREFABLE
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
w = weakref.ref(b)
del(b)
2014-11-10 09:13:44 +01:00
try:
assert w() is not None
except AssertionError:
IS_DEREFABLE = False
return
2014-04-14 23:00:37 +02:00
del(o)
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_optiondescription():
2014-11-10 09:13:44 +01:00
if not IS_DEREFABLE:
return
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
w = weakref.ref(o)
del(b)
assert w() is not None
del(o)
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_option_cache():
2014-11-10 09:13:44 +01:00
if not IS_DEREFABLE:
return
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-12-24 15:24:20 +01:00
await o._build_cache()
2014-04-14 23:00:37 +02:00
w = weakref.ref(b)
del(b)
assert w() is not None
del(o)
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_optiondescription_cache():
2016-10-01 20:15:08 +02:00
if not IS_DEREFABLE:
return
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2019-12-24 15:24:20 +01:00
await o._build_cache()
w = weakref.ref(o)
del(b)
assert w() is not None
del(o)
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-04-14 23:00:37 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_option_config():
2014-11-10 09:13:44 +01:00
if not IS_DEREFABLE:
return
2014-04-14 23:00:37 +02:00
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(b)
del(b)
assert w() is not None
del(o)
assert w() is not None
del cfg
2014-04-14 23:00:37 +02:00
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2014-01-25 10:15:25 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_optiondescription_config():
2016-10-01 20:15:08 +02:00
if not IS_DEREFABLE:
return
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
2020-01-22 20:46:18 +01:00
async with await Config(o) as cfg:
w = weakref.ref(o)
del(b)
assert w() is not None
del(o)
assert w() is not None
del cfg
assert w() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2013-09-30 16:22:08 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_validator():
if not IS_DEREFABLE:
return
a = StrOption('a', '', default='yes')
2019-10-27 11:09:15 +01:00
b = StrOption('b', '', validators=[Calculation(funcname, Params(ParamOption(a)))], default='val')
od = OptionDescription('root', '', [a, b])
2020-01-22 20:46:18 +01:00
async with await Config(od) as cfg:
w = weakref.ref(a)
x = weakref.ref(b)
y = weakref.ref(od)
z = weakref.ref(cfg)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(a)
del(b)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(od)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del cfg
assert y() is None
assert z() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_callback():
if not IS_DEREFABLE:
return
a = StrOption('a', "", 'val')
2019-09-28 16:32:48 +02:00
b = StrOption('b', "", Calculation(funcname, Params((ParamOption(a),))))
od = OptionDescription('root', '', [a, b])
2020-01-22 20:46:18 +01:00
async with await Config(od) as cfg:
w = weakref.ref(a)
x = weakref.ref(b)
y = weakref.ref(od)
z = weakref.ref(cfg)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(a)
del(b)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(od)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del cfg
assert y() is None
assert z() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_symlink():
if not IS_DEREFABLE:
return
a = BoolOption("a", "", default=False)
b = SymLinkOption("b", a)
od = OptionDescription('root', '', [a, b])
2020-01-22 20:46:18 +01:00
async with await Config(od) as cfg:
w = weakref.ref(a)
x = weakref.ref(b)
y = weakref.ref(od)
z = weakref.ref(cfg)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(a)
del(b)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(od)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del cfg
assert y() is None
assert z() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_deref_dyn():
if not IS_DEREFABLE:
return
a = StrOption('a', '', ['val1', 'val2'], multi=True)
b = StrOption('b', '')
dod = DynOptionDescription('dod', '', [b], suffixes=Calculation(funcname, Params((ParamOption(a),))))
od = OptionDescription('od', '', [dod, a])
2020-01-22 20:46:18 +01:00
async with await Config(od) as cfg:
w = weakref.ref(a)
x = weakref.ref(b)
y = weakref.ref(od)
z = weakref.ref(cfg)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(a)
del(b)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del(od)
del(dod)
assert w() is not None
assert x() is not None
assert w() is not None
assert x() is not None
del cfg
assert y() is None
assert z() is None
2020-01-22 20:46:18 +01:00
assert not await list_sessions()