Beispiel #1
0
async def test_basic_watch(client, loop):
    """Watches which don't actually watch"""
    # object type registration
    types = EtcTypes()
    twotypes = EtcTypes()

    @twotypes.register()
    class rTwo(EtcDir):
        pass

    class rDie(EtcValue):
        async def has_update(self):
            raise RuntimeError("RIP")

    @twotypes.register("die")
    class rPreDie(EtcValue):
        @classmethod
        async def this_obj(cls, recursive=None, **kw):
            return rDie(**kw)

    # reg funcion shall return the right thing
    types.step('two', dest=twotypes)
    assert types[('two', 'die')] is rPreDie
    assert types.lookup(('two', 'die'), dir=False) is rPreDie
    assert types.lookup('two', 'die', dir=False) is rPreDie
    assert types.lookup('two/die', dir=False) is rPreDie
    assert types.lookup('two', dir=True, raw=True).lookup('die',
                                                          dir=False) is rPreDie
    assert types.lookup('two/die', dir=False,
                        raw=True).lookup(dir=False) is rPreDie
    i = types.register("two", "vier", cls=EtcBoolean)
    assert i is EtcBoolean
    i = types.register("*/vierixx")(EtcInteger)
    assert i is EtcInteger
    types['what/ever'] = EtcFloat
    types['what/ever'] = rTwo
    assert types.lookup('what', 'ever', dir=False) is EtcFloat
    assert types.lookup('what', 'ever', dir=True) is rTwo
    assert types['what/ever'] is EtcFloat
    with pytest.raises(AssertionError):
        types['/what/ever']
    with pytest.raises(AssertionError):
        types['what/ever/']
    with pytest.raises(AssertionError):
        types['what//ever']
    types['something/else'] = EtcInteger
    assert types['two/vier'] is EtcBoolean
    assert types['something/else'] is EtcInteger
    assert types['not/not'] is None

    d = dict
    t = client
    d1 = d(one="eins",
           two=d(zwei=d(und="drei", a=d(b=d(c='d'))), vier="true"),
           x="y")
    await t._f(d1)

    # basic access, each directory separately
    class xRoot(EtcRoot):
        pass

    types.register(cls=xRoot)

    @xRoot.register("zwei", "und")
    class xUnd(EtcString):
        pass

    w = await t.tree("/two", immediate=False, static=True, types=types)
    w.env.foobar = "Foo Bar"
    assert sorted(dict(
        (a, b) for a, b, c in w.registrations()).items()) == sorted([
            (('.', ), [None, xRoot]),
            (('.', 'something', 'else'), [EtcInteger, None]),
            (('.', '*', 'vierixx'), [EtcInteger, None]),
            (('.', 'what', 'ever'), [EtcFloat, rTwo]),
            (('.', 'two'), [None, rTwo]),
            (('.', 'two', 'die'), [rPreDie, None]),
            (('.', 'two', 'vier'), [EtcBoolean, None]),
            (('zwei', 'und'), [xUnd, None]),
        ]), list(w.registrations())
    assert isinstance(w, xRoot)
    assert w.env.foobar == "Foo Bar"
    assert w.env.barbaz is None
    assert w['zwei'].env is w.env
    assert w['zwei']['a']['b'].env is w.env

    assert w['zwei']['und'] == "drei"
    assert type(w['zwei'].get('und', raw=True)) is xUnd
    assert w['vier'] == "true"
    with pytest.raises(KeyError):
        w['x']
    # basic access, read it all at once
    w2 = await t.tree("/two", immediate=True, static=True, types=types)
    assert w2['zwei']['und'] == "drei"
    assert w['vier'] == "true"
    assert w == w2

    # basic access, read it on demand
    w5 = await t.tree("/two", immediate=None, types=types)

    def wx(x):
        assert x.added == {'und', 'a'}
        x.test_called = 1

    mx = w5['zwei'].add_monitor(wx)
    assert isinstance(w5['zwei']['und'], EtcAwaiter)
    assert (await w5['zwei']['und']).value == "drei"
    assert w5['vier'] == "true"
    await w5['zwei'].force_updated()
    assert w5['zwei'].test_called

    # use typed subtrees
    w4 = await t.tree((), types=types)
    await w4.set('two', d(sechs="sieben"))
    w3 = await t.tree("/", static=True, types=types)
    assert w3['two']['vier'] is True
    assert w3['two']['sechs'] == "sieben"
    ##assert not w3['two'] == w2
    # which are different, but not because of the tree types
    assert not w3 is w4
    assert w3 == w4

    # check basic node iterators
    res = set()
    for v in w3['two']['zwei'].values():
        assert not isinstance(v, EtcValue)
        if not isinstance(v, EtcDir):
            res.add(v)
    assert res == {"drei"}

    res = set()
    for k in w3['two'].keys():
        res.add(k)
    assert res == {"zwei", "vier", "sechs"}

    res = set()
    for k, v in w3['two'].items():
        res.add(k)
        assert v == w3['two'][k]
    assert res == {"zwei", "vier", "sechs"}

    # check what happens if an updater dies on us
    await w4['two'].set('hello', 'one')
    await w4['two'].set('die', '42')
    await asyncio.sleep(1.5, loop=loop)
    with pytest.raises(RuntimeError):
        await w4['two'].set('hello', 'two')

    await w.close()
    await w2.close()
    await w3.close()
    await w4.close()
    await w5.close()
Beispiel #2
0
async def test_basic_watch(client,loop):
    """Watches which don't actually watch"""
    # object type registration
    types = EtcTypes()
    twotypes = EtcTypes()
    @twotypes.register()
    class rTwo(EtcDir):
        pass
    class rDie(EtcValue):
        def has_update(self):
            raise RuntimeError("RIP")
    @twotypes.register("die")
    class rPreDie(EtcValue):
        @classmethod
        async def this_obj(cls,recursive=None,**kw):
            return rDie(**kw)
    # reg funcion shall return the right thing
    types.step('two',dest=twotypes)
    assert types[('two','die')] is rPreDie
    assert types.lookup(('two','die'),dir=False) is rPreDie
    assert types.lookup('two','die',dir=False) is rPreDie
    assert types.lookup('two/die',dir=False) is rPreDie
    assert types.lookup('two',dir=True,raw=True).lookup('die',dir=False) is rPreDie
    assert types.lookup('two/die',dir=False,raw=True).lookup(dir=False) is rPreDie
    i = types.register("two","vier", cls=EtcInteger)
    assert i is EtcInteger
    i = types.register("*/vierixx")(EtcInteger)
    assert i is EtcInteger
    types['what/ever'] = EtcFloat
    assert types.lookup('what','ever', dir=False) is EtcFloat
    assert types['what/ever'] is EtcFloat
    with pytest.raises(AssertionError):
        types['/what/ever']
    with pytest.raises(AssertionError):
        types['what/ever/']
    with pytest.raises(AssertionError):
        types['what//ever']
    types['something/else'] = EtcInteger
    assert types['two/vier'] is EtcInteger
    assert types['something/else'] is EtcInteger
    assert types['not/not'] is None

    d=dict
    t = client
    d1=d(one="eins",two=d(zwei=d(und="drei",a=d(b=d(c='d'))),vier="5"),x="y")
    await t._f(d1)
    # basic access, each directory separately
    class xRoot(EtcRoot):
        pass
    types.register(cls=xRoot)
    @xRoot.register("zwei","und")
    class xUnd(EtcString):
        pass
    w = await t.tree("/two", immediate=False, static=True, types=types)
    w.env.foobar="Foo Bar"
    assert isinstance(w,xRoot)
    assert w.env.foobar == "Foo Bar"
    assert w.env.barbaz is None
    assert w['zwei'].env is w.env
    assert w['zwei']['a']['b'].env is w.env

    assert w['zwei']['und'] == "drei"
    assert type(w['zwei']._get('und')) is xUnd
    assert w['vier'] == "5"
    with pytest.raises(KeyError):
        w['x']
    # basic access, read it all at once
    w2 = await t.tree("/two", immediate=True, static=True, types=types)
    assert w2['zwei']['und'] == "drei"
    assert w['vier'] == "5"
    assert w == w2

    # basic access, read it on demand
    w5 = await t.tree("/two", immediate=None, types=types)
    def wx(x):
        assert x.added == {'und','a'}
        x.test_called = 1
    mx = w5['zwei'].add_monitor(wx)
    assert isinstance(w5['zwei']['und'],EtcAwaiter)
    assert (await w5['zwei']['und']).value == "drei"
    assert w5['vier'] == "5"
    w5['zwei'].force_updated()
    assert w5['zwei'].test_called

    # use typed subtrees
    w4 = await t.tree((), types=types)
    await w4.set('two',d(sechs="sieben"))
    w3 = await t.tree("/", static=True, types=types)
    assert w3['two']['vier'] == 5
    assert w3['two']['sechs'] == "sieben"
    assert not w3['two'] == w2
    # which are different, but not because of the tree types
    assert not w3 is w4
    assert w3 == w4

    # check basic node iterators
    res=set()
    for v in w3['two']['zwei'].values():
        assert not isinstance(v,EtcValue)
        if not isinstance(v,EtcDir):
            res.add(v)
    assert res == {"drei"}

    res=set()
    for k in w3['two'].keys():
        res.add(k)
    assert res == {"zwei","vier","sechs"}

    res=set()
    for k,v in w3['two'].items():
        res.add(k)
        assert v == w3['two'][k]
    assert res == {"zwei","vier","sechs"}

    # check what happens if an updater dies on us
    await w4['two'].set('hello','one')
    await w4['two'].set('die',42)
    await asyncio.sleep(1.5, loop=loop)
    with pytest.raises(WatchStopped):
        await w4['two'].set('hello','two')
    
    await w2.close()
    await w3.close()
    await w4.close()